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 "../debug.h" // for assert
28 #include <string.h> // for memcpy
33 #define FIXEDPOINT_FACTOR 1000.0f
35 // 0x7FFFFFFF / 1000.0f is not serializable.
36 // The limited float precision at this magnitude may cause the result to round
37 // to a greater value than can be represented by a 32 bit integer when increased
38 // by a factor of FIXEDPOINT_FACTOR. As a result, [F1000_MIN..F1000_MAX] does
39 // not represent the full range, but rather the largest safe range, of values on
40 // all supported architectures. Note: This definition makes assumptions on
41 // platform float-to-int conversion behavior.
42 #define F1000_MIN ((float)(s32)((-0x7FFFFFFF - 1) / FIXEDPOINT_FACTOR))
43 #define F1000_MAX ((float)(s32)((0x7FFFFFFF) / FIXEDPOINT_FACTOR))
45 #define STRING_MAX_LEN 0xFFFF
46 #define WIDE_STRING_MAX_LEN 0xFFFF
47 // 64 MB ought to be enough for anybody - Billy G.
48 #define LONG_STRING_MAX_LEN (64 * 1024 * 1024)
52 // use machine native byte swapping routines
53 // Note: memcpy below is optimized out by modern compilers
55 inline u16 readU16(const u8 *data)
58 memcpy(&val, data, 2);
62 inline u32 readU32(const u8 *data)
65 memcpy(&val, data, 4);
69 inline u64 readU64(const u8 *data)
72 memcpy(&val, data, 8);
76 inline void writeU16(u8 *data, u16 i)
79 memcpy(data, &val, 2);
82 inline void writeU32(u8 *data, u32 i)
85 memcpy(data, &val, 4);
88 inline void writeU64(u8 *data, u64 i)
91 memcpy(data, &val, 8);
95 // generic byte-swapping implementation
97 inline u16 readU16(const u8 *data)
100 ((u16)data[0] << 8) | ((u16)data[1] << 0);
103 inline u32 readU32(const u8 *data)
106 ((u32)data[0] << 24) | ((u32)data[1] << 16) |
107 ((u32)data[2] << 8) | ((u32)data[3] << 0);
110 inline u64 readU64(const u8 *data)
113 ((u64)data[0] << 56) | ((u64)data[1] << 48) |
114 ((u64)data[2] << 40) | ((u64)data[3] << 32) |
115 ((u64)data[4] << 24) | ((u64)data[5] << 16) |
116 ((u64)data[6] << 8) | ((u64)data[7] << 0);
119 inline void writeU16(u8 *data, u16 i)
121 data[0] = (i >> 8) & 0xFF;
122 data[1] = (i >> 0) & 0xFF;
125 inline void writeU32(u8 *data, u32 i)
127 data[0] = (i >> 24) & 0xFF;
128 data[1] = (i >> 16) & 0xFF;
129 data[2] = (i >> 8) & 0xFF;
130 data[3] = (i >> 0) & 0xFF;
133 inline void writeU64(u8 *data, u64 i)
135 data[0] = (i >> 56) & 0xFF;
136 data[1] = (i >> 48) & 0xFF;
137 data[2] = (i >> 40) & 0xFF;
138 data[3] = (i >> 32) & 0xFF;
139 data[4] = (i >> 24) & 0xFF;
140 data[5] = (i >> 16) & 0xFF;
141 data[6] = (i >> 8) & 0xFF;
142 data[7] = (i >> 0) & 0xFF;
145 #endif // HAVE_ENDIAN_H
147 //////////////// read routines ////////////////
149 inline u8 readU8(const u8 *data)
151 return ((u8)data[0] << 0);
154 inline s8 readS8(const u8 *data)
156 return (s8)readU8(data);
159 inline s16 readS16(const u8 *data)
161 return (s16)readU16(data);
164 inline s32 readS32(const u8 *data)
166 return (s32)readU32(data);
169 inline s64 readS64(const u8 *data)
171 return (s64)readU64(data);
174 inline f32 readF1000(const u8 *data)
176 return (f32)readS32(data) / FIXEDPOINT_FACTOR;
179 inline video::SColor readARGB8(const u8 *data)
181 video::SColor p(readU32(data));
185 inline v2s16 readV2S16(const u8 *data)
188 p.X = readS16(&data[0]);
189 p.Y = readS16(&data[2]);
193 inline v3s16 readV3S16(const u8 *data)
196 p.X = readS16(&data[0]);
197 p.Y = readS16(&data[2]);
198 p.Z = readS16(&data[4]);
202 inline v2s32 readV2S32(const u8 *data)
205 p.X = readS32(&data[0]);
206 p.Y = readS32(&data[4]);
210 inline v3s32 readV3S32(const u8 *data)
213 p.X = readS32(&data[0]);
214 p.Y = readS32(&data[4]);
215 p.Z = readS32(&data[8]);
219 inline v2f readV2F1000(const u8 *data)
222 p.X = (float)readF1000(&data[0]);
223 p.Y = (float)readF1000(&data[4]);
227 inline v3f readV3F1000(const u8 *data)
230 p.X = (float)readF1000(&data[0]);
231 p.Y = (float)readF1000(&data[4]);
232 p.Z = (float)readF1000(&data[8]);
236 /////////////// write routines ////////////////
238 inline void writeU8(u8 *data, u8 i)
240 data[0] = (i >> 0) & 0xFF;
243 inline void writeS8(u8 *data, s8 i)
245 writeU8(data, (u8)i);
248 inline void writeS16(u8 *data, s16 i)
250 writeU16(data, (u16)i);
253 inline void writeS32(u8 *data, s32 i)
255 writeU32(data, (u32)i);
258 inline void writeS64(u8 *data, s64 i)
260 writeU64(data, (u64)i);
263 inline void writeF1000(u8 *data, f32 i)
265 assert(i >= F1000_MIN && i <= F1000_MAX);
266 writeS32(data, i * FIXEDPOINT_FACTOR);
269 inline void writeARGB8(u8 *data, video::SColor p)
271 writeU32(data, p.color);
274 inline void writeV2S16(u8 *data, v2s16 p)
276 writeS16(&data[0], p.X);
277 writeS16(&data[2], p.Y);
280 inline void writeV3S16(u8 *data, v3s16 p)
282 writeS16(&data[0], p.X);
283 writeS16(&data[2], p.Y);
284 writeS16(&data[4], p.Z);
287 inline void writeV2S32(u8 *data, v2s32 p)
289 writeS32(&data[0], p.X);
290 writeS32(&data[4], p.Y);
293 inline void writeV3S32(u8 *data, v3s32 p)
295 writeS32(&data[0], p.X);
296 writeS32(&data[4], p.Y);
297 writeS32(&data[8], p.Z);
300 inline void writeV2F1000(u8 *data, v2f p)
302 writeF1000(&data[0], p.X);
303 writeF1000(&data[4], p.Y);
306 inline void writeV3F1000(u8 *data, v3f p)
308 writeF1000(&data[0], p.X);
309 writeF1000(&data[4], p.Y);
310 writeF1000(&data[8], p.Z);
314 //// Iostream wrapper for data read/write
317 #define MAKE_STREAM_READ_FXN(T, N, S) \
318 inline T read ## N(std::istream &is) \
321 is.read(buf, sizeof(buf)); \
322 return read ## N((u8 *)buf); \
325 #define MAKE_STREAM_WRITE_FXN(T, N, S) \
326 inline void write ## N(std::ostream &os, T val) \
329 write ## N((u8 *)buf, val); \
330 os.write(buf, sizeof(buf)); \
333 MAKE_STREAM_READ_FXN(u8, U8, 1);
334 MAKE_STREAM_READ_FXN(u16, U16, 2);
335 MAKE_STREAM_READ_FXN(u32, U32, 4);
336 MAKE_STREAM_READ_FXN(u64, U64, 8);
337 MAKE_STREAM_READ_FXN(s8, S8, 1);
338 MAKE_STREAM_READ_FXN(s16, S16, 2);
339 MAKE_STREAM_READ_FXN(s32, S32, 4);
340 MAKE_STREAM_READ_FXN(s64, S64, 8);
341 MAKE_STREAM_READ_FXN(f32, F1000, 4);
342 MAKE_STREAM_READ_FXN(v2s16, V2S16, 4);
343 MAKE_STREAM_READ_FXN(v3s16, V3S16, 6);
344 MAKE_STREAM_READ_FXN(v2s32, V2S32, 8);
345 MAKE_STREAM_READ_FXN(v3s32, V3S32, 12);
346 MAKE_STREAM_READ_FXN(v2f, V2F1000, 8);
347 MAKE_STREAM_READ_FXN(v3f, V3F1000, 12);
348 MAKE_STREAM_READ_FXN(video::SColor, ARGB8, 4);
350 MAKE_STREAM_WRITE_FXN(u8, U8, 1);
351 MAKE_STREAM_WRITE_FXN(u16, U16, 2);
352 MAKE_STREAM_WRITE_FXN(u32, U32, 4);
353 MAKE_STREAM_WRITE_FXN(u64, U64, 8);
354 MAKE_STREAM_WRITE_FXN(s8, S8, 1);
355 MAKE_STREAM_WRITE_FXN(s16, S16, 2);
356 MAKE_STREAM_WRITE_FXN(s32, S32, 4);
357 MAKE_STREAM_WRITE_FXN(s64, S64, 8);
358 MAKE_STREAM_WRITE_FXN(f32, F1000, 4);
359 MAKE_STREAM_WRITE_FXN(v2s16, V2S16, 4);
360 MAKE_STREAM_WRITE_FXN(v3s16, V3S16, 6);
361 MAKE_STREAM_WRITE_FXN(v2s32, V2S32, 8);
362 MAKE_STREAM_WRITE_FXN(v3s32, V3S32, 12);
363 MAKE_STREAM_WRITE_FXN(v2f, V2F1000, 8);
364 MAKE_STREAM_WRITE_FXN(v3f, V3F1000, 12);
365 MAKE_STREAM_WRITE_FXN(video::SColor, ARGB8, 4);
368 //// More serialization stuff
371 // Creates a string with the length as the first two bytes
372 std::string serializeString(const std::string &plain);
374 // Creates a string with the length as the first two bytes from wide string
375 std::string serializeWideString(const std::wstring &plain);
377 // Reads a string with the length as the first two bytes
378 std::string deSerializeString(std::istream &is);
380 // Reads a wide string with the length as the first two bytes
381 std::wstring deSerializeWideString(std::istream &is);
383 // Creates a string with the length as the first four bytes
384 std::string serializeLongString(const std::string &plain);
386 // Reads a string with the length as the first four bytes
387 std::string deSerializeLongString(std::istream &is);
389 // Creates a string encoded in JSON format (almost equivalent to a C string literal)
390 std::string serializeJsonString(const std::string &plain);
392 // Reads a string encoded in JSON format
393 std::string deSerializeJsonString(std::istream &is);
395 // Creates a string consisting of the hexadecimal representation of `data`
396 std::string serializeHexString(const std::string &data, bool insert_spaces=false);
398 // Creates a string containing comma delimited values of a struct whose layout is
399 // described by the parameter format
400 bool serializeStructToString(std::string *out,
401 std::string format, void *value);
403 // Reads a comma delimited string of values into a struct whose layout is
404 // decribed by the parameter format
405 bool deSerializeStringToStruct(std::string valstr,
406 std::string format, void *out, size_t olen);