]> git.lizzy.rs Git - minetest.git/blob - src/util/serialize.h
Fix segfaults caused by the Environment not being initialized yet
[minetest.git] / src / util / serialize.h
1 /*
2 Minetest
3 Copyright (C) 2010-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 #ifndef UTIL_SERIALIZE_HEADER
21 #define UTIL_SERIALIZE_HEADER
22
23 #include "../irrlichttypes_bloated.h"
24 #include "../debug.h" // for assert
25 #include "config.h"
26 #if HAVE_ENDIAN_H
27 #include <endian.h>
28 #include <string.h> // for memcpy
29 #endif
30 #include <iostream>
31 #include <string>
32
33 #define FIXEDPOINT_FACTOR 1000.0f
34
35 // 0x7FFFFFFF / 1000.0f is not serializable.
36 // The limited float precision at this magnitude may cause the result to round
37 // to a greater value than can be represented by a 32 bit integer when increased
38 // by a factor of FIXEDPOINT_FACTOR.  As a result, [F1000_MIN..F1000_MAX] does
39 // not represent the full range, but rather the largest safe range, of values on
40 // all supported architectures.  Note: This definition makes assumptions on
41 // platform float-to-int conversion behavior.
42 #define F1000_MIN ((float)(s32)((-0x7FFFFFFF - 1) / FIXEDPOINT_FACTOR))
43 #define F1000_MAX ((float)(s32)((0x7FFFFFFF) / FIXEDPOINT_FACTOR))
44
45 #define STRING_MAX_LEN 0xFFFF
46 #define WIDE_STRING_MAX_LEN 0xFFFF
47 // 64 MB ought to be enough for anybody - Billy G.
48 #define LONG_STRING_MAX_LEN (64 * 1024 * 1024)
49
50
51 #if HAVE_ENDIAN_H
52 // use machine native byte swapping routines
53 // Note: memcpy below is optimized out by modern compilers
54
55 inline u16 readU16(const u8 *data)
56 {
57         u16 val;
58         memcpy(&val, data, 2);
59         return be16toh(val);
60 }
61
62 inline u32 readU32(const u8 *data)
63 {
64         u32 val;
65         memcpy(&val, data, 4);
66         return be32toh(val);
67 }
68
69 inline u64 readU64(const u8 *data)
70 {
71         u64 val;
72         memcpy(&val, data, 8);
73         return be64toh(val);
74 }
75
76 inline void writeU16(u8 *data, u16 i)
77 {
78         u16 val = htobe16(i);
79         memcpy(data, &val, 2);
80 }
81
82 inline void writeU32(u8 *data, u32 i)
83 {
84         u32 val = htobe32(i);
85         memcpy(data, &val, 4);
86 }
87
88 inline void writeU64(u8 *data, u64 i)
89 {
90         u64 val = htobe64(i);
91         memcpy(data, &val, 8);
92 }
93
94 #else
95 // generic byte-swapping implementation
96
97 inline u16 readU16(const u8 *data)
98 {
99         return
100                 ((u16)data[0] << 8) | ((u16)data[1] << 0);
101 }
102
103 inline u32 readU32(const u8 *data)
104 {
105         return
106                 ((u32)data[0] << 24) | ((u32)data[1] << 16) |
107                 ((u32)data[2] <<  8) | ((u32)data[3] <<  0);
108 }
109
110 inline u64 readU64(const u8 *data)
111 {
112         return
113                 ((u64)data[0] << 56) | ((u64)data[1] << 48) |
114                 ((u64)data[2] << 40) | ((u64)data[3] << 32) |
115                 ((u64)data[4] << 24) | ((u64)data[5] << 16) |
116                 ((u64)data[6] <<  8) | ((u64)data[7] << 0);
117 }
118
119 inline void writeU16(u8 *data, u16 i)
120 {
121         data[0] = (i >> 8) & 0xFF;
122         data[1] = (i >> 0) & 0xFF;
123 }
124
125 inline void writeU32(u8 *data, u32 i)
126 {
127         data[0] = (i >> 24) & 0xFF;
128         data[1] = (i >> 16) & 0xFF;
129         data[2] = (i >>  8) & 0xFF;
130         data[3] = (i >>  0) & 0xFF;
131 }
132
133 inline void writeU64(u8 *data, u64 i)
134 {
135         data[0] = (i >> 56) & 0xFF;
136         data[1] = (i >> 48) & 0xFF;
137         data[2] = (i >> 40) & 0xFF;
138         data[3] = (i >> 32) & 0xFF;
139         data[4] = (i >> 24) & 0xFF;
140         data[5] = (i >> 16) & 0xFF;
141         data[6] = (i >>  8) & 0xFF;
142         data[7] = (i >>  0) & 0xFF;
143 }
144
145 #endif // HAVE_ENDIAN_H
146
147 //////////////// read routines ////////////////
148
149 inline u8 readU8(const u8 *data)
150 {
151         return ((u8)data[0] << 0);
152 }
153
154 inline s8 readS8(const u8 *data)
155 {
156         return (s8)readU8(data);
157 }
158
159 inline s16 readS16(const u8 *data)
160 {
161         return (s16)readU16(data);
162 }
163
164 inline s32 readS32(const u8 *data)
165 {
166         return (s32)readU32(data);
167 }
168
169 inline s64 readS64(const u8 *data)
170 {
171         return (s64)readU64(data);
172 }
173
174 inline f32 readF1000(const u8 *data)
175 {
176         return (f32)readS32(data) / FIXEDPOINT_FACTOR;
177 }
178
179 inline video::SColor readARGB8(const u8 *data)
180 {
181         video::SColor p(readU32(data));
182         return p;
183 }
184
185 inline v2s16 readV2S16(const u8 *data)
186 {
187         v2s16 p;
188         p.X = readS16(&data[0]);
189         p.Y = readS16(&data[2]);
190         return p;
191 }
192
193 inline v3s16 readV3S16(const u8 *data)
194 {
195         v3s16 p;
196         p.X = readS16(&data[0]);
197         p.Y = readS16(&data[2]);
198         p.Z = readS16(&data[4]);
199         return p;
200 }
201
202 inline v2s32 readV2S32(const u8 *data)
203 {
204         v2s32 p;
205         p.X = readS32(&data[0]);
206         p.Y = readS32(&data[4]);
207         return p;
208 }
209
210 inline v3s32 readV3S32(const u8 *data)
211 {
212         v3s32 p;
213         p.X = readS32(&data[0]);
214         p.Y = readS32(&data[4]);
215         p.Z = readS32(&data[8]);
216         return p;
217 }
218
219 inline v2f readV2F1000(const u8 *data)
220 {
221         v2f p;
222         p.X = (float)readF1000(&data[0]);
223         p.Y = (float)readF1000(&data[4]);
224         return p;
225 }
226
227 inline v3f readV3F1000(const u8 *data)
228 {
229         v3f p;
230         p.X = (float)readF1000(&data[0]);
231         p.Y = (float)readF1000(&data[4]);
232         p.Z = (float)readF1000(&data[8]);
233         return p;
234 }
235
236 /////////////// write routines ////////////////
237
238 inline void writeU8(u8 *data, u8 i)
239 {
240         data[0] = (i >> 0) & 0xFF;
241 }
242
243 inline void writeS8(u8 *data, s8 i)
244 {
245         writeU8(data, (u8)i);
246 }
247
248 inline void writeS16(u8 *data, s16 i)
249 {
250         writeU16(data, (u16)i);
251 }
252
253 inline void writeS32(u8 *data, s32 i)
254 {
255         writeU32(data, (u32)i);
256 }
257
258 inline void writeS64(u8 *data, s64 i)
259 {
260         writeU64(data, (u64)i);
261 }
262
263 inline void writeF1000(u8 *data, f32 i)
264 {
265         assert(i >= F1000_MIN && i <= F1000_MAX);
266         writeS32(data, i * FIXEDPOINT_FACTOR);
267 }
268
269 inline void writeARGB8(u8 *data, video::SColor p)
270 {
271         writeU32(data, p.color);
272 }
273
274 inline void writeV2S16(u8 *data, v2s16 p)
275 {
276         writeS16(&data[0], p.X);
277         writeS16(&data[2], p.Y);
278 }
279
280 inline void writeV3S16(u8 *data, v3s16 p)
281 {
282         writeS16(&data[0], p.X);
283         writeS16(&data[2], p.Y);
284         writeS16(&data[4], p.Z);
285 }
286
287 inline void writeV2S32(u8 *data, v2s32 p)
288 {
289         writeS32(&data[0], p.X);
290         writeS32(&data[4], p.Y);
291 }
292
293 inline void writeV3S32(u8 *data, v3s32 p)
294 {
295         writeS32(&data[0], p.X);
296         writeS32(&data[4], p.Y);
297         writeS32(&data[8], p.Z);
298 }
299
300 inline void writeV2F1000(u8 *data, v2f p)
301 {
302         writeF1000(&data[0], p.X);
303         writeF1000(&data[4], p.Y);
304 }
305
306 inline void writeV3F1000(u8 *data, v3f p)
307 {
308         writeF1000(&data[0], p.X);
309         writeF1000(&data[4], p.Y);
310         writeF1000(&data[8], p.Z);
311 }
312
313 ////
314 //// Iostream wrapper for data read/write
315 ////
316
317 #define MAKE_STREAM_READ_FXN(T, N, S)    \
318         inline T read ## N(std::istream &is) \
319         {                                    \
320                 char buf[S] = {0};               \
321                 is.read(buf, sizeof(buf));       \
322                 return read ## N((u8 *)buf);     \
323         }
324
325 #define MAKE_STREAM_WRITE_FXN(T, N, S)              \
326         inline void write ## N(std::ostream &os, T val) \
327         {                                               \
328                 char buf[S];                                \
329                 write ## N((u8 *)buf, val);                 \
330                 os.write(buf, sizeof(buf));                 \
331         }
332
333 MAKE_STREAM_READ_FXN(u8,    U8,       1);
334 MAKE_STREAM_READ_FXN(u16,   U16,      2);
335 MAKE_STREAM_READ_FXN(u32,   U32,      4);
336 MAKE_STREAM_READ_FXN(u64,   U64,      8);
337 MAKE_STREAM_READ_FXN(s8,    S8,       1);
338 MAKE_STREAM_READ_FXN(s16,   S16,      2);
339 MAKE_STREAM_READ_FXN(s32,   S32,      4);
340 MAKE_STREAM_READ_FXN(s64,   S64,      8);
341 MAKE_STREAM_READ_FXN(f32,   F1000,    4);
342 MAKE_STREAM_READ_FXN(v2s16, V2S16,    4);
343 MAKE_STREAM_READ_FXN(v3s16, V3S16,    6);
344 MAKE_STREAM_READ_FXN(v2s32, V2S32,    8);
345 MAKE_STREAM_READ_FXN(v3s32, V3S32,   12);
346 MAKE_STREAM_READ_FXN(v2f,   V2F1000,  8);
347 MAKE_STREAM_READ_FXN(v3f,   V3F1000, 12);
348 MAKE_STREAM_READ_FXN(video::SColor, ARGB8, 4);
349
350 MAKE_STREAM_WRITE_FXN(u8,    U8,       1);
351 MAKE_STREAM_WRITE_FXN(u16,   U16,      2);
352 MAKE_STREAM_WRITE_FXN(u32,   U32,      4);
353 MAKE_STREAM_WRITE_FXN(u64,   U64,      8);
354 MAKE_STREAM_WRITE_FXN(s8,    S8,       1);
355 MAKE_STREAM_WRITE_FXN(s16,   S16,      2);
356 MAKE_STREAM_WRITE_FXN(s32,   S32,      4);
357 MAKE_STREAM_WRITE_FXN(s64,   S64,      8);
358 MAKE_STREAM_WRITE_FXN(f32,   F1000,    4);
359 MAKE_STREAM_WRITE_FXN(v2s16, V2S16,    4);
360 MAKE_STREAM_WRITE_FXN(v3s16, V3S16,    6);
361 MAKE_STREAM_WRITE_FXN(v2s32, V2S32,    8);
362 MAKE_STREAM_WRITE_FXN(v3s32, V3S32,   12);
363 MAKE_STREAM_WRITE_FXN(v2f,   V2F1000,  8);
364 MAKE_STREAM_WRITE_FXN(v3f,   V3F1000, 12);
365 MAKE_STREAM_WRITE_FXN(video::SColor, ARGB8, 4);
366
367 ////
368 //// More serialization stuff
369 ////
370
371 // Creates a string with the length as the first two bytes
372 std::string serializeString(const std::string &plain);
373
374 // Creates a string with the length as the first two bytes from wide string
375 std::string serializeWideString(const std::wstring &plain);
376
377 // Reads a string with the length as the first two bytes
378 std::string deSerializeString(std::istream &is);
379
380 // Reads a wide string with the length as the first two bytes
381 std::wstring deSerializeWideString(std::istream &is);
382
383 // Creates a string with the length as the first four bytes
384 std::string serializeLongString(const std::string &plain);
385
386 // Reads a string with the length as the first four bytes
387 std::string deSerializeLongString(std::istream &is);
388
389 // Creates a string encoded in JSON format (almost equivalent to a C string literal)
390 std::string serializeJsonString(const std::string &plain);
391
392 // Reads a string encoded in JSON format
393 std::string deSerializeJsonString(std::istream &is);
394
395 // Creates a string consisting of the hexadecimal representation of `data`
396 std::string serializeHexString(const std::string &data, bool insert_spaces=false);
397
398 // Creates a string containing comma delimited values of a struct whose layout is
399 // described by the parameter format
400 bool serializeStructToString(std::string *out,
401         std::string format, void *value);
402
403 // Reads a comma delimited string of values into a struct whose layout is
404 // decribed by the parameter format
405 bool deSerializeStringToStruct(std::string valstr,
406         std::string format, void *out, size_t olen);
407
408 #endif