]> git.lizzy.rs Git - minetest.git/blob - src/util/serialize.h
Increase `ftos` precision (#13141)
[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 #pragma once
21
22 #include "irrlichttypes_bloated.h"
23 #include "exceptions.h" // for SerializationError
24 #include "debug.h" // for assert
25 #include "ieee_float.h"
26
27 #include "config.h"
28 #if HAVE_ENDIAN_H
29         #ifdef _WIN32
30                 #define __BYTE_ORDER 0
31                 #define __LITTLE_ENDIAN 0
32                 #define __BIG_ENDIAN 1
33         #elif defined(__MACH__) && defined(__APPLE__)
34                 #include <machine/endian.h>
35         #elif defined(__FreeBSD__) || defined(__DragonFly__)
36                 #include <sys/endian.h>
37         #else
38                 #include <endian.h>
39         #endif
40 #endif
41 #include <cstring> // for memcpy
42 #include <iostream>
43 #include <string>
44 #include <vector>
45
46 #define FIXEDPOINT_FACTOR 1000.0f
47
48 // 0x7FFFFFFF / 1000.0f is not serializable.
49 // The limited float precision at this magnitude may cause the result to round
50 // to a greater value than can be represented by a 32 bit integer when increased
51 // by a factor of FIXEDPOINT_FACTOR.  As a result, [F1000_MIN..F1000_MAX] does
52 // not represent the full range, but rather the largest safe range, of values on
53 // all supported architectures.  Note: This definition makes assumptions on
54 // platform float-to-int conversion behavior.
55 #define F1000_MIN ((float)(s32)((float)(-0x7FFFFFFF - 1) / FIXEDPOINT_FACTOR))
56 #define F1000_MAX ((float)(s32)((float)(0x7FFFFFFF) / FIXEDPOINT_FACTOR))
57
58 #define STRING_MAX_LEN 0xFFFF
59 #define WIDE_STRING_MAX_LEN 0xFFFF
60 // 64 MB ought to be enough for anybody - Billy G.
61 #define LONG_STRING_MAX_LEN (64 * 1024 * 1024)
62
63
64 extern FloatType g_serialize_f32_type;
65
66 #if HAVE_ENDIAN_H
67 // use machine native byte swapping routines
68 // Note: memcpy below is optimized out by modern compilers
69
70 inline u16 readU16(const u8 *data)
71 {
72         u16 val;
73         memcpy(&val, data, 2);
74         return be16toh(val);
75 }
76
77 inline u32 readU32(const u8 *data)
78 {
79         u32 val;
80         memcpy(&val, data, 4);
81         return be32toh(val);
82 }
83
84 inline u64 readU64(const u8 *data)
85 {
86         u64 val;
87         memcpy(&val, data, 8);
88         return be64toh(val);
89 }
90
91 inline void writeU16(u8 *data, u16 i)
92 {
93         u16 val = htobe16(i);
94         memcpy(data, &val, 2);
95 }
96
97 inline void writeU32(u8 *data, u32 i)
98 {
99         u32 val = htobe32(i);
100         memcpy(data, &val, 4);
101 }
102
103 inline void writeU64(u8 *data, u64 i)
104 {
105         u64 val = htobe64(i);
106         memcpy(data, &val, 8);
107 }
108
109 #else
110 // generic byte-swapping implementation
111
112 inline u16 readU16(const u8 *data)
113 {
114         return
115                 ((u16)data[0] << 8) | ((u16)data[1] << 0);
116 }
117
118 inline u32 readU32(const u8 *data)
119 {
120         return
121                 ((u32)data[0] << 24) | ((u32)data[1] << 16) |
122                 ((u32)data[2] <<  8) | ((u32)data[3] <<  0);
123 }
124
125 inline u64 readU64(const u8 *data)
126 {
127         return
128                 ((u64)data[0] << 56) | ((u64)data[1] << 48) |
129                 ((u64)data[2] << 40) | ((u64)data[3] << 32) |
130                 ((u64)data[4] << 24) | ((u64)data[5] << 16) |
131                 ((u64)data[6] <<  8) | ((u64)data[7] << 0);
132 }
133
134 inline void writeU16(u8 *data, u16 i)
135 {
136         data[0] = (i >> 8) & 0xFF;
137         data[1] = (i >> 0) & 0xFF;
138 }
139
140 inline void writeU32(u8 *data, u32 i)
141 {
142         data[0] = (i >> 24) & 0xFF;
143         data[1] = (i >> 16) & 0xFF;
144         data[2] = (i >>  8) & 0xFF;
145         data[3] = (i >>  0) & 0xFF;
146 }
147
148 inline void writeU64(u8 *data, u64 i)
149 {
150         data[0] = (i >> 56) & 0xFF;
151         data[1] = (i >> 48) & 0xFF;
152         data[2] = (i >> 40) & 0xFF;
153         data[3] = (i >> 32) & 0xFF;
154         data[4] = (i >> 24) & 0xFF;
155         data[5] = (i >> 16) & 0xFF;
156         data[6] = (i >>  8) & 0xFF;
157         data[7] = (i >>  0) & 0xFF;
158 }
159
160 #endif // HAVE_ENDIAN_H
161
162 //////////////// read routines ////////////////
163
164 inline u8 readU8(const u8 *data)
165 {
166         return ((u8)data[0] << 0);
167 }
168
169 inline s8 readS8(const u8 *data)
170 {
171         return (s8)readU8(data);
172 }
173
174 inline s16 readS16(const u8 *data)
175 {
176         return (s16)readU16(data);
177 }
178
179 inline s32 readS32(const u8 *data)
180 {
181         return (s32)readU32(data);
182 }
183
184 inline s64 readS64(const u8 *data)
185 {
186         return (s64)readU64(data);
187 }
188
189 inline f32 readF1000(const u8 *data)
190 {
191         return (f32)readS32(data) / FIXEDPOINT_FACTOR;
192 }
193
194 inline f32 readF32(const u8 *data)
195 {
196         u32 u = readU32(data);
197
198         switch (g_serialize_f32_type) {
199         case FLOATTYPE_SYSTEM: {
200                         f32 f;
201                         memcpy(&f, &u, 4);
202                         return f;
203                 }
204         case FLOATTYPE_SLOW:
205                 return u32Tof32Slow(u);
206         case FLOATTYPE_UNKNOWN: // First initialization
207                 g_serialize_f32_type = getFloatSerializationType();
208                 return readF32(data);
209         }
210         throw SerializationError("readF32: Unreachable code");
211 }
212
213 inline video::SColor readARGB8(const u8 *data)
214 {
215         video::SColor p(readU32(data));
216         return p;
217 }
218
219 inline v2s16 readV2S16(const u8 *data)
220 {
221         v2s16 p;
222         p.X = readS16(&data[0]);
223         p.Y = readS16(&data[2]);
224         return p;
225 }
226
227 inline v3s16 readV3S16(const u8 *data)
228 {
229         v3s16 p;
230         p.X = readS16(&data[0]);
231         p.Y = readS16(&data[2]);
232         p.Z = readS16(&data[4]);
233         return p;
234 }
235
236 inline v2s32 readV2S32(const u8 *data)
237 {
238         v2s32 p;
239         p.X = readS32(&data[0]);
240         p.Y = readS32(&data[4]);
241         return p;
242 }
243
244 inline v3s32 readV3S32(const u8 *data)
245 {
246         v3s32 p;
247         p.X = readS32(&data[0]);
248         p.Y = readS32(&data[4]);
249         p.Z = readS32(&data[8]);
250         return p;
251 }
252
253 inline v3f readV3F1000(const u8 *data)
254 {
255         v3f p;
256         p.X = readF1000(&data[0]);
257         p.Y = readF1000(&data[4]);
258         p.Z = readF1000(&data[8]);
259         return p;
260 }
261
262 inline v2f readV2F32(const u8 *data)
263 {
264         v2f p;
265         p.X = readF32(&data[0]);
266         p.Y = readF32(&data[4]);
267         return p;
268 }
269
270 inline v3f readV3F32(const u8 *data)
271 {
272         v3f p;
273         p.X = readF32(&data[0]);
274         p.Y = readF32(&data[4]);
275         p.Z = readF32(&data[8]);
276         return p;
277 }
278
279 /////////////// write routines ////////////////
280
281 inline void writeU8(u8 *data, u8 i)
282 {
283         data[0] = (i >> 0) & 0xFF;
284 }
285
286 inline void writeS8(u8 *data, s8 i)
287 {
288         writeU8(data, (u8)i);
289 }
290
291 inline void writeS16(u8 *data, s16 i)
292 {
293         writeU16(data, (u16)i);
294 }
295
296 inline void writeS32(u8 *data, s32 i)
297 {
298         writeU32(data, (u32)i);
299 }
300
301 inline void writeS64(u8 *data, s64 i)
302 {
303         writeU64(data, (u64)i);
304 }
305
306 inline void writeF1000(u8 *data, f32 i)
307 {
308         assert(i >= F1000_MIN && i <= F1000_MAX);
309         writeS32(data, i * FIXEDPOINT_FACTOR);
310 }
311
312 inline void writeF32(u8 *data, f32 i)
313 {
314         switch (g_serialize_f32_type) {
315         case FLOATTYPE_SYSTEM: {
316                         u32 u;
317                         memcpy(&u, &i, 4);
318                         return writeU32(data, u);
319                 }
320         case FLOATTYPE_SLOW:
321                 return writeU32(data, f32Tou32Slow(i));
322         case FLOATTYPE_UNKNOWN: // First initialization
323                 g_serialize_f32_type = getFloatSerializationType();
324                 return writeF32(data, i);
325         }
326         throw SerializationError("writeF32: Unreachable code");
327 }
328
329 inline void writeARGB8(u8 *data, video::SColor p)
330 {
331         writeU32(data, p.color);
332 }
333
334 inline void writeV2S16(u8 *data, v2s16 p)
335 {
336         writeS16(&data[0], p.X);
337         writeS16(&data[2], p.Y);
338 }
339
340 inline void writeV3S16(u8 *data, v3s16 p)
341 {
342         writeS16(&data[0], p.X);
343         writeS16(&data[2], p.Y);
344         writeS16(&data[4], p.Z);
345 }
346
347 inline void writeV2S32(u8 *data, v2s32 p)
348 {
349         writeS32(&data[0], p.X);
350         writeS32(&data[4], p.Y);
351 }
352
353 inline void writeV3S32(u8 *data, v3s32 p)
354 {
355         writeS32(&data[0], p.X);
356         writeS32(&data[4], p.Y);
357         writeS32(&data[8], p.Z);
358 }
359
360 inline void writeV3F1000(u8 *data, v3f p)
361 {
362         writeF1000(&data[0], p.X);
363         writeF1000(&data[4], p.Y);
364         writeF1000(&data[8], p.Z);
365 }
366
367 inline void writeV2F32(u8 *data, v2f p)
368 {
369         writeF32(&data[0], p.X);
370         writeF32(&data[4], p.Y);
371 }
372
373 inline void writeV3F32(u8 *data, v3f p)
374 {
375         writeF32(&data[0], p.X);
376         writeF32(&data[4], p.Y);
377         writeF32(&data[8], p.Z);
378 }
379
380 ////
381 //// Iostream wrapper for data read/write
382 ////
383
384 #define MAKE_STREAM_READ_FXN(T, N, S)    \
385         inline T read ## N(std::istream &is) \
386         {                                    \
387                 char buf[S] = {0};               \
388                 is.read(buf, sizeof(buf));       \
389                 return read ## N((u8 *)buf);     \
390         }
391
392 #define MAKE_STREAM_WRITE_FXN(T, N, S)              \
393         inline void write ## N(std::ostream &os, T val) \
394         {                                               \
395                 char buf[S];                                \
396                 write ## N((u8 *)buf, val);                 \
397                 os.write(buf, sizeof(buf));                 \
398         }
399
400 MAKE_STREAM_READ_FXN(u8,    U8,       1);
401 MAKE_STREAM_READ_FXN(u16,   U16,      2);
402 MAKE_STREAM_READ_FXN(u32,   U32,      4);
403 MAKE_STREAM_READ_FXN(u64,   U64,      8);
404 MAKE_STREAM_READ_FXN(s8,    S8,       1);
405 MAKE_STREAM_READ_FXN(s16,   S16,      2);
406 MAKE_STREAM_READ_FXN(s32,   S32,      4);
407 MAKE_STREAM_READ_FXN(s64,   S64,      8);
408 MAKE_STREAM_READ_FXN(f32,   F1000,    4);
409 MAKE_STREAM_READ_FXN(f32,   F32,      4);
410 MAKE_STREAM_READ_FXN(v2s16, V2S16,    4);
411 MAKE_STREAM_READ_FXN(v3s16, V3S16,    6);
412 MAKE_STREAM_READ_FXN(v2s32, V2S32,    8);
413 MAKE_STREAM_READ_FXN(v3s32, V3S32,   12);
414 MAKE_STREAM_READ_FXN(v3f,   V3F1000, 12);
415 MAKE_STREAM_READ_FXN(v2f,   V2F32,    8);
416 MAKE_STREAM_READ_FXN(v3f,   V3F32,   12);
417 MAKE_STREAM_READ_FXN(video::SColor, ARGB8, 4);
418
419 MAKE_STREAM_WRITE_FXN(u8,    U8,       1);
420 MAKE_STREAM_WRITE_FXN(u16,   U16,      2);
421 MAKE_STREAM_WRITE_FXN(u32,   U32,      4);
422 MAKE_STREAM_WRITE_FXN(u64,   U64,      8);
423 MAKE_STREAM_WRITE_FXN(s8,    S8,       1);
424 MAKE_STREAM_WRITE_FXN(s16,   S16,      2);
425 MAKE_STREAM_WRITE_FXN(s32,   S32,      4);
426 MAKE_STREAM_WRITE_FXN(s64,   S64,      8);
427 MAKE_STREAM_WRITE_FXN(f32,   F1000,    4);
428 MAKE_STREAM_WRITE_FXN(f32,   F32,      4);
429 MAKE_STREAM_WRITE_FXN(v2s16, V2S16,    4);
430 MAKE_STREAM_WRITE_FXN(v3s16, V3S16,    6);
431 MAKE_STREAM_WRITE_FXN(v2s32, V2S32,    8);
432 MAKE_STREAM_WRITE_FXN(v3s32, V3S32,   12);
433 MAKE_STREAM_WRITE_FXN(v3f,   V3F1000, 12);
434 MAKE_STREAM_WRITE_FXN(v2f,   V2F32,    8);
435 MAKE_STREAM_WRITE_FXN(v3f,   V3F32,   12);
436 MAKE_STREAM_WRITE_FXN(video::SColor, ARGB8, 4);
437
438 ////
439 //// More serialization stuff
440 ////
441
442 inline float clampToF1000(float v)
443 {
444         return core::clamp(v, F1000_MIN, F1000_MAX);
445 }
446
447 inline v3f clampToF1000(v3f v)
448 {
449         return {clampToF1000(v.X), clampToF1000(v.Y), clampToF1000(v.Z)};
450 }
451
452 // Creates a string with the length as the first two bytes
453 std::string serializeString16(const std::string &plain);
454
455 // Reads a string with the length as the first two bytes
456 std::string deSerializeString16(std::istream &is);
457
458 // Creates a string with the length as the first four bytes
459 std::string serializeString32(const std::string &plain);
460
461 // Reads a string with the length as the first four bytes
462 std::string deSerializeString32(std::istream &is);
463
464 // Creates a string encoded in JSON format (almost equivalent to a C string literal)
465 std::string serializeJsonString(const std::string &plain);
466
467 // Reads a string encoded in JSON format
468 std::string deSerializeJsonString(std::istream &is);
469
470 // If the string contains spaces, quotes or control characters, encodes as JSON.
471 // Else returns the string unmodified.
472 std::string serializeJsonStringIfNeeded(const std::string &s);
473
474 // Parses a string serialized by serializeJsonStringIfNeeded.
475 std::string deSerializeJsonStringIfNeeded(std::istream &is);