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.
20 #ifndef UTIL_SERIALIZE_HEADER
21 #define UTIL_SERIALIZE_HEADER
23 #include "../irrlichttypes_bloated.h"
24 #include "../exceptions.h" // for SerializationError
25 #include "../debug.h" // for assert
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>
41 #include <string.h> // 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)
65 // use machine native byte swapping routines
66 // Note: memcpy below is optimized out by modern compilers
68 inline u16 readU16(const u8 *data)
71 memcpy(&val, data, 2);
75 inline u32 readU32(const u8 *data)
78 memcpy(&val, data, 4);
82 inline u64 readU64(const u8 *data)
85 memcpy(&val, data, 8);
89 inline void writeU16(u8 *data, u16 i)
92 memcpy(data, &val, 2);
95 inline void writeU32(u8 *data, u32 i)
98 memcpy(data, &val, 4);
101 inline void writeU64(u8 *data, u64 i)
103 u64 val = htobe64(i);
104 memcpy(data, &val, 8);
108 // generic byte-swapping implementation
110 inline u16 readU16(const u8 *data)
113 ((u16)data[0] << 8) | ((u16)data[1] << 0);
116 inline u32 readU32(const u8 *data)
119 ((u32)data[0] << 24) | ((u32)data[1] << 16) |
120 ((u32)data[2] << 8) | ((u32)data[3] << 0);
123 inline u64 readU64(const u8 *data)
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);
132 inline void writeU16(u8 *data, u16 i)
134 data[0] = (i >> 8) & 0xFF;
135 data[1] = (i >> 0) & 0xFF;
138 inline void writeU32(u8 *data, u32 i)
140 data[0] = (i >> 24) & 0xFF;
141 data[1] = (i >> 16) & 0xFF;
142 data[2] = (i >> 8) & 0xFF;
143 data[3] = (i >> 0) & 0xFF;
146 inline void writeU64(u8 *data, u64 i)
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;
158 #endif // HAVE_ENDIAN_H
160 //////////////// read routines ////////////////
162 inline u8 readU8(const u8 *data)
164 return ((u8)data[0] << 0);
167 inline s8 readS8(const u8 *data)
169 return (s8)readU8(data);
172 inline s16 readS16(const u8 *data)
174 return (s16)readU16(data);
177 inline s32 readS32(const u8 *data)
179 return (s32)readU32(data);
182 inline s64 readS64(const u8 *data)
184 return (s64)readU64(data);
187 inline f32 readF1000(const u8 *data)
189 return (f32)readS32(data) / FIXEDPOINT_FACTOR;
192 inline video::SColor readARGB8(const u8 *data)
194 video::SColor p(readU32(data));
198 inline v2s16 readV2S16(const u8 *data)
201 p.X = readS16(&data[0]);
202 p.Y = readS16(&data[2]);
206 inline v3s16 readV3S16(const u8 *data)
209 p.X = readS16(&data[0]);
210 p.Y = readS16(&data[2]);
211 p.Z = readS16(&data[4]);
215 inline v2s32 readV2S32(const u8 *data)
218 p.X = readS32(&data[0]);
219 p.Y = readS32(&data[4]);
223 inline v3s32 readV3S32(const u8 *data)
226 p.X = readS32(&data[0]);
227 p.Y = readS32(&data[4]);
228 p.Z = readS32(&data[8]);
232 inline v2f readV2F1000(const u8 *data)
235 p.X = (float)readF1000(&data[0]);
236 p.Y = (float)readF1000(&data[4]);
240 inline v3f readV3F1000(const u8 *data)
243 p.X = (float)readF1000(&data[0]);
244 p.Y = (float)readF1000(&data[4]);
245 p.Z = (float)readF1000(&data[8]);
249 /////////////// write routines ////////////////
251 inline void writeU8(u8 *data, u8 i)
253 data[0] = (i >> 0) & 0xFF;
256 inline void writeS8(u8 *data, s8 i)
258 writeU8(data, (u8)i);
261 inline void writeS16(u8 *data, s16 i)
263 writeU16(data, (u16)i);
266 inline void writeS32(u8 *data, s32 i)
268 writeU32(data, (u32)i);
271 inline void writeS64(u8 *data, s64 i)
273 writeU64(data, (u64)i);
276 inline void writeF1000(u8 *data, f32 i)
278 assert(i >= F1000_MIN && i <= F1000_MAX);
279 writeS32(data, i * FIXEDPOINT_FACTOR);
282 inline void writeARGB8(u8 *data, video::SColor p)
284 writeU32(data, p.color);
287 inline void writeV2S16(u8 *data, v2s16 p)
289 writeS16(&data[0], p.X);
290 writeS16(&data[2], p.Y);
293 inline void writeV3S16(u8 *data, v3s16 p)
295 writeS16(&data[0], p.X);
296 writeS16(&data[2], p.Y);
297 writeS16(&data[4], p.Z);
300 inline void writeV2S32(u8 *data, v2s32 p)
302 writeS32(&data[0], p.X);
303 writeS32(&data[4], p.Y);
306 inline void writeV3S32(u8 *data, v3s32 p)
308 writeS32(&data[0], p.X);
309 writeS32(&data[4], p.Y);
310 writeS32(&data[8], p.Z);
313 inline void writeV2F1000(u8 *data, v2f p)
315 writeF1000(&data[0], p.X);
316 writeF1000(&data[4], p.Y);
319 inline void writeV3F1000(u8 *data, v3f p)
321 writeF1000(&data[0], p.X);
322 writeF1000(&data[4], p.Y);
323 writeF1000(&data[8], p.Z);
327 //// Iostream wrapper for data read/write
330 #define MAKE_STREAM_READ_FXN(T, N, S) \
331 inline T read ## N(std::istream &is) \
334 is.read(buf, sizeof(buf)); \
335 return read ## N((u8 *)buf); \
338 #define MAKE_STREAM_WRITE_FXN(T, N, S) \
339 inline void write ## N(std::ostream &os, T val) \
342 write ## N((u8 *)buf, val); \
343 os.write(buf, sizeof(buf)); \
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);
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);
381 //// More serialization stuff
384 // Creates a string with the length as the first two bytes
385 std::string serializeString(const std::string &plain);
387 // Creates a string with the length as the first two bytes from wide string
388 std::string serializeWideString(const std::wstring &plain);
390 // Reads a string with the length as the first two bytes
391 std::string deSerializeString(std::istream &is);
393 // Reads a wide string with the length as the first two bytes
394 std::wstring deSerializeWideString(std::istream &is);
396 // Creates a string with the length as the first four bytes
397 std::string serializeLongString(const std::string &plain);
399 // Reads a string with the length as the first four bytes
400 std::string deSerializeLongString(std::istream &is);
402 // Creates a string encoded in JSON format (almost equivalent to a C string literal)
403 std::string serializeJsonString(const std::string &plain);
405 // Reads a string encoded in JSON format
406 std::string deSerializeJsonString(std::istream &is);
408 // If the string contains spaces, quotes or control characters, encodes as JSON.
409 // Else returns the string unmodified.
410 std::string serializeJsonStringIfNeeded(const std::string &s);
412 // Parses a string serialized by serializeJsonStringIfNeeded.
413 std::string deSerializeJsonStringIfNeeded(std::istream &is);
415 // Creates a string consisting of the hexadecimal representation of `data`
416 std::string serializeHexString(const std::string &data, bool insert_spaces=false);
418 // Creates a string containing comma delimited values of a struct whose layout is
419 // described by the parameter format
420 bool serializeStructToString(std::string *out,
421 std::string format, void *value);
423 // Reads a comma delimited string of values into a struct whose layout is
424 // decribed by the parameter format
425 bool deSerializeStringToStruct(std::string valstr,
426 std::string format, void *out, size_t olen);
432 extern SerializationError eof_ser_err;
434 #define MAKE_BUFREADER_GETNOEX_FXN(T, N, S) \
435 inline bool get ## N ## NoEx(T *val) \
437 if (pos + S > size) \
439 *val = read ## N(data + pos); \
444 #define MAKE_BUFREADER_GET_FXN(T, N) \
445 inline T get ## N() \
448 if (!get ## N ## NoEx(&val)) \
455 BufReader(const u8 *data_, size_t size_) :
462 MAKE_BUFREADER_GETNOEX_FXN(u8, U8, 1);
463 MAKE_BUFREADER_GETNOEX_FXN(u16, U16, 2);
464 MAKE_BUFREADER_GETNOEX_FXN(u32, U32, 4);
465 MAKE_BUFREADER_GETNOEX_FXN(u64, U64, 8);
466 MAKE_BUFREADER_GETNOEX_FXN(s8, S8, 1);
467 MAKE_BUFREADER_GETNOEX_FXN(s16, S16, 2);
468 MAKE_BUFREADER_GETNOEX_FXN(s32, S32, 4);
469 MAKE_BUFREADER_GETNOEX_FXN(s64, S64, 8);
470 MAKE_BUFREADER_GETNOEX_FXN(f32, F1000, 4);
471 MAKE_BUFREADER_GETNOEX_FXN(v2s16, V2S16, 4);
472 MAKE_BUFREADER_GETNOEX_FXN(v3s16, V3S16, 6);
473 MAKE_BUFREADER_GETNOEX_FXN(v2s32, V2S32, 8);
474 MAKE_BUFREADER_GETNOEX_FXN(v3s32, V3S32, 12);
475 MAKE_BUFREADER_GETNOEX_FXN(v2f, V2F1000, 8);
476 MAKE_BUFREADER_GETNOEX_FXN(v3f, V3F1000, 12);
477 MAKE_BUFREADER_GETNOEX_FXN(video::SColor, ARGB8, 4);
479 bool getStringNoEx(std::string *val);
480 bool getWideStringNoEx(std::wstring *val);
481 bool getLongStringNoEx(std::string *val);
482 bool getRawDataNoEx(void *data, size_t len);
484 MAKE_BUFREADER_GET_FXN(u8, U8);
485 MAKE_BUFREADER_GET_FXN(u16, U16);
486 MAKE_BUFREADER_GET_FXN(u32, U32);
487 MAKE_BUFREADER_GET_FXN(u64, U64);
488 MAKE_BUFREADER_GET_FXN(s8, S8);
489 MAKE_BUFREADER_GET_FXN(s16, S16);
490 MAKE_BUFREADER_GET_FXN(s32, S32);
491 MAKE_BUFREADER_GET_FXN(s64, S64);
492 MAKE_BUFREADER_GET_FXN(f32, F1000);
493 MAKE_BUFREADER_GET_FXN(v2s16, V2S16);
494 MAKE_BUFREADER_GET_FXN(v3s16, V3S16);
495 MAKE_BUFREADER_GET_FXN(v2s32, V2S32);
496 MAKE_BUFREADER_GET_FXN(v3s32, V3S32);
497 MAKE_BUFREADER_GET_FXN(v2f, V2F1000);
498 MAKE_BUFREADER_GET_FXN(v3f, V3F1000);
499 MAKE_BUFREADER_GET_FXN(video::SColor, ARGB8);
500 MAKE_BUFREADER_GET_FXN(std::string, String);
501 MAKE_BUFREADER_GET_FXN(std::wstring, WideString);
502 MAKE_BUFREADER_GET_FXN(std::string, LongString);
504 inline void getRawData(void *val, size_t len)
506 if (!getRawDataNoEx(val, len))
510 inline size_t remaining()
521 #undef MAKE_BUFREADER_GET_FXN
522 #undef MAKE_BUFREADER_GETNOEX_FXN
526 //// Vector-based write routines
529 inline void putU8(std::vector<u8> *dest, u8 val)
531 dest->push_back((val >> 0) & 0xFF);
534 inline void putU16(std::vector<u8> *dest, u16 val)
536 dest->push_back((val >> 8) & 0xFF);
537 dest->push_back((val >> 0) & 0xFF);
540 inline void putU32(std::vector<u8> *dest, u32 val)
542 dest->push_back((val >> 24) & 0xFF);
543 dest->push_back((val >> 16) & 0xFF);
544 dest->push_back((val >> 8) & 0xFF);
545 dest->push_back((val >> 0) & 0xFF);
548 inline void putU64(std::vector<u8> *dest, u64 val)
550 dest->push_back((val >> 56) & 0xFF);
551 dest->push_back((val >> 48) & 0xFF);
552 dest->push_back((val >> 40) & 0xFF);
553 dest->push_back((val >> 32) & 0xFF);
554 dest->push_back((val >> 24) & 0xFF);
555 dest->push_back((val >> 16) & 0xFF);
556 dest->push_back((val >> 8) & 0xFF);
557 dest->push_back((val >> 0) & 0xFF);
560 inline void putS8(std::vector<u8> *dest, s8 val)
565 inline void putS16(std::vector<u8> *dest, s16 val)
570 inline void putS32(std::vector<u8> *dest, s32 val)
575 inline void putS64(std::vector<u8> *dest, s64 val)
580 inline void putF1000(std::vector<u8> *dest, f32 val)
582 putS32(dest, val * FIXEDPOINT_FACTOR);
585 inline void putV2S16(std::vector<u8> *dest, v2s16 val)
591 inline void putV3S16(std::vector<u8> *dest, v3s16 val)
598 inline void putV2S32(std::vector<u8> *dest, v2s32 val)
604 inline void putV3S32(std::vector<u8> *dest, v3s32 val)
611 inline void putV2F1000(std::vector<u8> *dest, v2f val)
613 putF1000(dest, val.X);
614 putF1000(dest, val.Y);
617 inline void putV3F1000(std::vector<u8> *dest, v3f val)
619 putF1000(dest, val.X);
620 putF1000(dest, val.Y);
621 putF1000(dest, val.Z);
624 inline void putARGB8(std::vector<u8> *dest, video::SColor val)
626 putU32(dest, val.color);
629 inline void putString(std::vector<u8> *dest, const std::string &val)
631 if (val.size() > STRING_MAX_LEN)
632 throw SerializationError("String too long");
634 putU16(dest, val.size());
635 dest->insert(dest->end(), val.begin(), val.end());
638 inline void putWideString(std::vector<u8> *dest, const std::wstring &val)
640 if (val.size() > WIDE_STRING_MAX_LEN)
641 throw SerializationError("String too long");
643 putU16(dest, val.size());
644 for (size_t i = 0; i != val.size(); i++)
645 putU16(dest, val[i]);
648 inline void putLongString(std::vector<u8> *dest, const std::string &val)
650 if (val.size() > LONG_STRING_MAX_LEN)
651 throw SerializationError("String too long");
653 putU32(dest, val.size());
654 dest->insert(dest->end(), val.begin(), val.end());
657 inline void putRawData(std::vector<u8> *dest, const void *src, size_t len)
659 dest->insert(dest->end(), (u8 *)src, (u8 *)src + len);