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];
111 int bytes_written = 0;
112 int input_buffer_len = 0;
118 ret = inflateInit(&z);
120 throw SerializationError("dcompressZlib: inflateInit failed");
126 int output_size = bufsize;
127 z.next_out = (Bytef*)output_buffer;
128 z.avail_out = output_size;
131 int limit_remaining = limit - bytes_written;
132 if (limit_remaining <= 0) {
133 // we're aborting ahead of time - throw an error?
136 if (limit_remaining < output_size) {
137 z.avail_out = output_size = limit_remaining;
143 z.next_in = (Bytef*)input_buffer;
144 is.read(input_buffer, bufsize);
145 input_buffer_len = is.gcount();
146 z.avail_in = input_buffer_len;
153 status = inflate(&z, Z_NO_FLUSH);
155 if(status == Z_NEED_DICT || status == Z_DATA_ERROR
156 || status == Z_MEM_ERROR)
159 throw SerializationError("decompressZlib: inflate failed");
161 int count = output_size - z.avail_out;
163 os.write(output_buffer, count);
164 bytes_written += count;
165 if(status == Z_STREAM_END)
167 // Unget all the data that inflate didn't take
168 is.clear(); // Just in case EOF is set
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("decompressZlib: unget failed");
187 struct ZSTD_Deleter {
188 void operator() (ZSTD_CStream* cstream) {
189 ZSTD_freeCStream(cstream);
192 void operator() (ZSTD_DStream* dstream) {
193 ZSTD_freeDStream(dstream);
197 void compressZstd(const u8 *data, size_t data_size, std::ostream &os, int level)
199 // reusing the context is recommended for performance
200 // it will be destroyed when the thread ends
201 thread_local std::unique_ptr<ZSTD_CStream, ZSTD_Deleter> stream(ZSTD_createCStream());
204 ZSTD_initCStream(stream.get(), level);
206 const size_t bufsize = 16384;
207 char output_buffer[bufsize];
209 ZSTD_inBuffer input = { data, data_size, 0 };
210 ZSTD_outBuffer output = { output_buffer, bufsize, 0 };
212 while (input.pos < input.size) {
213 size_t ret = ZSTD_compressStream(stream.get(), &output, &input);
214 if (ZSTD_isError(ret)) {
215 dstream << ZSTD_getErrorName(ret) << std::endl;
216 throw SerializationError("compressZstd: failed");
219 os.write(output_buffer, output.pos);
226 ret = ZSTD_endStream(stream.get(), &output);
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 void compressZstd(const std::string &data, std::ostream &os, int level)
241 compressZstd((u8*)data.c_str(), data.size(), os, level);
244 void decompressZstd(std::istream &is, std::ostream &os)
246 // reusing the context is recommended for performance
247 // it will be destroyed when the thread ends
248 thread_local std::unique_ptr<ZSTD_DStream, ZSTD_Deleter> stream(ZSTD_createDStream());
250 ZSTD_initDStream(stream.get());
252 const size_t bufsize = 16384;
253 char output_buffer[bufsize];
254 char input_buffer[bufsize];
256 ZSTD_outBuffer output = { output_buffer, bufsize, 0 };
257 ZSTD_inBuffer input = { input_buffer, 0, 0 };
261 if (input.size == input.pos) {
262 is.read(input_buffer, bufsize);
263 input.size = is.gcount();
267 ret = ZSTD_decompressStream(stream.get(), &output, &input);
268 if (ZSTD_isError(ret)) {
269 dstream << ZSTD_getErrorName(ret) << std::endl;
270 throw SerializationError("decompressZstd: failed");
273 os.write(output_buffer, output.pos);
278 // Unget all the data that ZSTD_decompressStream didn't take
279 is.clear(); // Just in case EOF is set
280 for (u32 i = 0; i < input.size - input.pos; i++) {
282 if (is.fail() || is.bad())
283 throw SerializationError("decompressZstd: unget failed");
287 void compress(u8 *data, u32 size, std::ostream &os, u8 version, int level)
291 // map the zlib levels [0,9] to [1,10]. -1 becomes 0 which indicates the default (currently 3)
292 compressZstd(data, size, os, level + 1);
298 compressZlib(data, size, os, level);
305 // Write length (u32)
309 os.write((char*)tmp, 4);
311 // We will be writing 8-bit pairs of more_count and byte
313 u8 current_byte = data[0];
314 for(u32 i=1; i<size; i++)
317 data[i] != current_byte
321 // write count and byte
322 os.write((char*)&more_count, 1);
323 os.write((char*)¤t_byte, 1);
325 current_byte = data[i];
332 // write count and byte
333 os.write((char*)&more_count, 1);
334 os.write((char*)¤t_byte, 1);
337 void compress(const SharedBuffer<u8> &data, std::ostream &os, u8 version, int level)
339 compress(*data, data.getSize(), os, version, level);
342 void compress(const std::string &data, std::ostream &os, u8 version, int level)
344 compress((u8*)data.c_str(), data.size(), os, version, level);
347 void decompress(std::istream &is, std::ostream &os, u8 version)
351 decompressZstd(is, os);
357 decompressZlib(is, os);
364 is.read((char*)tmp, 4);
365 u32 len = readU32(tmp);
367 // We will be reading 8-bit pairs of more_count and byte
374 is.read((char*)&more_count, 1);
376 is.read((char*)&byte, 1);
379 throw SerializationError("decompress: stream ended halfway");
381 for(s32 i=0; i<(u16)more_count+1; i++)
382 os.write((char*)&byte, 1);
384 count += (u16)more_count+1;