]> git.lizzy.rs Git - minetest.git/blob - src/util/serialize.h
Add ItemStack key-value meta storage
[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 "../exceptions.h" // for SerializationError
25 #include "../debug.h" // for assert
26
27 #include "config.h"
28 #if HAVE_ENDIAN_H
29         #ifdef _WIN32
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>
37         #else
38                 #include <endian.h>
39         #endif
40 #endif
41 #include <string.h> // for memcpy
42 #include <iostream>
43 #include <string>
44 #include <vector>
45
46 #define FIXEDPOINT_FACTOR 1000.0f
47
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))
57
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)
62
63
64 #if HAVE_ENDIAN_H
65 // use machine native byte swapping routines
66 // Note: memcpy below is optimized out by modern compilers
67
68 inline u16 readU16(const u8 *data)
69 {
70         u16 val;
71         memcpy(&val, data, 2);
72         return be16toh(val);
73 }
74
75 inline u32 readU32(const u8 *data)
76 {
77         u32 val;
78         memcpy(&val, data, 4);
79         return be32toh(val);
80 }
81
82 inline u64 readU64(const u8 *data)
83 {
84         u64 val;
85         memcpy(&val, data, 8);
86         return be64toh(val);
87 }
88
89 inline void writeU16(u8 *data, u16 i)
90 {
91         u16 val = htobe16(i);
92         memcpy(data, &val, 2);
93 }
94
95 inline void writeU32(u8 *data, u32 i)
96 {
97         u32 val = htobe32(i);
98         memcpy(data, &val, 4);
99 }
100
101 inline void writeU64(u8 *data, u64 i)
102 {
103         u64 val = htobe64(i);
104         memcpy(data, &val, 8);
105 }
106
107 #else
108 // generic byte-swapping implementation
109
110 inline u16 readU16(const u8 *data)
111 {
112         return
113                 ((u16)data[0] << 8) | ((u16)data[1] << 0);
114 }
115
116 inline u32 readU32(const u8 *data)
117 {
118         return
119                 ((u32)data[0] << 24) | ((u32)data[1] << 16) |
120                 ((u32)data[2] <<  8) | ((u32)data[3] <<  0);
121 }
122
123 inline u64 readU64(const u8 *data)
124 {
125         return
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);
130 }
131
132 inline void writeU16(u8 *data, u16 i)
133 {
134         data[0] = (i >> 8) & 0xFF;
135         data[1] = (i >> 0) & 0xFF;
136 }
137
138 inline void writeU32(u8 *data, u32 i)
139 {
140         data[0] = (i >> 24) & 0xFF;
141         data[1] = (i >> 16) & 0xFF;
142         data[2] = (i >>  8) & 0xFF;
143         data[3] = (i >>  0) & 0xFF;
144 }
145
146 inline void writeU64(u8 *data, u64 i)
147 {
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;
156 }
157
158 #endif // HAVE_ENDIAN_H
159
160 //////////////// read routines ////////////////
161
162 inline u8 readU8(const u8 *data)
163 {
164         return ((u8)data[0] << 0);
165 }
166
167 inline s8 readS8(const u8 *data)
168 {
169         return (s8)readU8(data);
170 }
171
172 inline s16 readS16(const u8 *data)
173 {
174         return (s16)readU16(data);
175 }
176
177 inline s32 readS32(const u8 *data)
178 {
179         return (s32)readU32(data);
180 }
181
182 inline s64 readS64(const u8 *data)
183 {
184         return (s64)readU64(data);
185 }
186
187 inline f32 readF1000(const u8 *data)
188 {
189         return (f32)readS32(data) / FIXEDPOINT_FACTOR;
190 }
191
192 inline video::SColor readARGB8(const u8 *data)
193 {
194         video::SColor p(readU32(data));
195         return p;
196 }
197
198 inline v2s16 readV2S16(const u8 *data)
199 {
200         v2s16 p;
201         p.X = readS16(&data[0]);
202         p.Y = readS16(&data[2]);
203         return p;
204 }
205
206 inline v3s16 readV3S16(const u8 *data)
207 {
208         v3s16 p;
209         p.X = readS16(&data[0]);
210         p.Y = readS16(&data[2]);
211         p.Z = readS16(&data[4]);
212         return p;
213 }
214
215 inline v2s32 readV2S32(const u8 *data)
216 {
217         v2s32 p;
218         p.X = readS32(&data[0]);
219         p.Y = readS32(&data[4]);
220         return p;
221 }
222
223 inline v3s32 readV3S32(const u8 *data)
224 {
225         v3s32 p;
226         p.X = readS32(&data[0]);
227         p.Y = readS32(&data[4]);
228         p.Z = readS32(&data[8]);
229         return p;
230 }
231
232 inline v2f readV2F1000(const u8 *data)
233 {
234         v2f p;
235         p.X = (float)readF1000(&data[0]);
236         p.Y = (float)readF1000(&data[4]);
237         return p;
238 }
239
240 inline v3f readV3F1000(const u8 *data)
241 {
242         v3f p;
243         p.X = (float)readF1000(&data[0]);
244         p.Y = (float)readF1000(&data[4]);
245         p.Z = (float)readF1000(&data[8]);
246         return p;
247 }
248
249 /////////////// write routines ////////////////
250
251 inline void writeU8(u8 *data, u8 i)
252 {
253         data[0] = (i >> 0) & 0xFF;
254 }
255
256 inline void writeS8(u8 *data, s8 i)
257 {
258         writeU8(data, (u8)i);
259 }
260
261 inline void writeS16(u8 *data, s16 i)
262 {
263         writeU16(data, (u16)i);
264 }
265
266 inline void writeS32(u8 *data, s32 i)
267 {
268         writeU32(data, (u32)i);
269 }
270
271 inline void writeS64(u8 *data, s64 i)
272 {
273         writeU64(data, (u64)i);
274 }
275
276 inline void writeF1000(u8 *data, f32 i)
277 {
278         assert(i >= F1000_MIN && i <= F1000_MAX);
279         writeS32(data, i * FIXEDPOINT_FACTOR);
280 }
281
282 inline void writeARGB8(u8 *data, video::SColor p)
283 {
284         writeU32(data, p.color);
285 }
286
287 inline void writeV2S16(u8 *data, v2s16 p)
288 {
289         writeS16(&data[0], p.X);
290         writeS16(&data[2], p.Y);
291 }
292
293 inline void writeV3S16(u8 *data, v3s16 p)
294 {
295         writeS16(&data[0], p.X);
296         writeS16(&data[2], p.Y);
297         writeS16(&data[4], p.Z);
298 }
299
300 inline void writeV2S32(u8 *data, v2s32 p)
301 {
302         writeS32(&data[0], p.X);
303         writeS32(&data[4], p.Y);
304 }
305
306 inline void writeV3S32(u8 *data, v3s32 p)
307 {
308         writeS32(&data[0], p.X);
309         writeS32(&data[4], p.Y);
310         writeS32(&data[8], p.Z);
311 }
312
313 inline void writeV2F1000(u8 *data, v2f p)
314 {
315         writeF1000(&data[0], p.X);
316         writeF1000(&data[4], p.Y);
317 }
318
319 inline void writeV3F1000(u8 *data, v3f p)
320 {
321         writeF1000(&data[0], p.X);
322         writeF1000(&data[4], p.Y);
323         writeF1000(&data[8], p.Z);
324 }
325
326 ////
327 //// Iostream wrapper for data read/write
328 ////
329
330 #define MAKE_STREAM_READ_FXN(T, N, S)    \
331         inline T read ## N(std::istream &is) \
332         {                                    \
333                 char buf[S] = {0};               \
334                 is.read(buf, sizeof(buf));       \
335                 return read ## N((u8 *)buf);     \
336         }
337
338 #define MAKE_STREAM_WRITE_FXN(T, N, S)              \
339         inline void write ## N(std::ostream &os, T val) \
340         {                                               \
341                 char buf[S];                                \
342                 write ## N((u8 *)buf, val);                 \
343                 os.write(buf, sizeof(buf));                 \
344         }
345
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);
362
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);
379
380 ////
381 //// More serialization stuff
382 ////
383
384 // Creates a string with the length as the first two bytes
385 std::string serializeString(const std::string &plain);
386
387 // Creates a string with the length as the first two bytes from wide string
388 std::string serializeWideString(const std::wstring &plain);
389
390 // Reads a string with the length as the first two bytes
391 std::string deSerializeString(std::istream &is);
392
393 // Reads a wide string with the length as the first two bytes
394 std::wstring deSerializeWideString(std::istream &is);
395
396 // Creates a string with the length as the first four bytes
397 std::string serializeLongString(const std::string &plain);
398
399 // Reads a string with the length as the first four bytes
400 std::string deSerializeLongString(std::istream &is);
401
402 // Creates a string encoded in JSON format (almost equivalent to a C string literal)
403 std::string serializeJsonString(const std::string &plain);
404
405 // Reads a string encoded in JSON format
406 std::string deSerializeJsonString(std::istream &is);
407
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);
411
412 // Parses a string serialized by serializeJsonStringIfNeeded.
413 std::string deSerializeJsonStringIfNeeded(std::istream &is);
414
415 // Creates a string consisting of the hexadecimal representation of `data`
416 std::string serializeHexString(const std::string &data, bool insert_spaces=false);
417
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);
422
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);
427
428 ////
429 //// BufReader
430 ////
431
432 extern SerializationError eof_ser_err;
433
434 #define MAKE_BUFREADER_GETNOEX_FXN(T, N, S) \
435         inline bool get ## N ## NoEx(T *val)    \
436         {                                       \
437                 if (pos + S > size)                 \
438                         return false;                   \
439                 *val = read ## N(data + pos);       \
440                 pos += S;                           \
441                 return true;                        \
442         }
443
444 #define MAKE_BUFREADER_GET_FXN(T, N) \
445         inline T get ## N()              \
446         {                                \
447                 T val;                       \
448                 if (!get ## N ## NoEx(&val)) \
449                         throw eof_ser_err;       \
450                 return val;                  \
451         }
452
453 class BufReader {
454 public:
455         BufReader(const u8 *data_, size_t size_) :
456                 data(data_),
457                 size(size_),
458                 pos(0)
459         {
460         }
461
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);
478
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);
483
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);
503
504         inline void getRawData(void *val, size_t len)
505         {
506                 if (!getRawDataNoEx(val, len))
507                         throw eof_ser_err;
508         }
509
510         inline size_t remaining()
511         {
512                 assert(pos <= size);
513                 return size - pos;
514         }
515
516         const u8 *data;
517         size_t size;
518         size_t pos;
519 };
520
521 #undef MAKE_BUFREADER_GET_FXN
522 #undef MAKE_BUFREADER_GETNOEX_FXN
523
524
525 ////
526 //// Vector-based write routines
527 ////
528
529 inline void putU8(std::vector<u8> *dest, u8 val)
530 {
531         dest->push_back((val >> 0) & 0xFF);
532 }
533
534 inline void putU16(std::vector<u8> *dest, u16 val)
535 {
536         dest->push_back((val >> 8) & 0xFF);
537         dest->push_back((val >> 0) & 0xFF);
538 }
539
540 inline void putU32(std::vector<u8> *dest, u32 val)
541 {
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);
546 }
547
548 inline void putU64(std::vector<u8> *dest, u64 val)
549 {
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);
558 }
559
560 inline void putS8(std::vector<u8> *dest, s8 val)
561 {
562         putU8(dest, val);
563 }
564
565 inline void putS16(std::vector<u8> *dest, s16 val)
566 {
567         putU16(dest, val);
568 }
569
570 inline void putS32(std::vector<u8> *dest, s32 val)
571 {
572         putU32(dest, val);
573 }
574
575 inline void putS64(std::vector<u8> *dest, s64 val)
576 {
577         putU64(dest, val);
578 }
579
580 inline void putF1000(std::vector<u8> *dest, f32 val)
581 {
582         putS32(dest, val * FIXEDPOINT_FACTOR);
583 }
584
585 inline void putV2S16(std::vector<u8> *dest, v2s16 val)
586 {
587         putS16(dest, val.X);
588         putS16(dest, val.Y);
589 }
590
591 inline void putV3S16(std::vector<u8> *dest, v3s16 val)
592 {
593         putS16(dest, val.X);
594         putS16(dest, val.Y);
595         putS16(dest, val.Z);
596 }
597
598 inline void putV2S32(std::vector<u8> *dest, v2s32 val)
599 {
600         putS32(dest, val.X);
601         putS32(dest, val.Y);
602 }
603
604 inline void putV3S32(std::vector<u8> *dest, v3s32 val)
605 {
606         putS32(dest, val.X);
607         putS32(dest, val.Y);
608         putS32(dest, val.Z);
609 }
610
611 inline void putV2F1000(std::vector<u8> *dest, v2f val)
612 {
613         putF1000(dest, val.X);
614         putF1000(dest, val.Y);
615 }
616
617 inline void putV3F1000(std::vector<u8> *dest, v3f val)
618 {
619         putF1000(dest, val.X);
620         putF1000(dest, val.Y);
621         putF1000(dest, val.Z);
622 }
623
624 inline void putARGB8(std::vector<u8> *dest, video::SColor val)
625 {
626         putU32(dest, val.color);
627 }
628
629 inline void putString(std::vector<u8> *dest, const std::string &val)
630 {
631         if (val.size() > STRING_MAX_LEN)
632                 throw SerializationError("String too long");
633
634         putU16(dest, val.size());
635         dest->insert(dest->end(), val.begin(), val.end());
636 }
637
638 inline void putWideString(std::vector<u8> *dest, const std::wstring &val)
639 {
640         if (val.size() > WIDE_STRING_MAX_LEN)
641                 throw SerializationError("String too long");
642
643         putU16(dest, val.size());
644         for (size_t i = 0; i != val.size(); i++)
645                 putU16(dest, val[i]);
646 }
647
648 inline void putLongString(std::vector<u8> *dest, const std::string &val)
649 {
650         if (val.size() > LONG_STRING_MAX_LEN)
651                 throw SerializationError("String too long");
652
653         putU32(dest, val.size());
654         dest->insert(dest->end(), val.begin(), val.end());
655 }
656
657 inline void putRawData(std::vector<u8> *dest, const void *src, size_t len)
658 {
659         dest->insert(dest->end(), (u8 *)src, (u8 *)src + len);
660 }
661
662 #endif