]> git.lizzy.rs Git - minetest.git/blob - src/serialization.cpp
Optimize headers (part 2) (#6272)
[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 #if defined(_WIN32) && !defined(WIN32_NO_ZLIB_WINAPI)
24         #define ZLIB_WINAPI
25 #endif
26 #include "zlib.h"
27
28 /* report a zlib or i/o error */
29 void zerr(int ret)
30 {
31     dstream<<"zerr: ";
32     switch (ret) {
33     case Z_ERRNO:
34         if (ferror(stdin))
35             dstream<<"error reading stdin"<<std::endl;
36         if (ferror(stdout))
37             dstream<<"error writing stdout"<<std::endl;
38         break;
39     case Z_STREAM_ERROR:
40         dstream<<"invalid compression level"<<std::endl;
41         break;
42     case Z_DATA_ERROR:
43         dstream<<"invalid or incomplete deflate data"<<std::endl;
44         break;
45     case Z_MEM_ERROR:
46         dstream<<"out of memory"<<std::endl;
47         break;
48     case Z_VERSION_ERROR:
49         dstream<<"zlib version mismatch!"<<std::endl;
50                 break;
51         default:
52                 dstream<<"return value = "<<ret<<std::endl;
53     }
54 }
55
56 void compressZlib(const u8 *data, size_t data_size, std::ostream &os, int level)
57 {
58         z_stream z;
59         const s32 bufsize = 16384;
60         char output_buffer[bufsize];
61         int status = 0;
62         int ret;
63
64         z.zalloc = Z_NULL;
65         z.zfree = Z_NULL;
66         z.opaque = Z_NULL;
67
68         ret = deflateInit(&z, level);
69         if(ret != Z_OK)
70                 throw SerializationError("compressZlib: deflateInit failed");
71
72         // Point zlib to our input buffer
73         z.next_in = (Bytef*)&data[0];
74         z.avail_in = data_size;
75         // And get all output
76         for(;;)
77         {
78                 z.next_out = (Bytef*)output_buffer;
79                 z.avail_out = bufsize;
80
81                 status = deflate(&z, Z_FINISH);
82                 if(status == Z_NEED_DICT || status == Z_DATA_ERROR
83                                 || status == Z_MEM_ERROR)
84                 {
85                         zerr(status);
86                         throw SerializationError("compressZlib: deflate failed");
87                 }
88                 int count = bufsize - z.avail_out;
89                 if(count)
90                         os.write(output_buffer, count);
91                 // This determines zlib has given all output
92                 if(status == Z_STREAM_END)
93                         break;
94         }
95
96         deflateEnd(&z);
97 }
98
99 void compressZlib(const std::string &data, std::ostream &os, int level)
100 {
101         compressZlib((u8*)data.c_str(), data.size(), os, level);
102 }
103
104 void decompressZlib(std::istream &is, std::ostream &os)
105 {
106         z_stream z;
107         const s32 bufsize = 16384;
108         char input_buffer[bufsize];
109         char output_buffer[bufsize];
110         int status = 0;
111         int ret;
112         int bytes_read = 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                 z.next_out = (Bytef*)output_buffer;
130                 z.avail_out = bufsize;
131
132                 if(z.avail_in == 0)
133                 {
134                         z.next_in = (Bytef*)input_buffer;
135                         is.read(input_buffer, bufsize);
136                         input_buffer_len = is.gcount();
137                         z.avail_in = input_buffer_len;
138                         //dstream<<"read fail="<<is.fail()<<" bad="<<is.bad()<<std::endl;
139                 }
140                 if(z.avail_in == 0)
141                 {
142                         //dstream<<"z.avail_in == 0"<<std::endl;
143                         break;
144                 }
145
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;
151
152                 if(status == Z_NEED_DICT || status == Z_DATA_ERROR
153                                 || status == Z_MEM_ERROR)
154                 {
155                         zerr(status);
156                         throw SerializationError("decompressZlib: inflate failed");
157                 }
158                 int count = bufsize - z.avail_out;
159                 //dstream<<"count="<<count<<std::endl;
160                 if(count)
161                         os.write(output_buffer, count);
162                 if(status == Z_STREAM_END)
163                 {
164                         //dstream<<"Z_STREAM_END"<<std::endl;
165
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                         is.clear(); // Just in case EOF is set
170                         for(u32 i=0; i < z.avail_in; i++)
171                         {
172                                 is.unget();
173                                 if(is.fail() || is.bad())
174                                 {
175                                         dstream<<"unget #"<<i<<" failed"<<std::endl;
176                                         dstream<<"fail="<<is.fail()<<" bad="<<is.bad()<<std::endl;
177                                         throw SerializationError("decompressZlib: unget failed");
178                                 }
179                         }
180
181                         break;
182                 }
183         }
184
185         inflateEnd(&z);
186 }
187
188 void compress(const SharedBuffer<u8> &data, std::ostream &os, u8 version)
189 {
190         if(version >= 11)
191         {
192                 compressZlib(*data ,data.getSize(), os);
193                 return;
194         }
195
196         if(data.getSize() == 0)
197                 return;
198
199         // Write length (u32)
200
201         u8 tmp[4];
202         writeU32(tmp, data.getSize());
203         os.write((char*)tmp, 4);
204
205         // We will be writing 8-bit pairs of more_count and byte
206         u8 more_count = 0;
207         u8 current_byte = data[0];
208         for(u32 i=1; i<data.getSize(); i++)
209         {
210                 if(
211                         data[i] != current_byte
212                         || more_count == 255
213                 )
214                 {
215                         // write count and byte
216                         os.write((char*)&more_count, 1);
217                         os.write((char*)&current_byte, 1);
218                         more_count = 0;
219                         current_byte = data[i];
220                 }
221                 else
222                 {
223                         more_count++;
224                 }
225         }
226         // write count and byte
227         os.write((char*)&more_count, 1);
228         os.write((char*)&current_byte, 1);
229 }
230
231 void decompress(std::istream &is, std::ostream &os, u8 version)
232 {
233         if(version >= 11)
234         {
235                 decompressZlib(is, os);
236                 return;
237         }
238
239         // Read length (u32)
240
241         u8 tmp[4];
242         is.read((char*)tmp, 4);
243         u32 len = readU32(tmp);
244
245         // We will be reading 8-bit pairs of more_count and byte
246         u32 count = 0;
247         for(;;)
248         {
249                 u8 more_count=0;
250                 u8 byte=0;
251
252                 is.read((char*)&more_count, 1);
253
254                 is.read((char*)&byte, 1);
255
256                 if(is.eof())
257                         throw SerializationError("decompress: stream ended halfway");
258
259                 for(s32 i=0; i<(u16)more_count+1; i++)
260                         os.write((char*)&byte, 1);
261
262                 count += (u16)more_count+1;
263
264                 if(count == len)
265                         break;
266         }
267 }
268
269