]> git.lizzy.rs Git - minetest.git/blob - src/util/serialize.h
Use true pitch/yaw/roll rotations without loss of precision by pgimeno (#8019)
[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 #pragma once
21
22 #include "irrlichttypes_bloated.h"
23 #include "exceptions.h" // for SerializationError
24 #include "debug.h" // for assert
25 #include "ieee_float.h"
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__) || defined(__DragonFly__)
36                 #include <sys/endian.h>
37         #else
38                 #include <endian.h>
39         #endif
40 #endif
41 #include <cstring> // 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 extern FloatType g_serialize_f32_type;
65
66 #if HAVE_ENDIAN_H
67 // use machine native byte swapping routines
68 // Note: memcpy below is optimized out by modern compilers
69
70 inline u16 readU16(const u8 *data)
71 {
72         u16 val;
73         memcpy(&val, data, 2);
74         return be16toh(val);
75 }
76
77 inline u32 readU32(const u8 *data)
78 {
79         u32 val;
80         memcpy(&val, data, 4);
81         return be32toh(val);
82 }
83
84 inline u64 readU64(const u8 *data)
85 {
86         u64 val;
87         memcpy(&val, data, 8);
88         return be64toh(val);
89 }
90
91 inline void writeU16(u8 *data, u16 i)
92 {
93         u16 val = htobe16(i);
94         memcpy(data, &val, 2);
95 }
96
97 inline void writeU32(u8 *data, u32 i)
98 {
99         u32 val = htobe32(i);
100         memcpy(data, &val, 4);
101 }
102
103 inline void writeU64(u8 *data, u64 i)
104 {
105         u64 val = htobe64(i);
106         memcpy(data, &val, 8);
107 }
108
109 #else
110 // generic byte-swapping implementation
111
112 inline u16 readU16(const u8 *data)
113 {
114         return
115                 ((u16)data[0] << 8) | ((u16)data[1] << 0);
116 }
117
118 inline u32 readU32(const u8 *data)
119 {
120         return
121                 ((u32)data[0] << 24) | ((u32)data[1] << 16) |
122                 ((u32)data[2] <<  8) | ((u32)data[3] <<  0);
123 }
124
125 inline u64 readU64(const u8 *data)
126 {
127         return
128                 ((u64)data[0] << 56) | ((u64)data[1] << 48) |
129                 ((u64)data[2] << 40) | ((u64)data[3] << 32) |
130                 ((u64)data[4] << 24) | ((u64)data[5] << 16) |
131                 ((u64)data[6] <<  8) | ((u64)data[7] << 0);
132 }
133
134 inline void writeU16(u8 *data, u16 i)
135 {
136         data[0] = (i >> 8) & 0xFF;
137         data[1] = (i >> 0) & 0xFF;
138 }
139
140 inline void writeU32(u8 *data, u32 i)
141 {
142         data[0] = (i >> 24) & 0xFF;
143         data[1] = (i >> 16) & 0xFF;
144         data[2] = (i >>  8) & 0xFF;
145         data[3] = (i >>  0) & 0xFF;
146 }
147
148 inline void writeU64(u8 *data, u64 i)
149 {
150         data[0] = (i >> 56) & 0xFF;
151         data[1] = (i >> 48) & 0xFF;
152         data[2] = (i >> 40) & 0xFF;
153         data[3] = (i >> 32) & 0xFF;
154         data[4] = (i >> 24) & 0xFF;
155         data[5] = (i >> 16) & 0xFF;
156         data[6] = (i >>  8) & 0xFF;
157         data[7] = (i >>  0) & 0xFF;
158 }
159
160 #endif // HAVE_ENDIAN_H
161
162 //////////////// read routines ////////////////
163
164 inline u8 readU8(const u8 *data)
165 {
166         return ((u8)data[0] << 0);
167 }
168
169 inline s8 readS8(const u8 *data)
170 {
171         return (s8)readU8(data);
172 }
173
174 inline s16 readS16(const u8 *data)
175 {
176         return (s16)readU16(data);
177 }
178
179 inline s32 readS32(const u8 *data)
180 {
181         return (s32)readU32(data);
182 }
183
184 inline s64 readS64(const u8 *data)
185 {
186         return (s64)readU64(data);
187 }
188
189 inline f32 readF1000(const u8 *data)
190 {
191         return (f32)readS32(data) / FIXEDPOINT_FACTOR;
192 }
193
194 inline f32 readF32(const u8 *data)
195 {
196         u32 u = readU32(data);
197
198         switch (g_serialize_f32_type) {
199         case FLOATTYPE_SYSTEM: {
200                         f32 f;
201                         memcpy(&f, &u, 4);
202                         return f;
203                 }
204         case FLOATTYPE_SLOW:
205                 return u32Tof32Slow(u);
206         case FLOATTYPE_UNKNOWN: // First initialization
207                 g_serialize_f32_type = getFloatSerializationType();
208                 return readF32(data);
209         }
210         throw SerializationError("readF32: Unreachable code");
211 }
212
213 inline video::SColor readARGB8(const u8 *data)
214 {
215         video::SColor p(readU32(data));
216         return p;
217 }
218
219 inline v2s16 readV2S16(const u8 *data)
220 {
221         v2s16 p;
222         p.X = readS16(&data[0]);
223         p.Y = readS16(&data[2]);
224         return p;
225 }
226
227 inline v3s16 readV3S16(const u8 *data)
228 {
229         v3s16 p;
230         p.X = readS16(&data[0]);
231         p.Y = readS16(&data[2]);
232         p.Z = readS16(&data[4]);
233         return p;
234 }
235
236 inline v2s32 readV2S32(const u8 *data)
237 {
238         v2s32 p;
239         p.X = readS32(&data[0]);
240         p.Y = readS32(&data[4]);
241         return p;
242 }
243
244 inline v3s32 readV3S32(const u8 *data)
245 {
246         v3s32 p;
247         p.X = readS32(&data[0]);
248         p.Y = readS32(&data[4]);
249         p.Z = readS32(&data[8]);
250         return p;
251 }
252
253 inline v3f readV3F1000(const u8 *data)
254 {
255         v3f p;
256         p.X = readF1000(&data[0]);
257         p.Y = readF1000(&data[4]);
258         p.Z = readF1000(&data[8]);
259         return p;
260 }
261
262 inline v2f readV2F32(const u8 *data)
263 {
264         v2f p;
265         p.X = readF32(&data[0]);
266         p.Y = readF32(&data[4]);
267         return p;
268 }
269
270 inline v3f readV3F32(const u8 *data)
271 {
272         v3f p;
273         p.X = readF32(&data[0]);
274         p.Y = readF32(&data[4]);
275         p.Z = readF32(&data[8]);
276         return p;
277 }
278
279 /////////////// write routines ////////////////
280
281 inline void writeU8(u8 *data, u8 i)
282 {
283         data[0] = (i >> 0) & 0xFF;
284 }
285
286 inline void writeS8(u8 *data, s8 i)
287 {
288         writeU8(data, (u8)i);
289 }
290
291 inline void writeS16(u8 *data, s16 i)
292 {
293         writeU16(data, (u16)i); 
294 }
295
296 inline void writeS32(u8 *data, s32 i)
297 {
298         writeU32(data, (u32)i);
299 }
300
301 inline void writeS64(u8 *data, s64 i)
302 {
303         writeU64(data, (u64)i);
304 }
305
306 inline void writeF1000(u8 *data, f32 i)
307 {
308         assert(i >= F1000_MIN && i <= F1000_MAX);
309         writeS32(data, i * FIXEDPOINT_FACTOR);
310 }
311
312 inline void writeF32(u8 *data, f32 i)
313 {
314         switch (g_serialize_f32_type) {
315         case FLOATTYPE_SYSTEM: {
316                         u32 u;
317                         memcpy(&u, &i, 4);
318                         return writeU32(data, u);
319                 }
320         case FLOATTYPE_SLOW:
321                 return writeU32(data, f32Tou32Slow(i));
322         case FLOATTYPE_UNKNOWN: // First initialization
323                 g_serialize_f32_type = getFloatSerializationType();
324                 return writeF32(data, i);
325         }
326         throw SerializationError("writeF32: Unreachable code");
327 }
328
329 inline void writeARGB8(u8 *data, video::SColor p)
330 {
331         writeU32(data, p.color);
332 }
333
334 inline void writeV2S16(u8 *data, v2s16 p)
335 {
336         writeS16(&data[0], p.X);
337         writeS16(&data[2], p.Y);
338 }
339
340 inline void writeV3S16(u8 *data, v3s16 p)
341 {
342         writeS16(&data[0], p.X);
343         writeS16(&data[2], p.Y);
344         writeS16(&data[4], p.Z);
345 }
346
347 inline void writeV2S32(u8 *data, v2s32 p)
348 {
349         writeS32(&data[0], p.X);
350         writeS32(&data[4], p.Y);
351 }
352
353 inline void writeV3S32(u8 *data, v3s32 p)
354 {
355         writeS32(&data[0], p.X);
356         writeS32(&data[4], p.Y);
357         writeS32(&data[8], p.Z);
358 }
359
360 inline void writeV3F1000(u8 *data, v3f p)
361 {
362         writeF1000(&data[0], p.X);
363         writeF1000(&data[4], p.Y);
364         writeF1000(&data[8], p.Z);
365 }
366
367 inline void writeV2F32(u8 *data, v2f p)
368 {
369         writeF32(&data[0], p.X);
370         writeF32(&data[4], p.Y);
371 }
372
373 inline void writeV3F32(u8 *data, v3f p)
374 {
375         writeF32(&data[0], p.X);
376         writeF32(&data[4], p.Y);
377         writeF32(&data[8], p.Z);
378 }
379
380 ////
381 //// Iostream wrapper for data read/write
382 ////
383
384 #define MAKE_STREAM_READ_FXN(T, N, S)    \
385         inline T read ## N(std::istream &is) \
386         {                                    \
387                 char buf[S] = {0};               \
388                 is.read(buf, sizeof(buf));       \
389                 return read ## N((u8 *)buf);     \
390         }
391
392 #define MAKE_STREAM_WRITE_FXN(T, N, S)              \
393         inline void write ## N(std::ostream &os, T val) \
394         {                                               \
395                 char buf[S];                                \
396                 write ## N((u8 *)buf, val);                 \
397                 os.write(buf, sizeof(buf));                 \
398         }
399
400 MAKE_STREAM_READ_FXN(u8,    U8,       1);
401 MAKE_STREAM_READ_FXN(u16,   U16,      2);
402 MAKE_STREAM_READ_FXN(u32,   U32,      4);
403 MAKE_STREAM_READ_FXN(u64,   U64,      8);
404 MAKE_STREAM_READ_FXN(s8,    S8,       1);
405 MAKE_STREAM_READ_FXN(s16,   S16,      2);
406 MAKE_STREAM_READ_FXN(s32,   S32,      4);
407 MAKE_STREAM_READ_FXN(s64,   S64,      8);
408 MAKE_STREAM_READ_FXN(f32,   F1000,    4);
409 MAKE_STREAM_READ_FXN(f32,   F32,      4);
410 MAKE_STREAM_READ_FXN(v2s16, V2S16,    4);
411 MAKE_STREAM_READ_FXN(v3s16, V3S16,    6);
412 MAKE_STREAM_READ_FXN(v2s32, V2S32,    8);
413 MAKE_STREAM_READ_FXN(v3s32, V3S32,   12);
414 MAKE_STREAM_READ_FXN(v3f,   V3F1000, 12);
415 MAKE_STREAM_READ_FXN(v2f,   V2F32,    8);
416 MAKE_STREAM_READ_FXN(v3f,   V3F32,   12);
417 MAKE_STREAM_READ_FXN(video::SColor, ARGB8, 4);
418
419 MAKE_STREAM_WRITE_FXN(u8,    U8,       1);
420 MAKE_STREAM_WRITE_FXN(u16,   U16,      2);
421 MAKE_STREAM_WRITE_FXN(u32,   U32,      4);
422 MAKE_STREAM_WRITE_FXN(u64,   U64,      8);
423 MAKE_STREAM_WRITE_FXN(s8,    S8,       1);
424 MAKE_STREAM_WRITE_FXN(s16,   S16,      2);
425 MAKE_STREAM_WRITE_FXN(s32,   S32,      4);
426 MAKE_STREAM_WRITE_FXN(s64,   S64,      8);
427 MAKE_STREAM_WRITE_FXN(f32,   F1000,    4);
428 MAKE_STREAM_WRITE_FXN(f32,   F32,      4);
429 MAKE_STREAM_WRITE_FXN(v2s16, V2S16,    4);
430 MAKE_STREAM_WRITE_FXN(v3s16, V3S16,    6);
431 MAKE_STREAM_WRITE_FXN(v2s32, V2S32,    8);
432 MAKE_STREAM_WRITE_FXN(v3s32, V3S32,   12);
433 MAKE_STREAM_WRITE_FXN(v3f,   V3F1000, 12);
434 MAKE_STREAM_WRITE_FXN(v2f,   V2F32,    8);
435 MAKE_STREAM_WRITE_FXN(v3f,   V3F32,   12);
436 MAKE_STREAM_WRITE_FXN(video::SColor, ARGB8, 4);
437
438 ////
439 //// More serialization stuff
440 ////
441
442 // Creates a string with the length as the first two bytes
443 std::string serializeString(const std::string &plain);
444
445 // Creates a string with the length as the first two bytes from wide string
446 std::string serializeWideString(const std::wstring &plain);
447
448 // Reads a string with the length as the first two bytes
449 std::string deSerializeString(std::istream &is);
450
451 // Reads a wide string with the length as the first two bytes
452 std::wstring deSerializeWideString(std::istream &is);
453
454 // Creates a string with the length as the first four bytes
455 std::string serializeLongString(const std::string &plain);
456
457 // Reads a string with the length as the first four bytes
458 std::string deSerializeLongString(std::istream &is);
459
460 // Creates a string encoded in JSON format (almost equivalent to a C string literal)
461 std::string serializeJsonString(const std::string &plain);
462
463 // Reads a string encoded in JSON format
464 std::string deSerializeJsonString(std::istream &is);
465
466 // If the string contains spaces, quotes or control characters, encodes as JSON.
467 // Else returns the string unmodified.
468 std::string serializeJsonStringIfNeeded(const std::string &s);
469
470 // Parses a string serialized by serializeJsonStringIfNeeded.
471 std::string deSerializeJsonStringIfNeeded(std::istream &is);
472
473 // Creates a string consisting of the hexadecimal representation of `data`
474 std::string serializeHexString(const std::string &data, bool insert_spaces=false);
475
476 // Creates a string containing comma delimited values of a struct whose layout is
477 // described by the parameter format
478 bool serializeStructToString(std::string *out,
479         std::string format, void *value);
480
481 // Reads a comma delimited string of values into a struct whose layout is
482 // decribed by the parameter format
483 bool deSerializeStringToStruct(std::string valstr,
484         std::string format, void *out, size_t olen);
485
486 ////
487 //// BufReader
488 ////
489
490 #define MAKE_BUFREADER_GETNOEX_FXN(T, N, S) \
491         inline bool get ## N ## NoEx(T *val)    \
492         {                                       \
493                 if (pos + S > size)                 \
494                         return false;                   \
495                 *val = read ## N(data + pos);       \
496                 pos += S;                           \
497                 return true;                        \
498         }
499
500 #define MAKE_BUFREADER_GET_FXN(T, N) \
501         inline T get ## N()              \
502         {                                \
503                 T val;                       \
504                 if (!get ## N ## NoEx(&val)) \
505                         throw SerializationError("Attempted read past end of data"); \
506                 return val;                  \
507         }
508
509 class BufReader {
510 public:
511         BufReader(const u8 *data_, size_t size_) :
512                 data(data_),
513                 size(size_)
514         {
515         }
516
517         MAKE_BUFREADER_GETNOEX_FXN(u8,    U8,       1);
518         MAKE_BUFREADER_GETNOEX_FXN(u16,   U16,      2);
519         MAKE_BUFREADER_GETNOEX_FXN(u32,   U32,      4);
520         MAKE_BUFREADER_GETNOEX_FXN(u64,   U64,      8);
521         MAKE_BUFREADER_GETNOEX_FXN(s8,    S8,       1);
522         MAKE_BUFREADER_GETNOEX_FXN(s16,   S16,      2);
523         MAKE_BUFREADER_GETNOEX_FXN(s32,   S32,      4);
524         MAKE_BUFREADER_GETNOEX_FXN(s64,   S64,      8);
525         MAKE_BUFREADER_GETNOEX_FXN(f32,   F1000,    4);
526         MAKE_BUFREADER_GETNOEX_FXN(v2s16, V2S16,    4);
527         MAKE_BUFREADER_GETNOEX_FXN(v3s16, V3S16,    6);
528         MAKE_BUFREADER_GETNOEX_FXN(v2s32, V2S32,    8);
529         MAKE_BUFREADER_GETNOEX_FXN(v3s32, V3S32,   12);
530         MAKE_BUFREADER_GETNOEX_FXN(v3f,   V3F1000, 12);
531         MAKE_BUFREADER_GETNOEX_FXN(video::SColor, ARGB8, 4);
532
533         bool getStringNoEx(std::string *val);
534         bool getWideStringNoEx(std::wstring *val);
535         bool getLongStringNoEx(std::string *val);
536         bool getRawDataNoEx(void *data, size_t len);
537
538         MAKE_BUFREADER_GET_FXN(u8,            U8);
539         MAKE_BUFREADER_GET_FXN(u16,           U16);
540         MAKE_BUFREADER_GET_FXN(u32,           U32);
541         MAKE_BUFREADER_GET_FXN(u64,           U64);
542         MAKE_BUFREADER_GET_FXN(s8,            S8);
543         MAKE_BUFREADER_GET_FXN(s16,           S16);
544         MAKE_BUFREADER_GET_FXN(s32,           S32);
545         MAKE_BUFREADER_GET_FXN(s64,           S64);
546         MAKE_BUFREADER_GET_FXN(f32,           F1000);
547         MAKE_BUFREADER_GET_FXN(v2s16,         V2S16);
548         MAKE_BUFREADER_GET_FXN(v3s16,         V3S16);
549         MAKE_BUFREADER_GET_FXN(v2s32,         V2S32);
550         MAKE_BUFREADER_GET_FXN(v3s32,         V3S32);
551         MAKE_BUFREADER_GET_FXN(v3f,           V3F1000);
552         MAKE_BUFREADER_GET_FXN(video::SColor, ARGB8);
553         MAKE_BUFREADER_GET_FXN(std::string,   String);
554         MAKE_BUFREADER_GET_FXN(std::wstring,  WideString);
555         MAKE_BUFREADER_GET_FXN(std::string,   LongString);
556
557         inline void getRawData(void *val, size_t len)
558         {
559                 if (!getRawDataNoEx(val, len))
560                         throw SerializationError("Attempted read past end of data");
561         }
562
563         inline size_t remaining()
564         {
565                 assert(pos <= size);
566                 return size - pos;
567         }
568
569         const u8 *data;
570         size_t size;
571         size_t pos = 0;
572 };
573
574 #undef MAKE_BUFREADER_GET_FXN
575 #undef MAKE_BUFREADER_GETNOEX_FXN
576
577
578 ////
579 //// Vector-based write routines
580 ////
581
582 inline void putU8(std::vector<u8> *dest, u8 val)
583 {
584         dest->push_back((val >> 0) & 0xFF);
585 }
586
587 inline void putU16(std::vector<u8> *dest, u16 val)
588 {
589         dest->push_back((val >> 8) & 0xFF);
590         dest->push_back((val >> 0) & 0xFF);
591 }
592
593 inline void putU32(std::vector<u8> *dest, u32 val)
594 {
595         dest->push_back((val >> 24) & 0xFF);
596         dest->push_back((val >> 16) & 0xFF);
597         dest->push_back((val >>  8) & 0xFF);
598         dest->push_back((val >>  0) & 0xFF);
599 }
600
601 inline void putU64(std::vector<u8> *dest, u64 val)
602 {
603         dest->push_back((val >> 56) & 0xFF);
604         dest->push_back((val >> 48) & 0xFF);
605         dest->push_back((val >> 40) & 0xFF);
606         dest->push_back((val >> 32) & 0xFF);
607         dest->push_back((val >> 24) & 0xFF);
608         dest->push_back((val >> 16) & 0xFF);
609         dest->push_back((val >>  8) & 0xFF);
610         dest->push_back((val >>  0) & 0xFF);
611 }
612
613 inline void putS8(std::vector<u8> *dest, s8 val)
614 {
615         putU8(dest, val);
616 }
617
618 inline void putS16(std::vector<u8> *dest, s16 val)
619 {
620         putU16(dest, val);
621 }
622
623 inline void putS32(std::vector<u8> *dest, s32 val)
624 {
625         putU32(dest, val);
626 }
627
628 inline void putS64(std::vector<u8> *dest, s64 val)
629 {
630         putU64(dest, val);
631 }
632
633 inline void putF1000(std::vector<u8> *dest, f32 val)
634 {
635         putS32(dest, val * FIXEDPOINT_FACTOR);
636 }
637
638 inline void putV2S16(std::vector<u8> *dest, v2s16 val)
639 {
640         putS16(dest, val.X);
641         putS16(dest, val.Y);
642 }
643
644 inline void putV3S16(std::vector<u8> *dest, v3s16 val)
645 {
646         putS16(dest, val.X);
647         putS16(dest, val.Y);
648         putS16(dest, val.Z);
649 }
650
651 inline void putV2S32(std::vector<u8> *dest, v2s32 val)
652 {
653         putS32(dest, val.X);
654         putS32(dest, val.Y);
655 }
656
657 inline void putV3S32(std::vector<u8> *dest, v3s32 val)
658 {
659         putS32(dest, val.X);
660         putS32(dest, val.Y);
661         putS32(dest, val.Z);
662 }
663
664 inline void putV3F1000(std::vector<u8> *dest, v3f val)
665 {
666         putF1000(dest, val.X);
667         putF1000(dest, val.Y);
668         putF1000(dest, val.Z);
669 }
670
671 inline void putARGB8(std::vector<u8> *dest, video::SColor val)
672 {
673         putU32(dest, val.color);
674 }
675
676 inline void putString(std::vector<u8> *dest, const std::string &val)
677 {
678         if (val.size() > STRING_MAX_LEN)
679                 throw SerializationError("String too long");
680
681         putU16(dest, val.size());
682         dest->insert(dest->end(), val.begin(), val.end());
683 }
684
685 inline void putWideString(std::vector<u8> *dest, const std::wstring &val)
686 {
687         if (val.size() > WIDE_STRING_MAX_LEN)
688                 throw SerializationError("String too long");
689
690         putU16(dest, val.size());
691         for (size_t i = 0; i != val.size(); i++)
692                 putU16(dest, val[i]);
693 }
694
695 inline void putLongString(std::vector<u8> *dest, const std::string &val)
696 {
697         if (val.size() > LONG_STRING_MAX_LEN)
698                 throw SerializationError("String too long");
699
700         putU32(dest, val.size());
701         dest->insert(dest->end(), val.begin(), val.end());
702 }
703
704 inline void putRawData(std::vector<u8> *dest, const void *src, size_t len)
705 {
706         dest->insert(dest->end(), (u8 *)src, (u8 *)src + len);
707 }