3 Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
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.
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.
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.
22 #include "irrlichttypes_bloated.h"
23 #include "exceptions.h" // for SerializationError
24 #include "debug.h" // for assert
25 #include "ieee_float.h"
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>
41 #include <cstring> // for memcpy
46 #define FIXEDPOINT_FACTOR 1000.0f
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))
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)
64 extern FloatType g_serialize_f32_type;
67 // use machine native byte swapping routines
68 // Note: memcpy below is optimized out by modern compilers
70 inline u16 readU16(const u8 *data)
73 memcpy(&val, data, 2);
77 inline u32 readU32(const u8 *data)
80 memcpy(&val, data, 4);
84 inline u64 readU64(const u8 *data)
87 memcpy(&val, data, 8);
91 inline void writeU16(u8 *data, u16 i)
94 memcpy(data, &val, 2);
97 inline void writeU32(u8 *data, u32 i)
100 memcpy(data, &val, 4);
103 inline void writeU64(u8 *data, u64 i)
105 u64 val = htobe64(i);
106 memcpy(data, &val, 8);
110 // generic byte-swapping implementation
112 inline u16 readU16(const u8 *data)
115 ((u16)data[0] << 8) | ((u16)data[1] << 0);
118 inline u32 readU32(const u8 *data)
121 ((u32)data[0] << 24) | ((u32)data[1] << 16) |
122 ((u32)data[2] << 8) | ((u32)data[3] << 0);
125 inline u64 readU64(const u8 *data)
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);
134 inline void writeU16(u8 *data, u16 i)
136 data[0] = (i >> 8) & 0xFF;
137 data[1] = (i >> 0) & 0xFF;
140 inline void writeU32(u8 *data, u32 i)
142 data[0] = (i >> 24) & 0xFF;
143 data[1] = (i >> 16) & 0xFF;
144 data[2] = (i >> 8) & 0xFF;
145 data[3] = (i >> 0) & 0xFF;
148 inline void writeU64(u8 *data, u64 i)
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;
160 #endif // HAVE_ENDIAN_H
162 //////////////// read routines ////////////////
164 inline u8 readU8(const u8 *data)
166 return ((u8)data[0] << 0);
169 inline s8 readS8(const u8 *data)
171 return (s8)readU8(data);
174 inline s16 readS16(const u8 *data)
176 return (s16)readU16(data);
179 inline s32 readS32(const u8 *data)
181 return (s32)readU32(data);
184 inline s64 readS64(const u8 *data)
186 return (s64)readU64(data);
189 inline f32 readF1000(const u8 *data)
191 return (f32)readS32(data) / FIXEDPOINT_FACTOR;
194 inline f32 readF32(const u8 *data)
196 u32 u = readU32(data);
198 switch (g_serialize_f32_type) {
199 case FLOATTYPE_SYSTEM: {
205 return u32Tof32Slow(u);
206 case FLOATTYPE_UNKNOWN: // First initialization
207 g_serialize_f32_type = getFloatSerializationType();
208 return readF32(data);
210 throw SerializationError("readF32: Unreachable code");
213 inline video::SColor readARGB8(const u8 *data)
215 video::SColor p(readU32(data));
219 inline v2s16 readV2S16(const u8 *data)
222 p.X = readS16(&data[0]);
223 p.Y = readS16(&data[2]);
227 inline v3s16 readV3S16(const u8 *data)
230 p.X = readS16(&data[0]);
231 p.Y = readS16(&data[2]);
232 p.Z = readS16(&data[4]);
236 inline v2s32 readV2S32(const u8 *data)
239 p.X = readS32(&data[0]);
240 p.Y = readS32(&data[4]);
244 inline v3s32 readV3S32(const u8 *data)
247 p.X = readS32(&data[0]);
248 p.Y = readS32(&data[4]);
249 p.Z = readS32(&data[8]);
253 inline v3f readV3F1000(const u8 *data)
256 p.X = readF1000(&data[0]);
257 p.Y = readF1000(&data[4]);
258 p.Z = readF1000(&data[8]);
262 inline v2f readV2F32(const u8 *data)
265 p.X = readF32(&data[0]);
266 p.Y = readF32(&data[4]);
270 inline v3f readV3F32(const u8 *data)
273 p.X = readF32(&data[0]);
274 p.Y = readF32(&data[4]);
275 p.Z = readF32(&data[8]);
279 /////////////// write routines ////////////////
281 inline void writeU8(u8 *data, u8 i)
283 data[0] = (i >> 0) & 0xFF;
286 inline void writeS8(u8 *data, s8 i)
288 writeU8(data, (u8)i);
291 inline void writeS16(u8 *data, s16 i)
293 writeU16(data, (u16)i);
296 inline void writeS32(u8 *data, s32 i)
298 writeU32(data, (u32)i);
301 inline void writeS64(u8 *data, s64 i)
303 writeU64(data, (u64)i);
306 inline void writeF1000(u8 *data, f32 i)
308 assert(i >= F1000_MIN && i <= F1000_MAX);
309 writeS32(data, i * FIXEDPOINT_FACTOR);
312 inline void writeF32(u8 *data, f32 i)
314 switch (g_serialize_f32_type) {
315 case FLOATTYPE_SYSTEM: {
318 return writeU32(data, u);
321 return writeU32(data, f32Tou32Slow(i));
322 case FLOATTYPE_UNKNOWN: // First initialization
323 g_serialize_f32_type = getFloatSerializationType();
324 return writeF32(data, i);
326 throw SerializationError("writeF32: Unreachable code");
329 inline void writeARGB8(u8 *data, video::SColor p)
331 writeU32(data, p.color);
334 inline void writeV2S16(u8 *data, v2s16 p)
336 writeS16(&data[0], p.X);
337 writeS16(&data[2], p.Y);
340 inline void writeV3S16(u8 *data, v3s16 p)
342 writeS16(&data[0], p.X);
343 writeS16(&data[2], p.Y);
344 writeS16(&data[4], p.Z);
347 inline void writeV2S32(u8 *data, v2s32 p)
349 writeS32(&data[0], p.X);
350 writeS32(&data[4], p.Y);
353 inline void writeV3S32(u8 *data, v3s32 p)
355 writeS32(&data[0], p.X);
356 writeS32(&data[4], p.Y);
357 writeS32(&data[8], p.Z);
360 inline void writeV3F1000(u8 *data, v3f p)
362 writeF1000(&data[0], p.X);
363 writeF1000(&data[4], p.Y);
364 writeF1000(&data[8], p.Z);
367 inline void writeV2F32(u8 *data, v2f p)
369 writeF32(&data[0], p.X);
370 writeF32(&data[4], p.Y);
373 inline void writeV3F32(u8 *data, v3f p)
375 writeF32(&data[0], p.X);
376 writeF32(&data[4], p.Y);
377 writeF32(&data[8], p.Z);
381 //// Iostream wrapper for data read/write
384 #define MAKE_STREAM_READ_FXN(T, N, S) \
385 inline T read ## N(std::istream &is) \
388 is.read(buf, sizeof(buf)); \
389 return read ## N((u8 *)buf); \
392 #define MAKE_STREAM_WRITE_FXN(T, N, S) \
393 inline void write ## N(std::ostream &os, T val) \
396 write ## N((u8 *)buf, val); \
397 os.write(buf, sizeof(buf)); \
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);
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);
439 //// More serialization stuff
442 // Creates a string with the length as the first two bytes
443 std::string serializeString(const std::string &plain);
445 // Creates a string with the length as the first two bytes from wide string
446 std::string serializeWideString(const std::wstring &plain);
448 // Reads a string with the length as the first two bytes
449 std::string deSerializeString(std::istream &is);
451 // Reads a wide string with the length as the first two bytes
452 std::wstring deSerializeWideString(std::istream &is);
454 // Creates a string with the length as the first four bytes
455 std::string serializeLongString(const std::string &plain);
457 // Reads a string with the length as the first four bytes
458 std::string deSerializeLongString(std::istream &is);
460 // Creates a string encoded in JSON format (almost equivalent to a C string literal)
461 std::string serializeJsonString(const std::string &plain);
463 // Reads a string encoded in JSON format
464 std::string deSerializeJsonString(std::istream &is);
466 // If the string contains spaces, quotes or control characters, encodes as JSON.
467 // Else returns the string unmodified.
468 std::string serializeJsonStringIfNeeded(const std::string &s);
470 // Parses a string serialized by serializeJsonStringIfNeeded.
471 std::string deSerializeJsonStringIfNeeded(std::istream &is);
473 // Creates a string consisting of the hexadecimal representation of `data`
474 std::string serializeHexString(const std::string &data, bool insert_spaces=false);
476 // Creates a string containing comma delimited values of a struct whose layout is
477 // described by the parameter format
478 bool serializeStructToString(std::string *out,
479 std::string format, void *value);
481 // Reads a comma delimited string of values into a struct whose layout is
482 // decribed by the parameter format
483 bool deSerializeStringToStruct(std::string valstr,
484 std::string format, void *out, size_t olen);
490 #define MAKE_BUFREADER_GETNOEX_FXN(T, N, S) \
491 inline bool get ## N ## NoEx(T *val) \
493 if (pos + S > size) \
495 *val = read ## N(data + pos); \
500 #define MAKE_BUFREADER_GET_FXN(T, N) \
501 inline T get ## N() \
504 if (!get ## N ## NoEx(&val)) \
505 throw SerializationError("Attempted read past end of data"); \
511 BufReader(const u8 *data_, size_t size_) :
517 MAKE_BUFREADER_GETNOEX_FXN(u8, U8, 1);
518 MAKE_BUFREADER_GETNOEX_FXN(u16, U16, 2);
519 MAKE_BUFREADER_GETNOEX_FXN(u32, U32, 4);
520 MAKE_BUFREADER_GETNOEX_FXN(u64, U64, 8);
521 MAKE_BUFREADER_GETNOEX_FXN(s8, S8, 1);
522 MAKE_BUFREADER_GETNOEX_FXN(s16, S16, 2);
523 MAKE_BUFREADER_GETNOEX_FXN(s32, S32, 4);
524 MAKE_BUFREADER_GETNOEX_FXN(s64, S64, 8);
525 MAKE_BUFREADER_GETNOEX_FXN(f32, F1000, 4);
526 MAKE_BUFREADER_GETNOEX_FXN(v2s16, V2S16, 4);
527 MAKE_BUFREADER_GETNOEX_FXN(v3s16, V3S16, 6);
528 MAKE_BUFREADER_GETNOEX_FXN(v2s32, V2S32, 8);
529 MAKE_BUFREADER_GETNOEX_FXN(v3s32, V3S32, 12);
530 MAKE_BUFREADER_GETNOEX_FXN(v3f, V3F1000, 12);
531 MAKE_BUFREADER_GETNOEX_FXN(video::SColor, ARGB8, 4);
533 bool getStringNoEx(std::string *val);
534 bool getWideStringNoEx(std::wstring *val);
535 bool getLongStringNoEx(std::string *val);
536 bool getRawDataNoEx(void *data, size_t len);
538 MAKE_BUFREADER_GET_FXN(u8, U8);
539 MAKE_BUFREADER_GET_FXN(u16, U16);
540 MAKE_BUFREADER_GET_FXN(u32, U32);
541 MAKE_BUFREADER_GET_FXN(u64, U64);
542 MAKE_BUFREADER_GET_FXN(s8, S8);
543 MAKE_BUFREADER_GET_FXN(s16, S16);
544 MAKE_BUFREADER_GET_FXN(s32, S32);
545 MAKE_BUFREADER_GET_FXN(s64, S64);
546 MAKE_BUFREADER_GET_FXN(f32, F1000);
547 MAKE_BUFREADER_GET_FXN(v2s16, V2S16);
548 MAKE_BUFREADER_GET_FXN(v3s16, V3S16);
549 MAKE_BUFREADER_GET_FXN(v2s32, V2S32);
550 MAKE_BUFREADER_GET_FXN(v3s32, V3S32);
551 MAKE_BUFREADER_GET_FXN(v3f, V3F1000);
552 MAKE_BUFREADER_GET_FXN(video::SColor, ARGB8);
553 MAKE_BUFREADER_GET_FXN(std::string, String);
554 MAKE_BUFREADER_GET_FXN(std::wstring, WideString);
555 MAKE_BUFREADER_GET_FXN(std::string, LongString);
557 inline void getRawData(void *val, size_t len)
559 if (!getRawDataNoEx(val, len))
560 throw SerializationError("Attempted read past end of data");
563 inline size_t remaining()
574 #undef MAKE_BUFREADER_GET_FXN
575 #undef MAKE_BUFREADER_GETNOEX_FXN
579 //// Vector-based write routines
582 inline void putU8(std::vector<u8> *dest, u8 val)
584 dest->push_back((val >> 0) & 0xFF);
587 inline void putU16(std::vector<u8> *dest, u16 val)
589 dest->push_back((val >> 8) & 0xFF);
590 dest->push_back((val >> 0) & 0xFF);
593 inline void putU32(std::vector<u8> *dest, u32 val)
595 dest->push_back((val >> 24) & 0xFF);
596 dest->push_back((val >> 16) & 0xFF);
597 dest->push_back((val >> 8) & 0xFF);
598 dest->push_back((val >> 0) & 0xFF);
601 inline void putU64(std::vector<u8> *dest, u64 val)
603 dest->push_back((val >> 56) & 0xFF);
604 dest->push_back((val >> 48) & 0xFF);
605 dest->push_back((val >> 40) & 0xFF);
606 dest->push_back((val >> 32) & 0xFF);
607 dest->push_back((val >> 24) & 0xFF);
608 dest->push_back((val >> 16) & 0xFF);
609 dest->push_back((val >> 8) & 0xFF);
610 dest->push_back((val >> 0) & 0xFF);
613 inline void putS8(std::vector<u8> *dest, s8 val)
618 inline void putS16(std::vector<u8> *dest, s16 val)
623 inline void putS32(std::vector<u8> *dest, s32 val)
628 inline void putS64(std::vector<u8> *dest, s64 val)
633 inline void putF1000(std::vector<u8> *dest, f32 val)
635 putS32(dest, val * FIXEDPOINT_FACTOR);
638 inline void putV2S16(std::vector<u8> *dest, v2s16 val)
644 inline void putV3S16(std::vector<u8> *dest, v3s16 val)
651 inline void putV2S32(std::vector<u8> *dest, v2s32 val)
657 inline void putV3S32(std::vector<u8> *dest, v3s32 val)
664 inline void putV3F1000(std::vector<u8> *dest, v3f val)
666 putF1000(dest, val.X);
667 putF1000(dest, val.Y);
668 putF1000(dest, val.Z);
671 inline void putARGB8(std::vector<u8> *dest, video::SColor val)
673 putU32(dest, val.color);
676 inline void putString(std::vector<u8> *dest, const std::string &val)
678 if (val.size() > STRING_MAX_LEN)
679 throw SerializationError("String too long");
681 putU16(dest, val.size());
682 dest->insert(dest->end(), val.begin(), val.end());
685 inline void putWideString(std::vector<u8> *dest, const std::wstring &val)
687 if (val.size() > WIDE_STRING_MAX_LEN)
688 throw SerializationError("String too long");
690 putU16(dest, val.size());
691 for (size_t i = 0; i != val.size(); i++)
692 putU16(dest, val[i]);
695 inline void putLongString(std::vector<u8> *dest, const std::string &val)
697 if (val.size() > LONG_STRING_MAX_LEN)
698 throw SerializationError("String too long");
700 putU32(dest, val.size());
701 dest->insert(dest->end(), val.begin(), val.end());
704 inline void putRawData(std::vector<u8> *dest, const void *src, size_t len)
706 dest->insert(dest->end(), (u8 *)src, (u8 *)src + len);