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