]> git.lizzy.rs Git - minetest.git/blob - src/serialization.cpp
Add "MINETEST_MOD_PATH" environment variable (#11515)
[minetest.git] / src / serialization.cpp
1 /*
2 Minetest
3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
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.
9
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.
14
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.
18 */
19
20 #include "serialization.h"
21
22 #include "util/serialize.h"
23
24 #include <zlib.h>
25 #include <zstd.h>
26
27 /* report a zlib or i/o error */
28 void zerr(int ret)
29 {
30     dstream<<"zerr: ";
31     switch (ret) {
32     case Z_ERRNO:
33         if (ferror(stdin))
34             dstream<<"error reading stdin"<<std::endl;
35         if (ferror(stdout))
36             dstream<<"error writing stdout"<<std::endl;
37         break;
38     case Z_STREAM_ERROR:
39         dstream<<"invalid compression level"<<std::endl;
40         break;
41     case Z_DATA_ERROR:
42         dstream<<"invalid or incomplete deflate data"<<std::endl;
43         break;
44     case Z_MEM_ERROR:
45         dstream<<"out of memory"<<std::endl;
46         break;
47     case Z_VERSION_ERROR:
48         dstream<<"zlib version mismatch!"<<std::endl;
49                 break;
50         default:
51                 dstream<<"return value = "<<ret<<std::endl;
52     }
53 }
54
55 void compressZlib(const u8 *data, size_t data_size, std::ostream &os, int level)
56 {
57         z_stream z;
58         const s32 bufsize = 16384;
59         char output_buffer[bufsize];
60         int status = 0;
61         int ret;
62
63         z.zalloc = Z_NULL;
64         z.zfree = Z_NULL;
65         z.opaque = Z_NULL;
66
67         ret = deflateInit(&z, level);
68         if(ret != Z_OK)
69                 throw SerializationError("compressZlib: deflateInit failed");
70
71         // Point zlib to our input buffer
72         z.next_in = (Bytef*)&data[0];
73         z.avail_in = data_size;
74         // And get all output
75         for(;;)
76         {
77                 z.next_out = (Bytef*)output_buffer;
78                 z.avail_out = bufsize;
79
80                 status = deflate(&z, Z_FINISH);
81                 if(status == Z_NEED_DICT || status == Z_DATA_ERROR
82                                 || status == Z_MEM_ERROR)
83                 {
84                         zerr(status);
85                         throw SerializationError("compressZlib: deflate failed");
86                 }
87                 int count = bufsize - z.avail_out;
88                 if(count)
89                         os.write(output_buffer, count);
90                 // This determines zlib has given all output
91                 if(status == Z_STREAM_END)
92                         break;
93         }
94
95         deflateEnd(&z);
96 }
97
98 void compressZlib(const std::string &data, std::ostream &os, int level)
99 {
100         compressZlib((u8*)data.c_str(), data.size(), os, level);
101 }
102
103 void decompressZlib(std::istream &is, std::ostream &os, size_t limit)
104 {
105         z_stream z;
106         const s32 bufsize = 16384;
107         char input_buffer[bufsize];
108         char output_buffer[bufsize];
109         int status = 0;
110         int ret;
111         int bytes_read = 0;
112         int bytes_written = 0;
113         int input_buffer_len = 0;
114
115         z.zalloc = Z_NULL;
116         z.zfree = Z_NULL;
117         z.opaque = Z_NULL;
118
119         ret = inflateInit(&z);
120         if(ret != Z_OK)
121                 throw SerializationError("dcompressZlib: inflateInit failed");
122
123         z.avail_in = 0;
124
125         //dstream<<"initial fail="<<is.fail()<<" bad="<<is.bad()<<std::endl;
126
127         for(;;)
128         {
129                 int output_size = bufsize;
130                 z.next_out = (Bytef*)output_buffer;
131                 z.avail_out = output_size;
132
133                 if (limit) {
134                         int limit_remaining = limit - bytes_written;
135                         if (limit_remaining <= 0) {
136                                 // we're aborting ahead of time - throw an error?
137                                 break;
138                         }
139                         if (limit_remaining < output_size) {
140                                 z.avail_out = output_size = limit_remaining;
141                         }
142                 }
143
144                 if(z.avail_in == 0)
145                 {
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;
151                 }
152                 if(z.avail_in == 0)
153                 {
154                         //dstream<<"z.avail_in == 0"<<std::endl;
155                         break;
156                 }
157
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;
163
164                 if(status == Z_NEED_DICT || status == Z_DATA_ERROR
165                                 || status == Z_MEM_ERROR)
166                 {
167                         zerr(status);
168                         throw SerializationError("decompressZlib: inflate failed");
169                 }
170                 int count = output_size - z.avail_out;
171                 //dstream<<"count="<<count<<std::endl;
172                 if(count)
173                         os.write(output_buffer, count);
174                 bytes_written += count;
175                 if(status == Z_STREAM_END)
176                 {
177                         //dstream<<"Z_STREAM_END"<<std::endl;
178
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++)
184                         {
185                                 is.unget();
186                                 if(is.fail() || is.bad())
187                                 {
188                                         dstream<<"unget #"<<i<<" failed"<<std::endl;
189                                         dstream<<"fail="<<is.fail()<<" bad="<<is.bad()<<std::endl;
190                                         throw SerializationError("decompressZlib: unget failed");
191                                 }
192                         }
193
194                         break;
195                 }
196         }
197
198         inflateEnd(&z);
199 }
200
201 struct ZSTD_Deleter {
202         void operator() (ZSTD_CStream* cstream) {
203                 ZSTD_freeCStream(cstream);
204         }
205
206         void operator() (ZSTD_DStream* dstream) {
207                 ZSTD_freeDStream(dstream);
208         }
209 };
210
211 void compressZstd(const u8 *data, size_t data_size, std::ostream &os, int level)
212 {
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());
216
217         ZSTD_initCStream(stream.get(), level);
218
219         const size_t bufsize = 16384;
220         char output_buffer[bufsize];
221
222         ZSTD_inBuffer input = { data, data_size, 0 };
223         ZSTD_outBuffer output = { output_buffer, bufsize, 0 };
224
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");
230                 }
231                 if (output.pos) {
232                         os.write(output_buffer, output.pos);
233                         output.pos = 0;
234                 }
235         }
236
237         size_t ret;
238         do {
239                 ret = ZSTD_endStream(stream.get(), &output);
240                 if (ZSTD_isError(ret)) {
241                         dstream << ZSTD_getErrorName(ret) << std::endl;
242                         throw SerializationError("compressZstd: failed");
243                 }
244                 if (output.pos) {
245                         os.write(output_buffer, output.pos);
246                         output.pos = 0;
247                 }
248         } while (ret != 0);
249
250 }
251
252 void compressZstd(const std::string &data, std::ostream &os, int level)
253 {
254         compressZstd((u8*)data.c_str(), data.size(), os, level);
255 }
256
257 void decompressZstd(std::istream &is, std::ostream &os)
258 {
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());
262
263         ZSTD_initDStream(stream.get());
264
265         const size_t bufsize = 16384;
266         char output_buffer[bufsize];
267         char input_buffer[bufsize];
268
269         ZSTD_outBuffer output = { output_buffer, bufsize, 0 };
270         ZSTD_inBuffer input = { input_buffer, 0, 0 };
271         size_t ret;
272         do
273         {
274                 if (input.size == input.pos) {
275                         is.read(input_buffer, bufsize);
276                         input.size = is.gcount();
277                         input.pos = 0;
278                 }
279
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");
284                 }
285                 if (output.pos) {
286                         os.write(output_buffer, output.pos);
287                         output.pos = 0;
288                 }
289         } while (ret != 0);
290
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++) {
294                 is.unget();
295                 if (is.fail() || is.bad())
296                         throw SerializationError("decompressZstd: unget failed");
297         }
298 }
299
300 void compress(u8 *data, u32 size, std::ostream &os, u8 version, int level)
301 {
302         if(version >= 29)
303         {
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);
306                 return;
307         }
308
309         if(version >= 11)
310         {
311                 compressZlib(data, size, os, level);
312                 return;
313         }
314
315         if(size == 0)
316                 return;
317
318         // Write length (u32)
319
320         u8 tmp[4];
321         writeU32(tmp, size);
322         os.write((char*)tmp, 4);
323
324         // We will be writing 8-bit pairs of more_count and byte
325         u8 more_count = 0;
326         u8 current_byte = data[0];
327         for(u32 i=1; i<size; i++)
328         {
329                 if(
330                         data[i] != current_byte
331                         || more_count == 255
332                 )
333                 {
334                         // write count and byte
335                         os.write((char*)&more_count, 1);
336                         os.write((char*)&current_byte, 1);
337                         more_count = 0;
338                         current_byte = data[i];
339                 }
340                 else
341                 {
342                         more_count++;
343                 }
344         }
345         // write count and byte
346         os.write((char*)&more_count, 1);
347         os.write((char*)&current_byte, 1);
348 }
349
350 void compress(const SharedBuffer<u8> &data, std::ostream &os, u8 version, int level)
351 {
352         compress(*data, data.getSize(), os, version, level);
353 }
354
355 void compress(const std::string &data, std::ostream &os, u8 version, int level)
356 {
357         compress((u8*)data.c_str(), data.size(), os, version, level);
358 }
359
360 void decompress(std::istream &is, std::ostream &os, u8 version)
361 {
362         if(version >= 29)
363         {
364                 decompressZstd(is, os);
365                 return;
366         }
367
368         if(version >= 11)
369         {
370                 decompressZlib(is, os);
371                 return;
372         }
373
374         // Read length (u32)
375
376         u8 tmp[4];
377         is.read((char*)tmp, 4);
378         u32 len = readU32(tmp);
379
380         // We will be reading 8-bit pairs of more_count and byte
381         u32 count = 0;
382         for(;;)
383         {
384                 u8 more_count=0;
385                 u8 byte=0;
386
387                 is.read((char*)&more_count, 1);
388
389                 is.read((char*)&byte, 1);
390
391                 if(is.eof())
392                         throw SerializationError("decompress: stream ended halfway");
393
394                 for(s32 i=0; i<(u16)more_count+1; i++)
395                         os.write((char*)&byte, 1);
396
397                 count += (u16)more_count+1;
398
399                 if(count == len)
400                         break;
401         }
402 }
403
404