better graphics, zlib to work on vc++
[oweals/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 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     fputs("zerr: ", stderr);
31     switch (ret) {
32     case Z_ERRNO:
33         if (ferror(stdin))
34             fputs("error reading stdin\n", stderr);
35         if (ferror(stdout))
36             fputs("error writing stdout\n", stderr);
37         break;
38     case Z_STREAM_ERROR:
39         fputs("invalid compression level\n", stderr);
40         break;
41     case Z_DATA_ERROR:
42         fputs("invalid or incomplete deflate data\n", stderr);
43         break;
44     case Z_MEM_ERROR:
45         fputs("out of memory\n", stderr);
46         break;
47     case Z_VERSION_ERROR:
48         fputs("zlib version mismatch!\n", stderr);
49                 break;
50         default:
51                 dstream<<"return value = "<<ret<<"\n";
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 decompressZlib(std::istream &is, std::ostream &os)
109 {
110         z_stream z;
111         const s32 bufsize = 16384;
112         char input_buffer[bufsize];
113         char output_buffer[bufsize];
114         int status = 0;
115         int ret;
116         int bytes_read = 0;
117         int input_buffer_len = 0;
118
119         z.zalloc = Z_NULL;
120         z.zfree = Z_NULL;
121         z.opaque = Z_NULL;
122
123         ret = inflateInit(&z);
124         if(ret != Z_OK)
125                 throw SerializationError("compressZlib: inflateInit failed");
126         
127         z.avail_in = 0;
128         
129         //dstream<<"initial fail="<<is.fail()<<" bad="<<is.bad()<<std::endl;
130
131         for(;;)
132         {
133                 z.next_out = (Bytef*)output_buffer;
134                 z.avail_out = bufsize;
135
136                 if(z.avail_in == 0)
137                 {
138                         z.next_in = (Bytef*)input_buffer;
139                         input_buffer_len = is.readsome(input_buffer, bufsize);
140                         z.avail_in = input_buffer_len;
141                         //dstream<<"read fail="<<is.fail()<<" bad="<<is.bad()<<std::endl;
142                 }
143                 if(z.avail_in == 0)
144                 {
145                         //dstream<<"z.avail_in == 0"<<std::endl;
146                         break;
147                 }
148                         
149                 //dstream<<"1 z.avail_in="<<z.avail_in<<std::endl;
150                 status = inflate(&z, Z_NO_FLUSH);
151                 //dstream<<"2 z.avail_in="<<z.avail_in<<std::endl;
152                 bytes_read += is.gcount() - z.avail_in;
153                 //dstream<<"bytes_read="<<bytes_read<<std::endl;
154
155                 if(status == Z_NEED_DICT || status == Z_DATA_ERROR
156                                 || status == Z_MEM_ERROR)
157                 {
158                         zerr(status);
159                         throw SerializationError("compressZlib: inflate failed");
160                 }
161                 int count = bufsize - z.avail_out;
162                 //dstream<<"count="<<count<<std::endl;
163                 if(count)
164                         os.write(output_buffer, count);
165                 if(status == Z_STREAM_END)
166                 {
167                         //dstream<<"Z_STREAM_END"<<std::endl;
168                         
169                         //dstream<<"z.avail_in="<<z.avail_in<<std::endl;
170                         //dstream<<"fail="<<is.fail()<<" bad="<<is.bad()<<std::endl;
171                         // Unget all the data that inflate didn't take
172                         for(u32 i=0; i < z.avail_in; i++)
173                         {
174                                 is.unget();
175                                 if(is.fail() || is.bad())
176                                 {
177                                         dstream<<"unget #"<<i<<" failed"<<std::endl;
178                                         dstream<<"fail="<<is.fail()<<" bad="<<is.bad()<<std::endl;
179                                         throw SerializationError("compressZlib: unget failed");
180                                 }
181                         }
182                         
183                         break;
184                 }
185         }
186
187         inflateEnd(&z);
188 }
189
190 void compress(SharedBuffer<u8> data, std::ostream &os, u8 version)
191 {
192         if(version >= 11)
193         {
194                 compressZlib(data, os);
195                 return;
196         }
197
198         if(data.getSize() == 0)
199                 return;
200         
201         // Write length (u32)
202
203         u8 tmp[4];
204         writeU32(tmp, data.getSize());
205         os.write((char*)tmp, 4);
206         
207         // We will be writing 8-bit pairs of more_count and byte
208         u8 more_count = 0;
209         u8 current_byte = data[0];
210         for(u32 i=1; i<data.getSize(); i++)
211         {
212                 if(
213                         data[i] != current_byte
214                         || more_count == 255
215                 )
216                 {
217                         // write count and byte
218                         os.write((char*)&more_count, 1);
219                         os.write((char*)&current_byte, 1);
220                         more_count = 0;
221                         current_byte = data[i];
222                 }
223                 else
224                 {
225                         more_count++;
226                 }
227         }
228         // write count and byte
229         os.write((char*)&more_count, 1);
230         os.write((char*)&current_byte, 1);
231 }
232
233 void decompress(std::istream &is, std::ostream &os, u8 version)
234 {
235         if(version >= 11)
236         {
237                 decompressZlib(is, os);
238                 return;
239         }
240
241         // Read length (u32)
242
243         u8 tmp[4];
244         is.read((char*)tmp, 4);
245         u32 len = readU32(tmp);
246         
247         // We will be reading 8-bit pairs of more_count and byte
248         u32 count = 0;
249         for(;;)
250         {
251                 u8 more_count=0;
252                 u8 byte=0;
253
254                 is.read((char*)&more_count, 1);
255                 
256                 is.read((char*)&byte, 1);
257
258                 if(is.eof())
259                         throw SerializationError("decompress: stream ended halfway");
260
261                 for(s32 i=0; i<(u16)more_count+1; i++)
262                         os.write((char*)&byte, 1);
263
264                 count += (u16)more_count+1;
265
266                 if(count == len)
267                         break;
268         }
269 }
270
271