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"
27 #include <string.h> // for memcpy
32 #define FIXEDPOINT_FACTOR 1000.0f
33 #define FIXEDPOINT_INVFACTOR (1.0f/FIXEDPOINT_FACTOR)
35 #define STRING_MAX_LEN 0xFFFF
36 #define WIDE_STRING_MAX_LEN 0xFFFF
37 // 64 MB ought to be enough for anybody - Billy G.
38 #define LONG_STRING_MAX_LEN (64 * 1024 * 1024)
42 // use machine native byte swapping routines
43 // Note: memcpy below is optimized out by modern compilers
45 inline u16 readU16(const u8 *data)
48 memcpy(&val, data, 2);
52 inline u32 readU32(const u8 *data)
55 memcpy(&val, data, 4);
59 inline u64 readU64(const u8 *data)
62 memcpy(&val, data, 8);
66 inline void writeU16(u8 *data, u16 i)
69 memcpy(data, &val, 2);
72 inline void writeU32(u8 *data, u32 i)
75 memcpy(data, &val, 4);
78 inline void writeU64(u8 *data, u64 i)
81 memcpy(data, &val, 8);
85 // generic byte-swapping implementation
87 inline u16 readU16(const u8 *data)
90 ((u16)data[0] << 8) | ((u16)data[1] << 0);
93 inline u32 readU32(const u8 *data)
96 ((u32)data[0] << 24) | ((u32)data[1] << 16) |
97 ((u32)data[2] << 8) | ((u32)data[3] << 0);
100 inline u64 readU64(const u8 *data)
103 ((u64)data[0] << 56) | ((u64)data[1] << 48) |
104 ((u64)data[2] << 40) | ((u64)data[3] << 32) |
105 ((u64)data[4] << 24) | ((u64)data[5] << 16) |
106 ((u64)data[6] << 8) | ((u64)data[7] << 0);
109 inline void writeU16(u8 *data, u16 i)
111 data[0] = (i >> 8) & 0xFF;
112 data[1] = (i >> 0) & 0xFF;
115 inline void writeU32(u8 *data, u32 i)
117 data[0] = (i >> 24) & 0xFF;
118 data[1] = (i >> 16) & 0xFF;
119 data[2] = (i >> 8) & 0xFF;
120 data[3] = (i >> 0) & 0xFF;
123 inline void writeU64(u8 *data, u64 i)
125 data[0] = (i >> 56) & 0xFF;
126 data[1] = (i >> 48) & 0xFF;
127 data[2] = (i >> 40) & 0xFF;
128 data[3] = (i >> 32) & 0xFF;
129 data[4] = (i >> 24) & 0xFF;
130 data[5] = (i >> 16) & 0xFF;
131 data[6] = (i >> 8) & 0xFF;
132 data[7] = (i >> 0) & 0xFF;
135 #endif // HAVE_ENDIAN_H
137 //////////////// read routines ////////////////
139 inline u8 readU8(const u8 *data)
141 return ((u8)data[0] << 0);
144 inline s8 readS8(const u8 *data)
146 return (s8)readU8(data);
149 inline s16 readS16(const u8 *data)
151 return (s16)readU16(data);
154 inline s32 readS32(const u8 *data)
156 return (s32)readU32(data);
159 inline s64 readS64(const u8 *data)
161 return (s64)readU64(data);
164 inline f32 readF1000(const u8 *data)
166 return (f32)readS32(data) * FIXEDPOINT_INVFACTOR;
169 inline video::SColor readARGB8(const u8 *data)
171 video::SColor p(readU32(data));
175 inline v2s16 readV2S16(const u8 *data)
178 p.X = readS16(&data[0]);
179 p.Y = readS16(&data[2]);
183 inline v3s16 readV3S16(const u8 *data)
186 p.X = readS16(&data[0]);
187 p.Y = readS16(&data[2]);
188 p.Z = readS16(&data[4]);
192 inline v2s32 readV2S32(const u8 *data)
195 p.X = readS32(&data[0]);
196 p.Y = readS32(&data[4]);
200 inline v3s32 readV3S32(const u8 *data)
203 p.X = readS32(&data[0]);
204 p.Y = readS32(&data[4]);
205 p.Z = readS32(&data[8]);
209 inline v2f readV2F1000(const u8 *data)
212 p.X = (float)readF1000(&data[0]);
213 p.Y = (float)readF1000(&data[4]);
217 inline v3f readV3F1000(const u8 *data)
220 p.X = (float)readF1000(&data[0]);
221 p.Y = (float)readF1000(&data[4]);
222 p.Z = (float)readF1000(&data[8]);
226 /////////////// write routines ////////////////
228 inline void writeU8(u8 *data, u8 i)
230 data[0] = (i >> 0) & 0xFF;
233 inline void writeS8(u8 *data, s8 i)
235 writeU8(data, (u8)i);
238 inline void writeS16(u8 *data, s16 i)
240 writeU16(data, (u16)i);
243 inline void writeS32(u8 *data, s32 i)
245 writeU32(data, (u32)i);
248 inline void writeS64(u8 *data, s64 i)
250 writeU64(data, (u64)i);
253 inline void writeF1000(u8 *data, f32 i)
255 writeS32(data, i * FIXEDPOINT_FACTOR);
258 inline void writeARGB8(u8 *data, video::SColor p)
260 writeU32(data, p.color);
263 inline void writeV2S16(u8 *data, v2s16 p)
265 writeS16(&data[0], p.X);
266 writeS16(&data[2], p.Y);
269 inline void writeV3S16(u8 *data, v3s16 p)
271 writeS16(&data[0], p.X);
272 writeS16(&data[2], p.Y);
273 writeS16(&data[4], p.Z);
276 inline void writeV2S32(u8 *data, v2s32 p)
278 writeS32(&data[0], p.X);
279 writeS32(&data[4], p.Y);
282 inline void writeV3S32(u8 *data, v3s32 p)
284 writeS32(&data[0], p.X);
285 writeS32(&data[4], p.Y);
286 writeS32(&data[8], p.Z);
289 inline void writeV2F1000(u8 *data, v2f p)
291 writeF1000(&data[0], p.X);
292 writeF1000(&data[4], p.Y);
295 inline void writeV3F1000(u8 *data, v3f p)
297 writeF1000(&data[0], p.X);
298 writeF1000(&data[4], p.Y);
299 writeF1000(&data[8], p.Z);
303 //// Iostream wrapper for data read/write
306 #define MAKE_STREAM_READ_FXN(T, N, S) \
307 inline T read ## N(std::istream &is) \
310 is.read(buf, sizeof(buf)); \
311 return read ## N((u8 *)buf); \
314 #define MAKE_STREAM_WRITE_FXN(T, N, S) \
315 inline void write ## N(std::ostream &os, T val) \
318 write ## N((u8 *)buf, val); \
319 os.write(buf, sizeof(buf)); \
322 MAKE_STREAM_READ_FXN(u8, U8, 1);
323 MAKE_STREAM_READ_FXN(u16, U16, 2);
324 MAKE_STREAM_READ_FXN(u32, U32, 4);
325 MAKE_STREAM_READ_FXN(u64, U64, 8);
326 MAKE_STREAM_READ_FXN(s8, S8, 1);
327 MAKE_STREAM_READ_FXN(s16, S16, 2);
328 MAKE_STREAM_READ_FXN(s32, S32, 4);
329 MAKE_STREAM_READ_FXN(s64, S64, 8);
330 MAKE_STREAM_READ_FXN(f32, F1000, 4);
331 MAKE_STREAM_READ_FXN(v2s16, V2S16, 4);
332 MAKE_STREAM_READ_FXN(v3s16, V3S16, 6);
333 MAKE_STREAM_READ_FXN(v2s32, V2S32, 8);
334 MAKE_STREAM_READ_FXN(v3s32, V3S32, 12);
335 MAKE_STREAM_READ_FXN(v2f, V2F1000, 8);
336 MAKE_STREAM_READ_FXN(v3f, V3F1000, 12);
337 MAKE_STREAM_READ_FXN(video::SColor, ARGB8, 4);
339 MAKE_STREAM_WRITE_FXN(u8, U8, 1);
340 MAKE_STREAM_WRITE_FXN(u16, U16, 2);
341 MAKE_STREAM_WRITE_FXN(u32, U32, 4);
342 MAKE_STREAM_WRITE_FXN(u64, U64, 8);
343 MAKE_STREAM_WRITE_FXN(s8, S8, 1);
344 MAKE_STREAM_WRITE_FXN(s16, S16, 2);
345 MAKE_STREAM_WRITE_FXN(s32, S32, 4);
346 MAKE_STREAM_WRITE_FXN(s64, S64, 8);
347 MAKE_STREAM_WRITE_FXN(f32, F1000, 4);
348 MAKE_STREAM_WRITE_FXN(v2s16, V2S16, 4);
349 MAKE_STREAM_WRITE_FXN(v3s16, V3S16, 6);
350 MAKE_STREAM_WRITE_FXN(v2s32, V2S32, 8);
351 MAKE_STREAM_WRITE_FXN(v3s32, V3S32, 12);
352 MAKE_STREAM_WRITE_FXN(v2f, V2F1000, 8);
353 MAKE_STREAM_WRITE_FXN(v3f, V3F1000, 12);
354 MAKE_STREAM_WRITE_FXN(video::SColor, ARGB8, 4);
357 //// More serialization stuff
360 // Creates a string with the length as the first two bytes
361 std::string serializeString(const std::string &plain);
363 // Creates a string with the length as the first two bytes from wide string
364 std::string serializeWideString(const std::wstring &plain);
366 // Reads a string with the length as the first two bytes
367 std::string deSerializeString(std::istream &is);
369 // Reads a wide string with the length as the first two bytes
370 std::wstring deSerializeWideString(std::istream &is);
372 // Creates a string with the length as the first four bytes
373 std::string serializeLongString(const std::string &plain);
375 // Reads a string with the length as the first four bytes
376 std::string deSerializeLongString(std::istream &is);
378 // Creates a string encoded in JSON format (almost equivalent to a C string literal)
379 std::string serializeJsonString(const std::string &plain);
381 // Reads a string encoded in JSON format
382 std::string deSerializeJsonString(std::istream &is);
384 // Creates a string consisting of the hexadecimal representation of `data`
385 std::string serializeHexString(const std::string &data, bool insert_spaces=false);
387 // Creates a string containing comma delimited values of a struct whose layout is
388 // described by the parameter format
389 bool serializeStructToString(std::string *out,
390 std::string format, void *value);
392 // Reads a comma delimited string of values into a struct whose layout is
393 // decribed by the parameter format
394 bool deSerializeStringToStruct(std::string valstr,
395 std::string format, void *out, size_t olen);