]> git.lizzy.rs Git - dragonfireclient.git/blob - src/util/serialize.h
Add macos/freebsd missing endian.h include and add win endianness info
[dragonfireclient.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 "../exceptions.h" // for SerializationError
25 #include "../debug.h" // for assert
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__)
36                 #include <sys/endian.h>
37         #else
38                 #include <endian.h>
39         #endif
40 #endif
41 #include <string.h> // 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)((-0x7FFFFFFF - 1) / FIXEDPOINT_FACTOR))
56 #define F1000_MAX ((float)(s32)((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 #if HAVE_ENDIAN_H
65 // use machine native byte swapping routines
66 // Note: memcpy below is optimized out by modern compilers
67
68 inline u16 readU16(const u8 *data)
69 {
70         u16 val;
71         memcpy(&val, data, 2);
72         return be16toh(val);
73 }
74
75 inline u32 readU32(const u8 *data)
76 {
77         u32 val;
78         memcpy(&val, data, 4);
79         return be32toh(val);
80 }
81
82 inline u64 readU64(const u8 *data)
83 {
84         u64 val;
85         memcpy(&val, data, 8);
86         return be64toh(val);
87 }
88
89 inline void writeU16(u8 *data, u16 i)
90 {
91         u16 val = htobe16(i);
92         memcpy(data, &val, 2);
93 }
94
95 inline void writeU32(u8 *data, u32 i)
96 {
97         u32 val = htobe32(i);
98         memcpy(data, &val, 4);
99 }
100
101 inline void writeU64(u8 *data, u64 i)
102 {
103         u64 val = htobe64(i);
104         memcpy(data, &val, 8);
105 }
106
107 #else
108 // generic byte-swapping implementation
109
110 inline u16 readU16(const u8 *data)
111 {
112         return
113                 ((u16)data[0] << 8) | ((u16)data[1] << 0);
114 }
115
116 inline u32 readU32(const u8 *data)
117 {
118         return
119                 ((u32)data[0] << 24) | ((u32)data[1] << 16) |
120                 ((u32)data[2] <<  8) | ((u32)data[3] <<  0);
121 }
122
123 inline u64 readU64(const u8 *data)
124 {
125         return
126                 ((u64)data[0] << 56) | ((u64)data[1] << 48) |
127                 ((u64)data[2] << 40) | ((u64)data[3] << 32) |
128                 ((u64)data[4] << 24) | ((u64)data[5] << 16) |
129                 ((u64)data[6] <<  8) | ((u64)data[7] << 0);
130 }
131
132 inline void writeU16(u8 *data, u16 i)
133 {
134         data[0] = (i >> 8) & 0xFF;
135         data[1] = (i >> 0) & 0xFF;
136 }
137
138 inline void writeU32(u8 *data, u32 i)
139 {
140         data[0] = (i >> 24) & 0xFF;
141         data[1] = (i >> 16) & 0xFF;
142         data[2] = (i >>  8) & 0xFF;
143         data[3] = (i >>  0) & 0xFF;
144 }
145
146 inline void writeU64(u8 *data, u64 i)
147 {
148         data[0] = (i >> 56) & 0xFF;
149         data[1] = (i >> 48) & 0xFF;
150         data[2] = (i >> 40) & 0xFF;
151         data[3] = (i >> 32) & 0xFF;
152         data[4] = (i >> 24) & 0xFF;
153         data[5] = (i >> 16) & 0xFF;
154         data[6] = (i >>  8) & 0xFF;
155         data[7] = (i >>  0) & 0xFF;
156 }
157
158 #endif // HAVE_ENDIAN_H
159
160 //////////////// read routines ////////////////
161
162 inline u8 readU8(const u8 *data)
163 {
164         return ((u8)data[0] << 0);
165 }
166
167 inline s8 readS8(const u8 *data)
168 {
169         return (s8)readU8(data);
170 }
171
172 inline s16 readS16(const u8 *data)
173 {
174         return (s16)readU16(data);
175 }
176
177 inline s32 readS32(const u8 *data)
178 {
179         return (s32)readU32(data);
180 }
181
182 inline s64 readS64(const u8 *data)
183 {
184         return (s64)readU64(data);
185 }
186
187 inline f32 readF1000(const u8 *data)
188 {
189         return (f32)readS32(data) / FIXEDPOINT_FACTOR;
190 }
191
192 inline video::SColor readARGB8(const u8 *data)
193 {
194         video::SColor p(readU32(data));
195         return p;
196 }
197
198 inline v2s16 readV2S16(const u8 *data)
199 {
200         v2s16 p;
201         p.X = readS16(&data[0]);
202         p.Y = readS16(&data[2]);
203         return p;
204 }
205
206 inline v3s16 readV3S16(const u8 *data)
207 {
208         v3s16 p;
209         p.X = readS16(&data[0]);
210         p.Y = readS16(&data[2]);
211         p.Z = readS16(&data[4]);
212         return p;
213 }
214
215 inline v2s32 readV2S32(const u8 *data)
216 {
217         v2s32 p;
218         p.X = readS32(&data[0]);
219         p.Y = readS32(&data[4]);
220         return p;
221 }
222
223 inline v3s32 readV3S32(const u8 *data)
224 {
225         v3s32 p;
226         p.X = readS32(&data[0]);
227         p.Y = readS32(&data[4]);
228         p.Z = readS32(&data[8]);
229         return p;
230 }
231
232 inline v2f readV2F1000(const u8 *data)
233 {
234         v2f p;
235         p.X = (float)readF1000(&data[0]);
236         p.Y = (float)readF1000(&data[4]);
237         return p;
238 }
239
240 inline v3f readV3F1000(const u8 *data)
241 {
242         v3f p;
243         p.X = (float)readF1000(&data[0]);
244         p.Y = (float)readF1000(&data[4]);
245         p.Z = (float)readF1000(&data[8]);
246         return p;
247 }
248
249 /////////////// write routines ////////////////
250
251 inline void writeU8(u8 *data, u8 i)
252 {
253         data[0] = (i >> 0) & 0xFF;
254 }
255
256 inline void writeS8(u8 *data, s8 i)
257 {
258         writeU8(data, (u8)i);
259 }
260
261 inline void writeS16(u8 *data, s16 i)
262 {
263         writeU16(data, (u16)i);
264 }
265
266 inline void writeS32(u8 *data, s32 i)
267 {
268         writeU32(data, (u32)i);
269 }
270
271 inline void writeS64(u8 *data, s64 i)
272 {
273         writeU64(data, (u64)i);
274 }
275
276 inline void writeF1000(u8 *data, f32 i)
277 {
278         assert(i >= F1000_MIN && i <= F1000_MAX);
279         writeS32(data, i * FIXEDPOINT_FACTOR);
280 }
281
282 inline void writeARGB8(u8 *data, video::SColor p)
283 {
284         writeU32(data, p.color);
285 }
286
287 inline void writeV2S16(u8 *data, v2s16 p)
288 {
289         writeS16(&data[0], p.X);
290         writeS16(&data[2], p.Y);
291 }
292
293 inline void writeV3S16(u8 *data, v3s16 p)
294 {
295         writeS16(&data[0], p.X);
296         writeS16(&data[2], p.Y);
297         writeS16(&data[4], p.Z);
298 }
299
300 inline void writeV2S32(u8 *data, v2s32 p)
301 {
302         writeS32(&data[0], p.X);
303         writeS32(&data[4], p.Y);
304 }
305
306 inline void writeV3S32(u8 *data, v3s32 p)
307 {
308         writeS32(&data[0], p.X);
309         writeS32(&data[4], p.Y);
310         writeS32(&data[8], p.Z);
311 }
312
313 inline void writeV2F1000(u8 *data, v2f p)
314 {
315         writeF1000(&data[0], p.X);
316         writeF1000(&data[4], p.Y);
317 }
318
319 inline void writeV3F1000(u8 *data, v3f p)
320 {
321         writeF1000(&data[0], p.X);
322         writeF1000(&data[4], p.Y);
323         writeF1000(&data[8], p.Z);
324 }
325
326 ////
327 //// Iostream wrapper for data read/write
328 ////
329
330 #define MAKE_STREAM_READ_FXN(T, N, S)    \
331         inline T read ## N(std::istream &is) \
332         {                                    \
333                 char buf[S] = {0};               \
334                 is.read(buf, sizeof(buf));       \
335                 return read ## N((u8 *)buf);     \
336         }
337
338 #define MAKE_STREAM_WRITE_FXN(T, N, S)              \
339         inline void write ## N(std::ostream &os, T val) \
340         {                                               \
341                 char buf[S];                                \
342                 write ## N((u8 *)buf, val);                 \
343                 os.write(buf, sizeof(buf));                 \
344         }
345
346 MAKE_STREAM_READ_FXN(u8,    U8,       1);
347 MAKE_STREAM_READ_FXN(u16,   U16,      2);
348 MAKE_STREAM_READ_FXN(u32,   U32,      4);
349 MAKE_STREAM_READ_FXN(u64,   U64,      8);
350 MAKE_STREAM_READ_FXN(s8,    S8,       1);
351 MAKE_STREAM_READ_FXN(s16,   S16,      2);
352 MAKE_STREAM_READ_FXN(s32,   S32,      4);
353 MAKE_STREAM_READ_FXN(s64,   S64,      8);
354 MAKE_STREAM_READ_FXN(f32,   F1000,    4);
355 MAKE_STREAM_READ_FXN(v2s16, V2S16,    4);
356 MAKE_STREAM_READ_FXN(v3s16, V3S16,    6);
357 MAKE_STREAM_READ_FXN(v2s32, V2S32,    8);
358 MAKE_STREAM_READ_FXN(v3s32, V3S32,   12);
359 MAKE_STREAM_READ_FXN(v2f,   V2F1000,  8);
360 MAKE_STREAM_READ_FXN(v3f,   V3F1000, 12);
361 MAKE_STREAM_READ_FXN(video::SColor, ARGB8, 4);
362
363 MAKE_STREAM_WRITE_FXN(u8,    U8,       1);
364 MAKE_STREAM_WRITE_FXN(u16,   U16,      2);
365 MAKE_STREAM_WRITE_FXN(u32,   U32,      4);
366 MAKE_STREAM_WRITE_FXN(u64,   U64,      8);
367 MAKE_STREAM_WRITE_FXN(s8,    S8,       1);
368 MAKE_STREAM_WRITE_FXN(s16,   S16,      2);
369 MAKE_STREAM_WRITE_FXN(s32,   S32,      4);
370 MAKE_STREAM_WRITE_FXN(s64,   S64,      8);
371 MAKE_STREAM_WRITE_FXN(f32,   F1000,    4);
372 MAKE_STREAM_WRITE_FXN(v2s16, V2S16,    4);
373 MAKE_STREAM_WRITE_FXN(v3s16, V3S16,    6);
374 MAKE_STREAM_WRITE_FXN(v2s32, V2S32,    8);
375 MAKE_STREAM_WRITE_FXN(v3s32, V3S32,   12);
376 MAKE_STREAM_WRITE_FXN(v2f,   V2F1000,  8);
377 MAKE_STREAM_WRITE_FXN(v3f,   V3F1000, 12);
378 MAKE_STREAM_WRITE_FXN(video::SColor, ARGB8, 4);
379
380 ////
381 //// More serialization stuff
382 ////
383
384 // Creates a string with the length as the first two bytes
385 std::string serializeString(const std::string &plain);
386
387 // Creates a string with the length as the first two bytes from wide string
388 std::string serializeWideString(const std::wstring &plain);
389
390 // Reads a string with the length as the first two bytes
391 std::string deSerializeString(std::istream &is);
392
393 // Reads a wide string with the length as the first two bytes
394 std::wstring deSerializeWideString(std::istream &is);
395
396 // Creates a string with the length as the first four bytes
397 std::string serializeLongString(const std::string &plain);
398
399 // Reads a string with the length as the first four bytes
400 std::string deSerializeLongString(std::istream &is);
401
402 // Creates a string encoded in JSON format (almost equivalent to a C string literal)
403 std::string serializeJsonString(const std::string &plain);
404
405 // Reads a string encoded in JSON format
406 std::string deSerializeJsonString(std::istream &is);
407
408 // Creates a string consisting of the hexadecimal representation of `data`
409 std::string serializeHexString(const std::string &data, bool insert_spaces=false);
410
411 // Creates a string containing comma delimited values of a struct whose layout is
412 // described by the parameter format
413 bool serializeStructToString(std::string *out,
414         std::string format, void *value);
415
416 // Reads a comma delimited string of values into a struct whose layout is
417 // decribed by the parameter format
418 bool deSerializeStringToStruct(std::string valstr,
419         std::string format, void *out, size_t olen);
420
421 ////
422 //// BufReader
423 ////
424
425 extern SerializationError eof_ser_err;
426
427 #define MAKE_BUFREADER_GETNOEX_FXN(T, N, S) \
428         inline bool get ## N ## NoEx(T *val)    \
429         {                                       \
430                 if (pos + S > size)                 \
431                         return false;                   \
432                 *val = read ## N(data + pos);       \
433                 pos += S;                           \
434                 return true;                        \
435         }
436
437 #define MAKE_BUFREADER_GET_FXN(T, N) \
438         inline T get ## N()              \
439         {                                \
440                 T val;                       \
441                 if (!get ## N ## NoEx(&val)) \
442                         throw eof_ser_err;       \
443                 return val;                  \
444         }
445
446 class BufReader {
447 public:
448         BufReader(const u8 *data_, size_t size_) :
449                 data(data_),
450                 size(size_),
451                 pos(0)
452         {
453         }
454
455         MAKE_BUFREADER_GETNOEX_FXN(u8,    U8,       1);
456         MAKE_BUFREADER_GETNOEX_FXN(u16,   U16,      2);
457         MAKE_BUFREADER_GETNOEX_FXN(u32,   U32,      4);
458         MAKE_BUFREADER_GETNOEX_FXN(u64,   U64,      8);
459         MAKE_BUFREADER_GETNOEX_FXN(s8,    S8,       1);
460         MAKE_BUFREADER_GETNOEX_FXN(s16,   S16,      2);
461         MAKE_BUFREADER_GETNOEX_FXN(s32,   S32,      4);
462         MAKE_BUFREADER_GETNOEX_FXN(s64,   S64,      8);
463         MAKE_BUFREADER_GETNOEX_FXN(f32,   F1000,    4);
464         MAKE_BUFREADER_GETNOEX_FXN(v2s16, V2S16,    4);
465         MAKE_BUFREADER_GETNOEX_FXN(v3s16, V3S16,    6);
466         MAKE_BUFREADER_GETNOEX_FXN(v2s32, V2S32,    8);
467         MAKE_BUFREADER_GETNOEX_FXN(v3s32, V3S32,   12);
468         MAKE_BUFREADER_GETNOEX_FXN(v2f,   V2F1000,  8);
469         MAKE_BUFREADER_GETNOEX_FXN(v3f,   V3F1000, 12);
470         MAKE_BUFREADER_GETNOEX_FXN(video::SColor, ARGB8, 4);
471
472         bool getStringNoEx(std::string *val);
473         bool getWideStringNoEx(std::wstring *val);
474         bool getLongStringNoEx(std::string *val);
475         bool getRawDataNoEx(void *data, size_t len);
476
477         MAKE_BUFREADER_GET_FXN(u8,            U8);
478         MAKE_BUFREADER_GET_FXN(u16,           U16);
479         MAKE_BUFREADER_GET_FXN(u32,           U32);
480         MAKE_BUFREADER_GET_FXN(u64,           U64);
481         MAKE_BUFREADER_GET_FXN(s8,            S8);
482         MAKE_BUFREADER_GET_FXN(s16,           S16);
483         MAKE_BUFREADER_GET_FXN(s32,           S32);
484         MAKE_BUFREADER_GET_FXN(s64,           S64);
485         MAKE_BUFREADER_GET_FXN(f32,           F1000);
486         MAKE_BUFREADER_GET_FXN(v2s16,         V2S16);
487         MAKE_BUFREADER_GET_FXN(v3s16,         V3S16);
488         MAKE_BUFREADER_GET_FXN(v2s32,         V2S32);
489         MAKE_BUFREADER_GET_FXN(v3s32,         V3S32);
490         MAKE_BUFREADER_GET_FXN(v2f,           V2F1000);
491         MAKE_BUFREADER_GET_FXN(v3f,           V3F1000);
492         MAKE_BUFREADER_GET_FXN(video::SColor, ARGB8);
493         MAKE_BUFREADER_GET_FXN(std::string,   String);
494         MAKE_BUFREADER_GET_FXN(std::wstring,  WideString);
495         MAKE_BUFREADER_GET_FXN(std::string,   LongString);
496
497         inline void getRawData(void *val, size_t len)
498         {
499                 if (!getRawDataNoEx(val, len))
500                         throw eof_ser_err;
501         }
502
503         inline size_t remaining()
504         {
505                 assert(pos <= size);
506                 return size - pos;
507         }
508
509         const u8 *data;
510         size_t size;
511         size_t pos;
512 };
513
514 #undef MAKE_BUFREADER_GET_FXN
515 #undef MAKE_BUFREADER_GETNOEX_FXN
516
517
518 ////
519 //// Vector-based write routines
520 ////
521
522 inline void putU8(std::vector<u8> *dest, u8 val)
523 {
524         dest->push_back((val >> 0) & 0xFF);
525 }
526
527 inline void putU16(std::vector<u8> *dest, u16 val)
528 {
529         dest->push_back((val >> 8) & 0xFF);
530         dest->push_back((val >> 0) & 0xFF);
531 }
532
533 inline void putU32(std::vector<u8> *dest, u32 val)
534 {
535         dest->push_back((val >> 24) & 0xFF);
536         dest->push_back((val >> 16) & 0xFF);
537         dest->push_back((val >>  8) & 0xFF);
538         dest->push_back((val >>  0) & 0xFF);
539 }
540
541 inline void putU64(std::vector<u8> *dest, u64 val)
542 {
543         dest->push_back((val >> 56) & 0xFF);
544         dest->push_back((val >> 48) & 0xFF);
545         dest->push_back((val >> 40) & 0xFF);
546         dest->push_back((val >> 32) & 0xFF);
547         dest->push_back((val >> 24) & 0xFF);
548         dest->push_back((val >> 16) & 0xFF);
549         dest->push_back((val >>  8) & 0xFF);
550         dest->push_back((val >>  0) & 0xFF);
551 }
552
553 inline void putS8(std::vector<u8> *dest, s8 val)
554 {
555         putU8(dest, val);
556 }
557
558 inline void putS16(std::vector<u8> *dest, s16 val)
559 {
560         putU16(dest, val);
561 }
562
563 inline void putS32(std::vector<u8> *dest, s32 val)
564 {
565         putU32(dest, val);
566 }
567
568 inline void putS64(std::vector<u8> *dest, s64 val)
569 {
570         putU64(dest, val);
571 }
572
573 inline void putF1000(std::vector<u8> *dest, f32 val)
574 {
575         putS32(dest, val * FIXEDPOINT_FACTOR);
576 }
577
578 inline void putV2S16(std::vector<u8> *dest, v2s16 val)
579 {
580         putS16(dest, val.X);
581         putS16(dest, val.Y);
582 }
583
584 inline void putV3S16(std::vector<u8> *dest, v3s16 val)
585 {
586         putS16(dest, val.X);
587         putS16(dest, val.Y);
588         putS16(dest, val.Z);
589 }
590
591 inline void putV2S32(std::vector<u8> *dest, v2s32 val)
592 {
593         putS32(dest, val.X);
594         putS32(dest, val.Y);
595 }
596
597 inline void putV3S32(std::vector<u8> *dest, v3s32 val)
598 {
599         putS32(dest, val.X);
600         putS32(dest, val.Y);
601         putS32(dest, val.Z);
602 }
603
604 inline void putV2F1000(std::vector<u8> *dest, v2f val)
605 {
606         putF1000(dest, val.X);
607         putF1000(dest, val.Y);
608 }
609
610 inline void putV3F1000(std::vector<u8> *dest, v3f val)
611 {
612         putF1000(dest, val.X);
613         putF1000(dest, val.Y);
614         putF1000(dest, val.Z);
615 }
616
617 inline void putARGB8(std::vector<u8> *dest, video::SColor val)
618 {
619         putU32(dest, val.color);
620 }
621
622 inline void putString(std::vector<u8> *dest, const std::string &val)
623 {
624         if (val.size() > STRING_MAX_LEN)
625                 throw SerializationError("String too long");
626
627         putU16(dest, val.size());
628         dest->insert(dest->end(), val.begin(), val.end());
629 }
630
631 inline void putWideString(std::vector<u8> *dest, const std::wstring &val)
632 {
633         if (val.size() > WIDE_STRING_MAX_LEN)
634                 throw SerializationError("String too long");
635
636         putU16(dest, val.size());
637         for (size_t i = 0; i != val.size(); i++)
638                 putU16(dest, val[i]);
639 }
640
641 inline void putLongString(std::vector<u8> *dest, const std::string &val)
642 {
643         if (val.size() > LONG_STRING_MAX_LEN)
644                 throw SerializationError("String too long");
645
646         putU32(dest, val.size());
647         dest->insert(dest->end(), val.begin(), val.end());
648 }
649
650 inline void putRawData(std::vector<u8> *dest, const void *src, size_t len)
651 {
652         dest->insert(dest->end(), (u8 *)src, (u8 *)src + len);
653 }
654
655 #endif