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 be destroyed when the thread ends
215 thread_local std::unique_ptr<ZSTD_CStream, ZSTD_Deleter> stream(ZSTD_createCStream());
218 ZSTD_initCStream(stream.get(), level);
220 const size_t bufsize = 16384;
221 char output_buffer[bufsize];
223 ZSTD_inBuffer input = { data, data_size, 0 };
224 ZSTD_outBuffer output = { output_buffer, bufsize, 0 };
226 while (input.pos < input.size) {
227 size_t ret = ZSTD_compressStream(stream.get(), &output, &input);
228 if (ZSTD_isError(ret)) {
229 dstream << ZSTD_getErrorName(ret) << std::endl;
230 throw SerializationError("compressZstd: failed");
233 os.write(output_buffer, output.pos);
240 ret = ZSTD_endStream(stream.get(), &output);
241 if (ZSTD_isError(ret)) {
242 dstream << ZSTD_getErrorName(ret) << std::endl;
243 throw SerializationError("compressZstd: failed");
246 os.write(output_buffer, output.pos);
253 void compressZstd(const std::string &data, std::ostream &os, int level)
255 compressZstd((u8*)data.c_str(), data.size(), os, level);
258 void decompressZstd(std::istream &is, std::ostream &os)
260 // reusing the context is recommended for performance
261 // it will be destroyed when the thread ends
262 thread_local std::unique_ptr<ZSTD_DStream, ZSTD_Deleter> stream(ZSTD_createDStream());
264 ZSTD_initDStream(stream.get());
266 const size_t bufsize = 16384;
267 char output_buffer[bufsize];
268 char input_buffer[bufsize];
270 ZSTD_outBuffer output = { output_buffer, bufsize, 0 };
271 ZSTD_inBuffer input = { input_buffer, 0, 0 };
275 if (input.size == input.pos) {
276 is.read(input_buffer, bufsize);
277 input.size = is.gcount();
281 ret = ZSTD_decompressStream(stream.get(), &output, &input);
282 if (ZSTD_isError(ret)) {
283 dstream << ZSTD_getErrorName(ret) << std::endl;
284 throw SerializationError("decompressZstd: failed");
287 os.write(output_buffer, output.pos);
292 // Unget all the data that ZSTD_decompressStream didn't take
293 is.clear(); // Just in case EOF is set
294 for (u32 i = 0; i < input.size - input.pos; i++) {
296 if (is.fail() || is.bad())
297 throw SerializationError("decompressZstd: unget failed");
301 void compress(u8 *data, u32 size, std::ostream &os, u8 version, int level)
305 // map the zlib levels [0,9] to [1,10]. -1 becomes 0 which indicates the default (currently 3)
306 compressZstd(data, size, os, level + 1);
312 compressZlib(data, size, os, level);
319 // Write length (u32)
323 os.write((char*)tmp, 4);
325 // We will be writing 8-bit pairs of more_count and byte
327 u8 current_byte = data[0];
328 for(u32 i=1; i<size; i++)
331 data[i] != current_byte
335 // write count and byte
336 os.write((char*)&more_count, 1);
337 os.write((char*)¤t_byte, 1);
339 current_byte = data[i];
346 // write count and byte
347 os.write((char*)&more_count, 1);
348 os.write((char*)¤t_byte, 1);
351 void compress(const SharedBuffer<u8> &data, std::ostream &os, u8 version, int level)
353 compress(*data, data.getSize(), os, version, level);
356 void compress(const std::string &data, std::ostream &os, u8 version, int level)
358 compress((u8*)data.c_str(), data.size(), os, version, level);
361 void decompress(std::istream &is, std::ostream &os, u8 version)
365 decompressZstd(is, os);
371 decompressZlib(is, os);
378 is.read((char*)tmp, 4);
379 u32 len = readU32(tmp);
381 // We will be reading 8-bit pairs of more_count and byte
388 is.read((char*)&more_count, 1);
390 is.read((char*)&byte, 1);
393 throw SerializationError("decompress: stream ended halfway");
395 for(s32 i=0; i<(u16)more_count+1; i++)
396 os.write((char*)&byte, 1);
398 count += (u16)more_count+1;