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)
36 // use machine native byte swapping routines
37 // Note: memcpy below is optimized out by modern compilers
39 inline void writeU64(u8* data, u64 i)
42 memcpy(data, &val, 8);
45 inline void writeU32(u8* data, u32 i)
48 memcpy(data, &val, 4);
51 inline void writeU16(u8* data, u16 i)
54 memcpy(data, &val, 2);
57 inline u64 readU64(const u8* data)
60 memcpy(&val, data, 8);
64 inline u32 readU32(const u8* data)
67 memcpy(&val, data, 4);
71 inline u16 readU16(const u8* data)
74 memcpy(&val, data, 2);
79 // generic byte-swapping implementation
81 inline void writeU64(u8 *data, u64 i)
83 data[0] = ((i>>56)&0xff);
84 data[1] = ((i>>48)&0xff);
85 data[2] = ((i>>40)&0xff);
86 data[3] = ((i>>32)&0xff);
87 data[4] = ((i>>24)&0xff);
88 data[5] = ((i>>16)&0xff);
89 data[6] = ((i>> 8)&0xff);
90 data[7] = ((i>> 0)&0xff);
93 inline void writeU32(u8 *data, u32 i)
95 data[0] = ((i>>24)&0xff);
96 data[1] = ((i>>16)&0xff);
97 data[2] = ((i>> 8)&0xff);
98 data[3] = ((i>> 0)&0xff);
101 inline void writeU16(u8 *data, u16 i)
103 data[0] = ((i>> 8)&0xff);
104 data[1] = ((i>> 0)&0xff);
107 inline u64 readU64(const u8 *data)
109 return ((u64)data[0]<<56) | ((u64)data[1]<<48)
110 | ((u64)data[2]<<40) | ((u64)data[3]<<32)
111 | ((u64)data[4]<<24) | ((u64)data[5]<<16)
112 | ((u64)data[6]<<8) | ((u64)data[7]<<0);
115 inline u32 readU32(const u8 *data)
117 return (data[0]<<24) | (data[1]<<16) | (data[2]<<8) | (data[3]<<0);
120 inline u16 readU16(const u8 *data)
122 return (data[0]<<8) | (data[1]<<0);
127 inline void writeU8(u8 *data, u8 i)
129 data[0] = ((i>> 0)&0xff);
132 inline u8 readU8(const u8 *data)
137 inline void writeS32(u8 *data, s32 i){
138 writeU32(data, (u32)i);
140 inline s32 readS32(const u8 *data){
141 return (s32)readU32(data);
144 inline void writeS16(u8 *data, s16 i){
145 writeU16(data, (u16)i);
147 inline s16 readS16(const u8 *data){
148 return (s16)readU16(data);
151 inline void writeS8(u8 *data, s8 i){
152 writeU8(data, (u8)i);
154 inline s8 readS8(const u8 *data){
155 return (s8)readU8(data);
158 inline void writeF1000(u8 *data, f32 i){
159 writeS32(data, i*FIXEDPOINT_FACTOR);
161 inline f32 readF1000(const u8 *data){
162 return (f32)readS32(data)*FIXEDPOINT_INVFACTOR;
165 inline void writeV3S32(u8 *data, v3s32 p)
167 writeS32(&data[0], p.X);
168 writeS32(&data[4], p.Y);
169 writeS32(&data[8], p.Z);
171 inline v3s32 readV3S32(const u8 *data)
174 p.X = readS32(&data[0]);
175 p.Y = readS32(&data[4]);
176 p.Z = readS32(&data[8]);
180 inline void writeV3F1000(u8 *data, v3f p)
182 writeF1000(&data[0], p.X);
183 writeF1000(&data[4], p.Y);
184 writeF1000(&data[8], p.Z);
186 inline v3f readV3F1000(const u8 *data)
189 p.X = (float)readF1000(&data[0]);
190 p.Y = (float)readF1000(&data[4]);
191 p.Z = (float)readF1000(&data[8]);
195 inline void writeV2F1000(u8 *data, v2f p)
197 writeF1000(&data[0], p.X);
198 writeF1000(&data[4], p.Y);
200 inline v2f readV2F1000(const u8 *data)
203 p.X = (float)readF1000(&data[0]);
204 p.Y = (float)readF1000(&data[4]);
208 inline void writeV2S16(u8 *data, v2s16 p)
210 writeS16(&data[0], p.X);
211 writeS16(&data[2], p.Y);
214 inline v2s16 readV2S16(const u8 *data)
217 p.X = readS16(&data[0]);
218 p.Y = readS16(&data[2]);
222 inline void writeV2S32(u8 *data, v2s32 p)
224 writeS32(&data[0], p.X);
225 writeS32(&data[4], p.Y);
228 inline v2s32 readV2S32(const u8 *data)
231 p.X = readS32(&data[0]);
232 p.Y = readS32(&data[4]);
236 inline void writeV3S16(u8 *data, v3s16 p)
238 writeS16(&data[0], p.X);
239 writeS16(&data[2], p.Y);
240 writeS16(&data[4], p.Z);
243 inline v3s16 readV3S16(const u8 *data)
246 p.X = readS16(&data[0]);
247 p.Y = readS16(&data[2]);
248 p.Z = readS16(&data[4]);
252 inline void writeARGB8(u8 *data, video::SColor p)
254 writeU32(data, p.color);
257 inline video::SColor readARGB8(const u8 *data)
259 video::SColor p(readU32(data));
264 The above stuff directly interfaced to iostream
267 inline void writeU8(std::ostream &os, u8 p)
270 writeU8((u8*)buf, p);
273 inline u8 readU8(std::istream &is)
277 return readU8((u8*)buf);
280 inline void writeU16(std::ostream &os, u16 p)
283 writeU16((u8*)buf, p);
286 inline u16 readU16(std::istream &is)
290 return readU16((u8*)buf);
293 inline void writeU32(std::ostream &os, u32 p)
296 writeU32((u8*)buf, p);
299 inline u32 readU32(std::istream &is)
303 return readU32((u8*)buf);
306 inline void writeS32(std::ostream &os, s32 p)
308 writeU32(os, (u32) p);
310 inline s32 readS32(std::istream &is)
312 return (s32)readU32(is);
315 inline void writeS16(std::ostream &os, s16 p)
317 writeU16(os, (u16) p);
319 inline s16 readS16(std::istream &is)
321 return (s16)readU16(is);
324 inline void writeS8(std::ostream &os, s8 p)
328 inline s8 readS8(std::istream &is)
330 return (s8)readU8(is);
333 inline void writeF1000(std::ostream &os, f32 p)
336 writeF1000((u8*)buf, p);
339 inline f32 readF1000(std::istream &is)
343 return readF1000((u8*)buf);
346 inline void writeV3F1000(std::ostream &os, v3f p)
349 writeV3F1000((u8*)buf, p);
352 inline v3f readV3F1000(std::istream &is)
356 return readV3F1000((u8*)buf);
359 inline void writeV2F1000(std::ostream &os, v2f p)
362 writeV2F1000((u8*)buf, p);
365 inline v2f readV2F1000(std::istream &is)
369 return readV2F1000((u8*)buf);
372 inline void writeV2S16(std::ostream &os, v2s16 p)
375 writeV2S16((u8*)buf, p);
378 inline v2s16 readV2S16(std::istream &is)
382 return readV2S16((u8*)buf);
385 inline void writeV2S32(std::ostream &os, v2s32 p)
388 writeV2S32((u8*)buf, p);
391 inline v2s32 readV2S32(std::istream &is)
395 return readV2S32((u8*)buf);
398 inline void writeV3S16(std::ostream &os, v3s16 p)
401 writeV3S16((u8*)buf, p);
404 inline v3s16 readV3S16(std::istream &is)
408 return readV3S16((u8*)buf);
411 inline void writeARGB8(std::ostream &os, video::SColor p)
414 writeARGB8((u8*)buf, p);
418 inline video::SColor readARGB8(std::istream &is)
422 return readARGB8((u8*)buf);
426 More serialization stuff
429 // 8 MB is a conservative limit. Increase later if problematic.
430 #define LONG_STRING_MAX (8 * 1024 * 1024)
432 // Creates a string with the length as the first two bytes
433 std::string serializeString(const std::string &plain);
435 // Creates a string with the length as the first two bytes from wide string
436 std::string serializeWideString(const std::wstring &plain);
438 // Reads a string with the length as the first two bytes
439 std::string deSerializeString(std::istream &is);
441 // Reads a wide string with the length as the first two bytes
442 std::wstring deSerializeWideString(std::istream &is);
444 // Creates a string with the length as the first four bytes
445 std::string serializeLongString(const std::string &plain);
447 // Reads a string with the length as the first four bytes
448 std::string deSerializeLongString(std::istream &is);
450 // Creates a string encoded in JSON format (almost equivalent to a C string literal)
451 std::string serializeJsonString(const std::string &plain);
453 // Reads a string encoded in JSON format
454 std::string deSerializeJsonString(std::istream &is);
456 // Creates a string consisting of the hexadecimal representation of `data`
457 std::string serializeHexString(const std::string &data, bool insert_spaces=false);
459 // Creates a string containing comma delimited values of a struct whose layout is
460 // described by the parameter format
461 bool serializeStructToString(std::string *out,
462 std::string format, void *value);
464 // Reads a comma delimited string of values into a struct whose layout is
465 // decribed by the parameter format
466 bool deSerializeStringToStruct(std::string valstr,
467 std::string format, void *out, size_t olen);