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"
27 /* report a zlib or i/o error */
34 dstream<<"error reading stdin"<<std::endl;
36 dstream<<"error writing stdout"<<std::endl;
39 dstream<<"invalid compression level"<<std::endl;
42 dstream<<"invalid or incomplete deflate data"<<std::endl;
45 dstream<<"out of memory"<<std::endl;
48 dstream<<"zlib version mismatch!"<<std::endl;
51 dstream<<"return value = "<<ret<<std::endl;
55 void compressZlib(const u8 *data, size_t data_size, std::ostream &os, int level)
58 const s32 bufsize = 16384;
59 char output_buffer[bufsize];
67 ret = deflateInit(&z, level);
69 throw SerializationError("compressZlib: deflateInit failed");
71 // Point zlib to our input buffer
72 z.next_in = (Bytef*)&data[0];
73 z.avail_in = data_size;
77 z.next_out = (Bytef*)output_buffer;
78 z.avail_out = bufsize;
80 status = deflate(&z, Z_FINISH);
81 if(status == Z_NEED_DICT || status == Z_DATA_ERROR
82 || status == Z_MEM_ERROR)
85 throw SerializationError("compressZlib: deflate failed");
87 int count = bufsize - z.avail_out;
89 os.write(output_buffer, count);
90 // This determines zlib has given all output
91 if(status == Z_STREAM_END)
98 void compressZlib(const std::string &data, std::ostream &os, int level)
100 compressZlib((u8*)data.c_str(), data.size(), os, level);
103 void decompressZlib(std::istream &is, std::ostream &os, size_t limit)
106 const s32 bufsize = 16384;
107 char input_buffer[bufsize];
108 char output_buffer[bufsize];
112 int bytes_written = 0;
113 int input_buffer_len = 0;
119 ret = inflateInit(&z);
121 throw SerializationError("dcompressZlib: inflateInit failed");
125 //dstream<<"initial fail="<<is.fail()<<" bad="<<is.bad()<<std::endl;
129 int output_size = bufsize;
130 z.next_out = (Bytef*)output_buffer;
131 z.avail_out = output_size;
134 int limit_remaining = limit - bytes_written;
135 if (limit_remaining <= 0) {
136 // we're aborting ahead of time - throw an error?
139 if (limit_remaining < output_size) {
140 z.avail_out = output_size = limit_remaining;
146 z.next_in = (Bytef*)input_buffer;
147 is.read(input_buffer, bufsize);
148 input_buffer_len = is.gcount();
149 z.avail_in = input_buffer_len;
150 //dstream<<"read fail="<<is.fail()<<" bad="<<is.bad()<<std::endl;
154 //dstream<<"z.avail_in == 0"<<std::endl;
158 //dstream<<"1 z.avail_in="<<z.avail_in<<std::endl;
159 status = inflate(&z, Z_NO_FLUSH);
160 //dstream<<"2 z.avail_in="<<z.avail_in<<std::endl;
161 bytes_read += is.gcount() - z.avail_in;
162 //dstream<<"bytes_read="<<bytes_read<<std::endl;
164 if(status == Z_NEED_DICT || status == Z_DATA_ERROR
165 || status == Z_MEM_ERROR)
168 throw SerializationError("decompressZlib: inflate failed");
170 int count = output_size - z.avail_out;
171 //dstream<<"count="<<count<<std::endl;
173 os.write(output_buffer, count);
174 bytes_written += count;
175 if(status == Z_STREAM_END)
177 //dstream<<"Z_STREAM_END"<<std::endl;
179 //dstream<<"z.avail_in="<<z.avail_in<<std::endl;
180 //dstream<<"fail="<<is.fail()<<" bad="<<is.bad()<<std::endl;
181 // Unget all the data that inflate didn't take
182 is.clear(); // Just in case EOF is set
183 for(u32 i=0; i < z.avail_in; i++)
186 if(is.fail() || is.bad())
188 dstream<<"unget #"<<i<<" failed"<<std::endl;
189 dstream<<"fail="<<is.fail()<<" bad="<<is.bad()<<std::endl;
190 throw SerializationError("decompressZlib: unget failed");
201 struct ZSTD_Deleter {
202 void operator() (ZSTD_CStream* cstream) {
203 ZSTD_freeCStream(cstream);
206 void operator() (ZSTD_DStream* dstream) {
207 ZSTD_freeDStream(dstream);
211 void compressZstd(const u8 *data, size_t data_size, std::ostream &os, int level)
213 // reusing the context is recommended for performance
214 // it will destroyed when the thread ends
215 thread_local std::unique_ptr<ZSTD_CStream, ZSTD_Deleter> stream(ZSTD_createCStream());
217 ZSTD_initCStream(stream.get(), level);
219 const size_t bufsize = 16384;
220 char output_buffer[bufsize];
222 ZSTD_inBuffer input = { data, data_size, 0 };
223 ZSTD_outBuffer output = { output_buffer, bufsize, 0 };
225 while (input.pos < input.size) {
226 size_t ret = ZSTD_compressStream(stream.get(), &output, &input);
227 if (ZSTD_isError(ret)) {
228 dstream << ZSTD_getErrorName(ret) << std::endl;
229 throw SerializationError("compressZstd: failed");
232 os.write(output_buffer, output.pos);
239 ret = ZSTD_endStream(stream.get(), &output);
240 if (ZSTD_isError(ret)) {
241 dstream << ZSTD_getErrorName(ret) << std::endl;
242 throw SerializationError("compressZstd: failed");
245 os.write(output_buffer, output.pos);
252 void compressZstd(const std::string &data, std::ostream &os, int level)
254 compressZstd((u8*)data.c_str(), data.size(), os, level);
257 void decompressZstd(std::istream &is, std::ostream &os)
259 // reusing the context is recommended for performance
260 // it will destroyed when the thread ends
261 thread_local std::unique_ptr<ZSTD_DStream, ZSTD_Deleter> stream(ZSTD_createDStream());
263 ZSTD_initDStream(stream.get());
265 const size_t bufsize = 16384;
266 char output_buffer[bufsize];
267 char input_buffer[bufsize];
269 ZSTD_outBuffer output = { output_buffer, bufsize, 0 };
270 ZSTD_inBuffer input = { input_buffer, 0, 0 };
274 if (input.size == input.pos) {
275 is.read(input_buffer, bufsize);
276 input.size = is.gcount();
280 ret = ZSTD_decompressStream(stream.get(), &output, &input);
281 if (ZSTD_isError(ret)) {
282 dstream << ZSTD_getErrorName(ret) << std::endl;
283 throw SerializationError("decompressZstd: failed");
286 os.write(output_buffer, output.pos);
291 // Unget all the data that ZSTD_decompressStream didn't take
292 is.clear(); // Just in case EOF is set
293 for (u32 i = 0; i < input.size - input.pos; i++) {
295 if (is.fail() || is.bad())
296 throw SerializationError("decompressZstd: unget failed");
300 void compress(u8 *data, u32 size, std::ostream &os, u8 version, int level)
304 // map the zlib levels [0,9] to [1,10]. -1 becomes 0 which indicates the default (currently 3)
305 compressZstd(data, size, os, level + 1);
311 compressZlib(data, size, os, level);
318 // Write length (u32)
322 os.write((char*)tmp, 4);
324 // We will be writing 8-bit pairs of more_count and byte
326 u8 current_byte = data[0];
327 for(u32 i=1; i<size; i++)
330 data[i] != current_byte
334 // write count and byte
335 os.write((char*)&more_count, 1);
336 os.write((char*)¤t_byte, 1);
338 current_byte = data[i];
345 // write count and byte
346 os.write((char*)&more_count, 1);
347 os.write((char*)¤t_byte, 1);
350 void compress(const SharedBuffer<u8> &data, std::ostream &os, u8 version, int level)
352 compress(*data, data.getSize(), os, version, level);
355 void compress(const std::string &data, std::ostream &os, u8 version, int level)
357 compress((u8*)data.c_str(), data.size(), os, version, level);
360 void decompress(std::istream &is, std::ostream &os, u8 version)
364 decompressZstd(is, os);
370 decompressZlib(is, os);
377 is.read((char*)tmp, 4);
378 u32 len = readU32(tmp);
380 // We will be reading 8-bit pairs of more_count and byte
387 is.read((char*)&more_count, 1);
389 is.read((char*)&byte, 1);
392 throw SerializationError("decompress: stream ended halfway");
394 for(s32 i=0; i<(u16)more_count+1; i++)
395 os.write((char*)&byte, 1);
397 count += (u16)more_count+1;