]> git.lizzy.rs Git - minetest.git/blob - src/serialization.cpp
commit before content-tile separation
[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 #include "zlib.h"
23
24 /* report a zlib or i/o error */
25 void zerr(int ret)
26 {   
27     fputs("zerr: ", stderr);
28     switch (ret) {
29     case Z_ERRNO:
30         if (ferror(stdin))
31             fputs("error reading stdin\n", stderr);
32         if (ferror(stdout))
33             fputs("error writing stdout\n", stderr);
34         break;
35     case Z_STREAM_ERROR:
36         fputs("invalid compression level\n", stderr);
37         break;
38     case Z_DATA_ERROR:
39         fputs("invalid or incomplete deflate data\n", stderr);
40         break;
41     case Z_MEM_ERROR:
42         fputs("out of memory\n", stderr);
43         break;
44     case Z_VERSION_ERROR:
45         fputs("zlib version mismatch!\n", stderr);
46                 break;
47         default:
48                 dstream<<"return value = "<<ret<<"\n";
49     }
50 }
51
52 void compressZlib(SharedBuffer<u8> data, std::ostream &os)
53 {
54         z_stream z;
55         const s32 bufsize = 16384;
56         //char input_buffer[bufsize];
57         char output_buffer[bufsize];
58         int input_i = 0;
59         int status = 0;
60         int ret;
61
62         z.zalloc = Z_NULL;
63         z.zfree = Z_NULL;
64         z.opaque = Z_NULL;
65
66         ret = deflateInit(&z, -1);
67         if(ret != Z_OK)
68                 throw SerializationError("compressZlib: deflateInit failed");
69         
70         z.avail_in = 0;
71         
72         for(;;)
73         {
74                 int flush = Z_NO_FLUSH;
75                 z.next_out = (Bytef*)output_buffer;
76                 z.avail_out = bufsize;
77
78                 if(z.avail_in == 0)
79                 {
80                         //z.next_in = (char*)&data[input_i];
81                         z.next_in = (Bytef*)&data[input_i];
82                         z.avail_in = data.getSize() - input_i;
83                         input_i += z.avail_in;
84                         if(input_i == (int)data.getSize())
85                                 flush = Z_FINISH;
86                 }
87                 if(z.avail_in == 0)
88                         break;
89                 status = deflate(&z, flush);
90                 if(status == Z_NEED_DICT || status == Z_DATA_ERROR
91                                 || status == Z_MEM_ERROR)
92                 {
93                         zerr(status);
94                         throw SerializationError("compressZlib: deflate failed");
95                 }
96                 int count = bufsize - z.avail_out;
97                 if(count)
98                         os.write(output_buffer, count);
99         }
100
101         deflateEnd(&z);
102
103 }
104
105 void decompressZlib(std::istream &is, std::ostream &os)
106 {
107         z_stream z;
108         const s32 bufsize = 16384;
109         char input_buffer[bufsize];
110         char output_buffer[bufsize];
111         int status = 0;
112         int ret;
113         int bytes_read = 0;
114         int input_buffer_len = 0;
115
116         z.zalloc = Z_NULL;
117         z.zfree = Z_NULL;
118         z.opaque = Z_NULL;
119
120         ret = inflateInit(&z);
121         if(ret != Z_OK)
122                 throw SerializationError("compressZlib: inflateInit failed");
123         
124         z.avail_in = 0;
125         
126         //dstream<<"initial fail="<<is.fail()<<" bad="<<is.bad()<<std::endl;
127
128         for(;;)
129         {
130                 z.next_out = (Bytef*)output_buffer;
131                 z.avail_out = bufsize;
132
133                 if(z.avail_in == 0)
134                 {
135                         z.next_in = (Bytef*)input_buffer;
136                         input_buffer_len = is.readsome(input_buffer, bufsize);
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("compressZlib: 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                         for(u32 i=0; i < z.avail_in; i++)
170                         {
171                                 is.unget();
172                                 if(is.fail() || is.bad())
173                                 {
174                                         dstream<<"unget #"<<i<<" failed"<<std::endl;
175                                         dstream<<"fail="<<is.fail()<<" bad="<<is.bad()<<std::endl;
176                                         throw SerializationError("compressZlib: unget failed");
177                                 }
178                         }
179                         
180                         break;
181                 }
182         }
183
184         inflateEnd(&z);
185 }
186
187 void compress(SharedBuffer<u8> data, std::ostream &os, u8 version)
188 {
189         if(version >= 11)
190         {
191                 compressZlib(data, os);
192                 return;
193         }
194
195         if(data.getSize() == 0)
196                 return;
197         
198         // Write length (u32)
199
200         u8 tmp[4];
201         writeU32(tmp, data.getSize());
202         os.write((char*)tmp, 4);
203         
204         // We will be writing 8-bit pairs of more_count and byte
205         u8 more_count = 0;
206         u8 current_byte = data[0];
207         for(u32 i=1; i<data.getSize(); i++)
208         {
209                 if(
210                         data[i] != current_byte
211                         || more_count == 255
212                 )
213                 {
214                         // write count and byte
215                         os.write((char*)&more_count, 1);
216                         os.write((char*)&current_byte, 1);
217                         more_count = 0;
218                         current_byte = data[i];
219                 }
220                 else
221                 {
222                         more_count++;
223                 }
224         }
225         // write count and byte
226         os.write((char*)&more_count, 1);
227         os.write((char*)&current_byte, 1);
228 }
229
230 void decompress(std::istream &is, std::ostream &os, u8 version)
231 {
232         if(version >= 11)
233         {
234                 decompressZlib(is, os);
235                 return;
236         }
237
238         // Read length (u32)
239
240         u8 tmp[4];
241         is.read((char*)tmp, 4);
242         u32 len = readU32(tmp);
243         
244         // We will be reading 8-bit pairs of more_count and byte
245         u32 count = 0;
246         for(;;)
247         {
248                 u8 more_count=0;
249                 u8 byte=0;
250
251                 is.read((char*)&more_count, 1);
252                 
253                 is.read((char*)&byte, 1);
254
255                 if(is.eof())
256                         throw SerializationError("decompress: stream ended halfway");
257
258                 for(s32 i=0; i<(u16)more_count+1; i++)
259                         os.write((char*)&byte, 1);
260
261                 count += (u16)more_count+1;
262
263                 if(count == len)
264                         break;
265         }
266 }
267
268