]> git.lizzy.rs Git - minetest.git/blob - src/serialization.cpp
Properly and efficiently use split utility headers
[minetest.git] / src / serialization.cpp
1 /*
2 Minetest-c55
3 Copyright (C) 2010 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 #ifdef _WIN32
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(SharedBuffer<u8> data, std::ostream &os)
57 {
58         z_stream z;
59         const s32 bufsize = 16384;
60         //char input_buffer[bufsize];
61         char output_buffer[bufsize];
62         int input_i = 0;
63         int status = 0;
64         int ret;
65
66         z.zalloc = Z_NULL;
67         z.zfree = Z_NULL;
68         z.opaque = Z_NULL;
69
70         ret = deflateInit(&z, -1);
71         if(ret != Z_OK)
72                 throw SerializationError("compressZlib: deflateInit failed");
73         
74         z.avail_in = 0;
75         
76         for(;;)
77         {
78                 int flush = Z_NO_FLUSH;
79                 z.next_out = (Bytef*)output_buffer;
80                 z.avail_out = bufsize;
81
82                 if(z.avail_in == 0)
83                 {
84                         //z.next_in = (char*)&data[input_i];
85                         z.next_in = (Bytef*)&data[input_i];
86                         z.avail_in = data.getSize() - input_i;
87                         input_i += z.avail_in;
88                         if(input_i == (int)data.getSize())
89                                 flush = Z_FINISH;
90                 }
91                 if(z.avail_in == 0)
92                         break;
93                 status = deflate(&z, flush);
94                 if(status == Z_NEED_DICT || status == Z_DATA_ERROR
95                                 || status == Z_MEM_ERROR)
96                 {
97                         zerr(status);
98                         throw SerializationError("compressZlib: deflate failed");
99                 }
100                 int count = bufsize - z.avail_out;
101                 if(count)
102                         os.write(output_buffer, count);
103         }
104
105         deflateEnd(&z);
106
107 }
108
109 void compressZlib(const std::string &data, std::ostream &os)
110 {
111         SharedBuffer<u8> databuf((u8*)data.c_str(), data.size());
112         compressZlib(databuf, os);
113 }
114
115 void decompressZlib(std::istream &is, std::ostream &os)
116 {
117         z_stream z;
118         const s32 bufsize = 16384;
119         char input_buffer[bufsize];
120         char output_buffer[bufsize];
121         int status = 0;
122         int ret;
123         int bytes_read = 0;
124         int input_buffer_len = 0;
125
126         z.zalloc = Z_NULL;
127         z.zfree = Z_NULL;
128         z.opaque = Z_NULL;
129
130         ret = inflateInit(&z);
131         if(ret != Z_OK)
132                 throw SerializationError("dcompressZlib: inflateInit failed");
133         
134         z.avail_in = 0;
135         
136         //dstream<<"initial fail="<<is.fail()<<" bad="<<is.bad()<<std::endl;
137
138         for(;;)
139         {
140                 z.next_out = (Bytef*)output_buffer;
141                 z.avail_out = bufsize;
142
143                 if(z.avail_in == 0)
144                 {
145                         z.next_in = (Bytef*)input_buffer;
146                         input_buffer_len = is.readsome(input_buffer, bufsize);
147                         z.avail_in = input_buffer_len;
148                         //dstream<<"read fail="<<is.fail()<<" bad="<<is.bad()<<std::endl;
149                 }
150                 if(z.avail_in == 0)
151                 {
152                         //dstream<<"z.avail_in == 0"<<std::endl;
153                         break;
154                 }
155                         
156                 //dstream<<"1 z.avail_in="<<z.avail_in<<std::endl;
157                 status = inflate(&z, Z_NO_FLUSH);
158                 //dstream<<"2 z.avail_in="<<z.avail_in<<std::endl;
159                 bytes_read += is.gcount() - z.avail_in;
160                 //dstream<<"bytes_read="<<bytes_read<<std::endl;
161
162                 if(status == Z_NEED_DICT || status == Z_DATA_ERROR
163                                 || status == Z_MEM_ERROR)
164                 {
165                         zerr(status);
166                         throw SerializationError("decompressZlib: inflate failed");
167                 }
168                 int count = bufsize - z.avail_out;
169                 //dstream<<"count="<<count<<std::endl;
170                 if(count)
171                         os.write(output_buffer, count);
172                 if(status == Z_STREAM_END)
173                 {
174                         //dstream<<"Z_STREAM_END"<<std::endl;
175                         
176                         //dstream<<"z.avail_in="<<z.avail_in<<std::endl;
177                         //dstream<<"fail="<<is.fail()<<" bad="<<is.bad()<<std::endl;
178                         // Unget all the data that inflate didn't take
179                         for(u32 i=0; i < z.avail_in; i++)
180                         {
181                                 is.unget();
182                                 if(is.fail() || is.bad())
183                                 {
184                                         dstream<<"unget #"<<i<<" failed"<<std::endl;
185                                         dstream<<"fail="<<is.fail()<<" bad="<<is.bad()<<std::endl;
186                                         throw SerializationError("decompressZlib: unget failed");
187                                 }
188                         }
189                         
190                         break;
191                 }
192         }
193
194         inflateEnd(&z);
195 }
196
197 void compress(SharedBuffer<u8> data, std::ostream &os, u8 version)
198 {
199         if(version >= 11)
200         {
201                 compressZlib(data, os);
202                 return;
203         }
204
205         if(data.getSize() == 0)
206                 return;
207         
208         // Write length (u32)
209
210         u8 tmp[4];
211         writeU32(tmp, data.getSize());
212         os.write((char*)tmp, 4);
213         
214         // We will be writing 8-bit pairs of more_count and byte
215         u8 more_count = 0;
216         u8 current_byte = data[0];
217         for(u32 i=1; i<data.getSize(); i++)
218         {
219                 if(
220                         data[i] != current_byte
221                         || more_count == 255
222                 )
223                 {
224                         // write count and byte
225                         os.write((char*)&more_count, 1);
226                         os.write((char*)&current_byte, 1);
227                         more_count = 0;
228                         current_byte = data[i];
229                 }
230                 else
231                 {
232                         more_count++;
233                 }
234         }
235         // write count and byte
236         os.write((char*)&more_count, 1);
237         os.write((char*)&current_byte, 1);
238 }
239
240 void decompress(std::istream &is, std::ostream &os, u8 version)
241 {
242         if(version >= 11)
243         {
244                 decompressZlib(is, os);
245                 return;
246         }
247
248         // Read length (u32)
249
250         u8 tmp[4];
251         is.read((char*)tmp, 4);
252         u32 len = readU32(tmp);
253         
254         // We will be reading 8-bit pairs of more_count and byte
255         u32 count = 0;
256         for(;;)
257         {
258                 u8 more_count=0;
259                 u8 byte=0;
260
261                 is.read((char*)&more_count, 1);
262                 
263                 is.read((char*)&byte, 1);
264
265                 if(is.eof())
266                         throw SerializationError("decompress: stream ended halfway");
267
268                 for(s32 i=0; i<(u16)more_count+1; i++)
269                         os.write((char*)&byte, 1);
270
271                 count += (u16)more_count+1;
272
273                 if(count == len)
274                         break;
275         }
276 }
277
278