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
29 #define __BYTE_ORDER 0
30 #define __LITTLE_ENDIAN 0
31 #define __BIG_ENDIAN 1
32 #elif defined(__MACH__) && defined(__APPLE__)
33 #include <machine/endian.h>
34 #elif defined(__FreeBSD__)
35 #include <sys/endian.h>
40 #include <cstring> // for memcpy
45 #define FIXEDPOINT_FACTOR 1000.0f
47 // 0x7FFFFFFF / 1000.0f is not serializable.
48 // The limited float precision at this magnitude may cause the result to round
49 // to a greater value than can be represented by a 32 bit integer when increased
50 // by a factor of FIXEDPOINT_FACTOR. As a result, [F1000_MIN..F1000_MAX] does
51 // not represent the full range, but rather the largest safe range, of values on
52 // all supported architectures. Note: This definition makes assumptions on
53 // platform float-to-int conversion behavior.
54 #define F1000_MIN ((float)(s32)((-0x7FFFFFFF - 1) / FIXEDPOINT_FACTOR))
55 #define F1000_MAX ((float)(s32)((0x7FFFFFFF) / FIXEDPOINT_FACTOR))
57 #define STRING_MAX_LEN 0xFFFF
58 #define WIDE_STRING_MAX_LEN 0xFFFF
59 // 64 MB ought to be enough for anybody - Billy G.
60 #define LONG_STRING_MAX_LEN (64 * 1024 * 1024)
64 // use machine native byte swapping routines
65 // Note: memcpy below is optimized out by modern compilers
67 inline u16 readU16(const u8 *data)
70 memcpy(&val, data, 2);
74 inline u32 readU32(const u8 *data)
77 memcpy(&val, data, 4);
81 inline u64 readU64(const u8 *data)
84 memcpy(&val, data, 8);
88 inline void writeU16(u8 *data, u16 i)
91 memcpy(data, &val, 2);
94 inline void writeU32(u8 *data, u32 i)
97 memcpy(data, &val, 4);
100 inline void writeU64(u8 *data, u64 i)
102 u64 val = htobe64(i);
103 memcpy(data, &val, 8);
107 // generic byte-swapping implementation
109 inline u16 readU16(const u8 *data)
112 ((u16)data[0] << 8) | ((u16)data[1] << 0);
115 inline u32 readU32(const u8 *data)
118 ((u32)data[0] << 24) | ((u32)data[1] << 16) |
119 ((u32)data[2] << 8) | ((u32)data[3] << 0);
122 inline u64 readU64(const u8 *data)
125 ((u64)data[0] << 56) | ((u64)data[1] << 48) |
126 ((u64)data[2] << 40) | ((u64)data[3] << 32) |
127 ((u64)data[4] << 24) | ((u64)data[5] << 16) |
128 ((u64)data[6] << 8) | ((u64)data[7] << 0);
131 inline void writeU16(u8 *data, u16 i)
133 data[0] = (i >> 8) & 0xFF;
134 data[1] = (i >> 0) & 0xFF;
137 inline void writeU32(u8 *data, u32 i)
139 data[0] = (i >> 24) & 0xFF;
140 data[1] = (i >> 16) & 0xFF;
141 data[2] = (i >> 8) & 0xFF;
142 data[3] = (i >> 0) & 0xFF;
145 inline void writeU64(u8 *data, u64 i)
147 data[0] = (i >> 56) & 0xFF;
148 data[1] = (i >> 48) & 0xFF;
149 data[2] = (i >> 40) & 0xFF;
150 data[3] = (i >> 32) & 0xFF;
151 data[4] = (i >> 24) & 0xFF;
152 data[5] = (i >> 16) & 0xFF;
153 data[6] = (i >> 8) & 0xFF;
154 data[7] = (i >> 0) & 0xFF;
157 #endif // HAVE_ENDIAN_H
159 //////////////// read routines ////////////////
161 inline u8 readU8(const u8 *data)
163 return ((u8)data[0] << 0);
166 inline s8 readS8(const u8 *data)
168 return (s8)readU8(data);
171 inline s16 readS16(const u8 *data)
173 return (s16)readU16(data);
176 inline s32 readS32(const u8 *data)
178 return (s32)readU32(data);
181 inline s64 readS64(const u8 *data)
183 return (s64)readU64(data);
186 inline f32 readF1000(const u8 *data)
188 return (f32)readS32(data) / FIXEDPOINT_FACTOR;
191 inline video::SColor readARGB8(const u8 *data)
193 video::SColor p(readU32(data));
197 inline v2s16 readV2S16(const u8 *data)
200 p.X = readS16(&data[0]);
201 p.Y = readS16(&data[2]);
205 inline v3s16 readV3S16(const u8 *data)
208 p.X = readS16(&data[0]);
209 p.Y = readS16(&data[2]);
210 p.Z = readS16(&data[4]);
214 inline v2s32 readV2S32(const u8 *data)
217 p.X = readS32(&data[0]);
218 p.Y = readS32(&data[4]);
222 inline v3s32 readV3S32(const u8 *data)
225 p.X = readS32(&data[0]);
226 p.Y = readS32(&data[4]);
227 p.Z = readS32(&data[8]);
231 inline v2f readV2F1000(const u8 *data)
234 p.X = (float)readF1000(&data[0]);
235 p.Y = (float)readF1000(&data[4]);
239 inline v3f readV3F1000(const u8 *data)
242 p.X = (float)readF1000(&data[0]);
243 p.Y = (float)readF1000(&data[4]);
244 p.Z = (float)readF1000(&data[8]);
248 /////////////// write routines ////////////////
250 inline void writeU8(u8 *data, u8 i)
252 data[0] = (i >> 0) & 0xFF;
255 inline void writeS8(u8 *data, s8 i)
257 writeU8(data, (u8)i);
260 inline void writeS16(u8 *data, s16 i)
262 writeU16(data, (u16)i);
265 inline void writeS32(u8 *data, s32 i)
267 writeU32(data, (u32)i);
270 inline void writeS64(u8 *data, s64 i)
272 writeU64(data, (u64)i);
275 inline void writeF1000(u8 *data, f32 i)
277 assert(i >= F1000_MIN && i <= F1000_MAX);
278 writeS32(data, i * FIXEDPOINT_FACTOR);
281 inline void writeARGB8(u8 *data, video::SColor p)
283 writeU32(data, p.color);
286 inline void writeV2S16(u8 *data, v2s16 p)
288 writeS16(&data[0], p.X);
289 writeS16(&data[2], p.Y);
292 inline void writeV3S16(u8 *data, v3s16 p)
294 writeS16(&data[0], p.X);
295 writeS16(&data[2], p.Y);
296 writeS16(&data[4], p.Z);
299 inline void writeV2S32(u8 *data, v2s32 p)
301 writeS32(&data[0], p.X);
302 writeS32(&data[4], p.Y);
305 inline void writeV3S32(u8 *data, v3s32 p)
307 writeS32(&data[0], p.X);
308 writeS32(&data[4], p.Y);
309 writeS32(&data[8], p.Z);
312 inline void writeV2F1000(u8 *data, v2f p)
314 writeF1000(&data[0], p.X);
315 writeF1000(&data[4], p.Y);
318 inline void writeV3F1000(u8 *data, v3f p)
320 writeF1000(&data[0], p.X);
321 writeF1000(&data[4], p.Y);
322 writeF1000(&data[8], p.Z);
326 //// Iostream wrapper for data read/write
329 #define MAKE_STREAM_READ_FXN(T, N, S) \
330 inline T read ## N(std::istream &is) \
333 is.read(buf, sizeof(buf)); \
334 return read ## N((u8 *)buf); \
337 #define MAKE_STREAM_WRITE_FXN(T, N, S) \
338 inline void write ## N(std::ostream &os, T val) \
341 write ## N((u8 *)buf, val); \
342 os.write(buf, sizeof(buf)); \
345 MAKE_STREAM_READ_FXN(u8, U8, 1);
346 MAKE_STREAM_READ_FXN(u16, U16, 2);
347 MAKE_STREAM_READ_FXN(u32, U32, 4);
348 MAKE_STREAM_READ_FXN(u64, U64, 8);
349 MAKE_STREAM_READ_FXN(s8, S8, 1);
350 MAKE_STREAM_READ_FXN(s16, S16, 2);
351 MAKE_STREAM_READ_FXN(s32, S32, 4);
352 MAKE_STREAM_READ_FXN(s64, S64, 8);
353 MAKE_STREAM_READ_FXN(f32, F1000, 4);
354 MAKE_STREAM_READ_FXN(v2s16, V2S16, 4);
355 MAKE_STREAM_READ_FXN(v3s16, V3S16, 6);
356 MAKE_STREAM_READ_FXN(v2s32, V2S32, 8);
357 MAKE_STREAM_READ_FXN(v3s32, V3S32, 12);
358 MAKE_STREAM_READ_FXN(v2f, V2F1000, 8);
359 MAKE_STREAM_READ_FXN(v3f, V3F1000, 12);
360 MAKE_STREAM_READ_FXN(video::SColor, ARGB8, 4);
362 MAKE_STREAM_WRITE_FXN(u8, U8, 1);
363 MAKE_STREAM_WRITE_FXN(u16, U16, 2);
364 MAKE_STREAM_WRITE_FXN(u32, U32, 4);
365 MAKE_STREAM_WRITE_FXN(u64, U64, 8);
366 MAKE_STREAM_WRITE_FXN(s8, S8, 1);
367 MAKE_STREAM_WRITE_FXN(s16, S16, 2);
368 MAKE_STREAM_WRITE_FXN(s32, S32, 4);
369 MAKE_STREAM_WRITE_FXN(s64, S64, 8);
370 MAKE_STREAM_WRITE_FXN(f32, F1000, 4);
371 MAKE_STREAM_WRITE_FXN(v2s16, V2S16, 4);
372 MAKE_STREAM_WRITE_FXN(v3s16, V3S16, 6);
373 MAKE_STREAM_WRITE_FXN(v2s32, V2S32, 8);
374 MAKE_STREAM_WRITE_FXN(v3s32, V3S32, 12);
375 MAKE_STREAM_WRITE_FXN(v2f, V2F1000, 8);
376 MAKE_STREAM_WRITE_FXN(v3f, V3F1000, 12);
377 MAKE_STREAM_WRITE_FXN(video::SColor, ARGB8, 4);
380 //// More serialization stuff
383 // Creates a string with the length as the first two bytes
384 std::string serializeString(const std::string &plain);
386 // Creates a string with the length as the first two bytes from wide string
387 std::string serializeWideString(const std::wstring &plain);
389 // Reads a string with the length as the first two bytes
390 std::string deSerializeString(std::istream &is);
392 // Reads a wide string with the length as the first two bytes
393 std::wstring deSerializeWideString(std::istream &is);
395 // Creates a string with the length as the first four bytes
396 std::string serializeLongString(const std::string &plain);
398 // Reads a string with the length as the first four bytes
399 std::string deSerializeLongString(std::istream &is);
401 // Creates a string encoded in JSON format (almost equivalent to a C string literal)
402 std::string serializeJsonString(const std::string &plain);
404 // Reads a string encoded in JSON format
405 std::string deSerializeJsonString(std::istream &is);
407 // If the string contains spaces, quotes or control characters, encodes as JSON.
408 // Else returns the string unmodified.
409 std::string serializeJsonStringIfNeeded(const std::string &s);
411 // Parses a string serialized by serializeJsonStringIfNeeded.
412 std::string deSerializeJsonStringIfNeeded(std::istream &is);
414 // Creates a string consisting of the hexadecimal representation of `data`
415 std::string serializeHexString(const std::string &data, bool insert_spaces=false);
417 // Creates a string containing comma delimited values of a struct whose layout is
418 // described by the parameter format
419 bool serializeStructToString(std::string *out,
420 std::string format, void *value);
422 // Reads a comma delimited string of values into a struct whose layout is
423 // decribed by the parameter format
424 bool deSerializeStringToStruct(std::string valstr,
425 std::string format, void *out, size_t olen);
431 #define MAKE_BUFREADER_GETNOEX_FXN(T, N, S) \
432 inline bool get ## N ## NoEx(T *val) \
434 if (pos + S > size) \
436 *val = read ## N(data + pos); \
441 #define MAKE_BUFREADER_GET_FXN(T, N) \
442 inline T get ## N() \
445 if (!get ## N ## NoEx(&val)) \
446 throw SerializationError("Attempted read past end of data"); \
452 BufReader(const u8 *data_, size_t size_) :
458 MAKE_BUFREADER_GETNOEX_FXN(u8, U8, 1);
459 MAKE_BUFREADER_GETNOEX_FXN(u16, U16, 2);
460 MAKE_BUFREADER_GETNOEX_FXN(u32, U32, 4);
461 MAKE_BUFREADER_GETNOEX_FXN(u64, U64, 8);
462 MAKE_BUFREADER_GETNOEX_FXN(s8, S8, 1);
463 MAKE_BUFREADER_GETNOEX_FXN(s16, S16, 2);
464 MAKE_BUFREADER_GETNOEX_FXN(s32, S32, 4);
465 MAKE_BUFREADER_GETNOEX_FXN(s64, S64, 8);
466 MAKE_BUFREADER_GETNOEX_FXN(f32, F1000, 4);
467 MAKE_BUFREADER_GETNOEX_FXN(v2s16, V2S16, 4);
468 MAKE_BUFREADER_GETNOEX_FXN(v3s16, V3S16, 6);
469 MAKE_BUFREADER_GETNOEX_FXN(v2s32, V2S32, 8);
470 MAKE_BUFREADER_GETNOEX_FXN(v3s32, V3S32, 12);
471 MAKE_BUFREADER_GETNOEX_FXN(v2f, V2F1000, 8);
472 MAKE_BUFREADER_GETNOEX_FXN(v3f, V3F1000, 12);
473 MAKE_BUFREADER_GETNOEX_FXN(video::SColor, ARGB8, 4);
475 bool getStringNoEx(std::string *val);
476 bool getWideStringNoEx(std::wstring *val);
477 bool getLongStringNoEx(std::string *val);
478 bool getRawDataNoEx(void *data, size_t len);
480 MAKE_BUFREADER_GET_FXN(u8, U8);
481 MAKE_BUFREADER_GET_FXN(u16, U16);
482 MAKE_BUFREADER_GET_FXN(u32, U32);
483 MAKE_BUFREADER_GET_FXN(u64, U64);
484 MAKE_BUFREADER_GET_FXN(s8, S8);
485 MAKE_BUFREADER_GET_FXN(s16, S16);
486 MAKE_BUFREADER_GET_FXN(s32, S32);
487 MAKE_BUFREADER_GET_FXN(s64, S64);
488 MAKE_BUFREADER_GET_FXN(f32, F1000);
489 MAKE_BUFREADER_GET_FXN(v2s16, V2S16);
490 MAKE_BUFREADER_GET_FXN(v3s16, V3S16);
491 MAKE_BUFREADER_GET_FXN(v2s32, V2S32);
492 MAKE_BUFREADER_GET_FXN(v3s32, V3S32);
493 MAKE_BUFREADER_GET_FXN(v2f, V2F1000);
494 MAKE_BUFREADER_GET_FXN(v3f, V3F1000);
495 MAKE_BUFREADER_GET_FXN(video::SColor, ARGB8);
496 MAKE_BUFREADER_GET_FXN(std::string, String);
497 MAKE_BUFREADER_GET_FXN(std::wstring, WideString);
498 MAKE_BUFREADER_GET_FXN(std::string, LongString);
500 inline void getRawData(void *val, size_t len)
502 if (!getRawDataNoEx(val, len))
503 throw SerializationError("Attempted read past end of data");
506 inline size_t remaining()
517 #undef MAKE_BUFREADER_GET_FXN
518 #undef MAKE_BUFREADER_GETNOEX_FXN
522 //// Vector-based write routines
525 inline void putU8(std::vector<u8> *dest, u8 val)
527 dest->push_back((val >> 0) & 0xFF);
530 inline void putU16(std::vector<u8> *dest, u16 val)
532 dest->push_back((val >> 8) & 0xFF);
533 dest->push_back((val >> 0) & 0xFF);
536 inline void putU32(std::vector<u8> *dest, u32 val)
538 dest->push_back((val >> 24) & 0xFF);
539 dest->push_back((val >> 16) & 0xFF);
540 dest->push_back((val >> 8) & 0xFF);
541 dest->push_back((val >> 0) & 0xFF);
544 inline void putU64(std::vector<u8> *dest, u64 val)
546 dest->push_back((val >> 56) & 0xFF);
547 dest->push_back((val >> 48) & 0xFF);
548 dest->push_back((val >> 40) & 0xFF);
549 dest->push_back((val >> 32) & 0xFF);
550 dest->push_back((val >> 24) & 0xFF);
551 dest->push_back((val >> 16) & 0xFF);
552 dest->push_back((val >> 8) & 0xFF);
553 dest->push_back((val >> 0) & 0xFF);
556 inline void putS8(std::vector<u8> *dest, s8 val)
561 inline void putS16(std::vector<u8> *dest, s16 val)
566 inline void putS32(std::vector<u8> *dest, s32 val)
571 inline void putS64(std::vector<u8> *dest, s64 val)
576 inline void putF1000(std::vector<u8> *dest, f32 val)
578 putS32(dest, val * FIXEDPOINT_FACTOR);
581 inline void putV2S16(std::vector<u8> *dest, v2s16 val)
587 inline void putV3S16(std::vector<u8> *dest, v3s16 val)
594 inline void putV2S32(std::vector<u8> *dest, v2s32 val)
600 inline void putV3S32(std::vector<u8> *dest, v3s32 val)
607 inline void putV2F1000(std::vector<u8> *dest, v2f val)
609 putF1000(dest, val.X);
610 putF1000(dest, val.Y);
613 inline void putV3F1000(std::vector<u8> *dest, v3f val)
615 putF1000(dest, val.X);
616 putF1000(dest, val.Y);
617 putF1000(dest, val.Z);
620 inline void putARGB8(std::vector<u8> *dest, video::SColor val)
622 putU32(dest, val.color);
625 inline void putString(std::vector<u8> *dest, const std::string &val)
627 if (val.size() > STRING_MAX_LEN)
628 throw SerializationError("String too long");
630 putU16(dest, val.size());
631 dest->insert(dest->end(), val.begin(), val.end());
634 inline void putWideString(std::vector<u8> *dest, const std::wstring &val)
636 if (val.size() > WIDE_STRING_MAX_LEN)
637 throw SerializationError("String too long");
639 putU16(dest, val.size());
640 for (size_t i = 0; i != val.size(); i++)
641 putU16(dest, val[i]);
644 inline void putLongString(std::vector<u8> *dest, const std::string &val)
646 if (val.size() > LONG_STRING_MAX_LEN)
647 throw SerializationError("String too long");
649 putU32(dest, val.size());
650 dest->insert(dest->end(), val.begin(), val.end());
653 inline void putRawData(std::vector<u8> *dest, const void *src, size_t len)
655 dest->insert(dest->end(), (u8 *)src, (u8 *)src + len);