Clean up util/serialization.{cpp,h} and add unit tests
[oweals/minetest.git] / src / util / serialize.h
1 /*
2 Minetest
3 Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
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.
9
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.
14
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.
18 */
19
20 #ifndef UTIL_SERIALIZE_HEADER
21 #define UTIL_SERIALIZE_HEADER
22
23 #include "../irrlichttypes_bloated.h"
24 #include "config.h"
25 #if HAVE_ENDIAN_H
26 #include <endian.h>
27 #include <string.h> // for memcpy
28 #endif
29 #include <iostream>
30 #include <string>
31
32 #define FIXEDPOINT_FACTOR 1000.0f
33 #define FIXEDPOINT_INVFACTOR (1.0f/FIXEDPOINT_FACTOR)
34
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)
39
40
41 #if HAVE_ENDIAN_H
42 // use machine native byte swapping routines
43 // Note: memcpy below is optimized out by modern compilers
44
45 inline u16 readU16(const u8 *data)
46 {
47         u16 val;
48         memcpy(&val, data, 2);
49         return be16toh(val);
50 }
51
52 inline u32 readU32(const u8 *data)
53 {
54         u32 val;
55         memcpy(&val, data, 4);
56         return be32toh(val);
57 }
58
59 inline u64 readU64(const u8 *data)
60 {
61         u64 val;
62         memcpy(&val, data, 8);
63         return be64toh(val);
64 }
65
66 inline void writeU16(u8 *data, u16 i)
67 {
68         u16 val = htobe16(i);
69         memcpy(data, &val, 2);
70 }
71
72 inline void writeU32(u8 *data, u32 i)
73 {
74         u32 val = htobe32(i);
75         memcpy(data, &val, 4);
76 }
77
78 inline void writeU64(u8 *data, u64 i)
79 {
80         u64 val = htobe64(i);
81         memcpy(data, &val, 8);
82 }
83
84 #else
85 // generic byte-swapping implementation
86
87 inline u16 readU16(const u8 *data)
88 {
89         return
90                 ((u16)data[0] << 8) | ((u16)data[1] << 0);
91 }
92
93 inline u32 readU32(const u8 *data)
94 {
95         return
96                 ((u32)data[0] << 24) | ((u32)data[1] << 16) |
97                 ((u32)data[2] <<  8) | ((u32)data[3] <<  0);
98 }
99
100 inline u64 readU64(const u8 *data)
101 {
102         return
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);
107 }
108
109 inline void writeU16(u8 *data, u16 i)
110 {
111         data[0] = (i >> 8) & 0xFF;
112         data[1] = (i >> 0) & 0xFF;
113 }
114
115 inline void writeU32(u8 *data, u32 i)
116 {
117         data[0] = (i >> 24) & 0xFF;
118         data[1] = (i >> 16) & 0xFF;
119         data[2] = (i >>  8) & 0xFF;
120         data[3] = (i >>  0) & 0xFF;
121 }
122
123 inline void writeU64(u8 *data, u64 i)
124 {
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;
133 }
134
135 #endif // HAVE_ENDIAN_H
136
137 //////////////// read routines ////////////////
138
139 inline u8 readU8(const u8 *data)
140 {
141         return ((u8)data[0] << 0);
142 }
143
144 inline s8 readS8(const u8 *data)
145 {
146         return (s8)readU8(data);
147 }
148
149 inline s16 readS16(const u8 *data)
150 {
151         return (s16)readU16(data);
152 }
153
154 inline s32 readS32(const u8 *data)
155 {
156         return (s32)readU32(data);
157 }
158
159 inline s64 readS64(const u8 *data)
160 {
161         return (s64)readU64(data);
162 }
163
164 inline f32 readF1000(const u8 *data)
165 {
166         return (f32)readS32(data) * FIXEDPOINT_INVFACTOR;
167 }
168
169 inline video::SColor readARGB8(const u8 *data)
170 {
171         video::SColor p(readU32(data));
172         return p;
173 }
174
175 inline v2s16 readV2S16(const u8 *data)
176 {
177         v2s16 p;
178         p.X = readS16(&data[0]);
179         p.Y = readS16(&data[2]);
180         return p;
181 }
182
183 inline v3s16 readV3S16(const u8 *data)
184 {
185         v3s16 p;
186         p.X = readS16(&data[0]);
187         p.Y = readS16(&data[2]);
188         p.Z = readS16(&data[4]);
189         return p;
190 }
191
192 inline v2s32 readV2S32(const u8 *data)
193 {
194         v2s32 p;
195         p.X = readS32(&data[0]);
196         p.Y = readS32(&data[4]);
197         return p;
198 }
199
200 inline v3s32 readV3S32(const u8 *data)
201 {
202         v3s32 p;
203         p.X = readS32(&data[0]);
204         p.Y = readS32(&data[4]);
205         p.Z = readS32(&data[8]);
206         return p;
207 }
208
209 inline v2f readV2F1000(const u8 *data)
210 {
211         v2f p;
212         p.X = (float)readF1000(&data[0]);
213         p.Y = (float)readF1000(&data[4]);
214         return p;
215 }
216
217 inline v3f readV3F1000(const u8 *data)
218 {
219         v3f p;
220         p.X = (float)readF1000(&data[0]);
221         p.Y = (float)readF1000(&data[4]);
222         p.Z = (float)readF1000(&data[8]);
223         return p;
224 }
225
226 /////////////// write routines ////////////////
227
228 inline void writeU8(u8 *data, u8 i)
229 {
230         data[0] = (i >> 0) & 0xFF;
231 }
232
233 inline void writeS8(u8 *data, s8 i)
234 {
235         writeU8(data, (u8)i);
236 }
237
238 inline void writeS16(u8 *data, s16 i)
239 {
240         writeU16(data, (u16)i);
241 }
242
243 inline void writeS32(u8 *data, s32 i)
244 {
245         writeU32(data, (u32)i);
246 }
247
248 inline void writeS64(u8 *data, s64 i)
249 {
250         writeU64(data, (u64)i);
251 }
252
253 inline void writeF1000(u8 *data, f32 i)
254 {
255         writeS32(data, i * FIXEDPOINT_FACTOR);
256 }
257
258 inline void writeARGB8(u8 *data, video::SColor p)
259 {
260         writeU32(data, p.color);
261 }
262
263 inline void writeV2S16(u8 *data, v2s16 p)
264 {
265         writeS16(&data[0], p.X);
266         writeS16(&data[2], p.Y);
267 }
268
269 inline void writeV3S16(u8 *data, v3s16 p)
270 {
271         writeS16(&data[0], p.X);
272         writeS16(&data[2], p.Y);
273         writeS16(&data[4], p.Z);
274 }
275
276 inline void writeV2S32(u8 *data, v2s32 p)
277 {
278         writeS32(&data[0], p.X);
279         writeS32(&data[4], p.Y);
280 }
281
282 inline void writeV3S32(u8 *data, v3s32 p)
283 {
284         writeS32(&data[0], p.X);
285         writeS32(&data[4], p.Y);
286         writeS32(&data[8], p.Z);
287 }
288
289 inline void writeV2F1000(u8 *data, v2f p)
290 {
291         writeF1000(&data[0], p.X);
292         writeF1000(&data[4], p.Y);
293 }
294
295 inline void writeV3F1000(u8 *data, v3f p)
296 {
297         writeF1000(&data[0], p.X);
298         writeF1000(&data[4], p.Y);
299         writeF1000(&data[8], p.Z);
300 }
301
302 ////
303 //// Iostream wrapper for data read/write
304 ////
305
306 #define MAKE_STREAM_READ_FXN(T, N, S)    \
307         inline T read ## N(std::istream &is) \
308         {                                    \
309                 char buf[S] = {0};               \
310                 is.read(buf, sizeof(buf));       \
311                 return read ## N((u8 *)buf);     \
312         }
313
314 #define MAKE_STREAM_WRITE_FXN(T, N, S)              \
315         inline void write ## N(std::ostream &os, T val) \
316         {                                               \
317                 char buf[S];                                \
318                 write ## N((u8 *)buf, val);                 \
319                 os.write(buf, sizeof(buf));                 \
320         }
321
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);
338
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);
355
356 ////
357 //// More serialization stuff
358 ////
359
360 // Creates a string with the length as the first two bytes
361 std::string serializeString(const std::string &plain);
362
363 // Creates a string with the length as the first two bytes from wide string
364 std::string serializeWideString(const std::wstring &plain);
365
366 // Reads a string with the length as the first two bytes
367 std::string deSerializeString(std::istream &is);
368
369 // Reads a wide string with the length as the first two bytes
370 std::wstring deSerializeWideString(std::istream &is);
371
372 // Creates a string with the length as the first four bytes
373 std::string serializeLongString(const std::string &plain);
374
375 // Reads a string with the length as the first four bytes
376 std::string deSerializeLongString(std::istream &is);
377
378 // Creates a string encoded in JSON format (almost equivalent to a C string literal)
379 std::string serializeJsonString(const std::string &plain);
380
381 // Reads a string encoded in JSON format
382 std::string deSerializeJsonString(std::istream &is);
383
384 // Creates a string consisting of the hexadecimal representation of `data`
385 std::string serializeHexString(const std::string &data, bool insert_spaces=false);
386
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);
391
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);
396
397 #endif