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 #include "serialize.h"
23 #include "util/string.h"
24 #include "../exceptions.h"
25 #include "../irrlichttypes.h"
35 std::string serializeString(const std::string &plain)
40 if (plain.size() > 65535)
41 throw SerializationError("String too long for serializeString");
43 writeU16((u8 *)&buf[0], plain.size());
50 std::string deSerializeString(std::istream &is)
57 throw SerializationError("deSerializeString: size not read");
59 u16 s_size = readU16((u8 *)buf);
63 Buffer<char> buf2(s_size);
64 is.read(&buf2[0], s_size);
65 if (is.gcount() != s_size)
66 throw SerializationError("deSerializeString: couldn't read all chars");
69 s.append(&buf2[0], s_size);
77 std::string serializeWideString(const std::wstring &plain)
82 if (plain.size() > 65535)
83 throw SerializationError("String too long for serializeString");
85 writeU16((u8 *)buf, plain.size());
88 for (u32 i = 0; i < plain.size(); i++) {
89 writeU16((u8 *)buf, plain[i]);
95 std::wstring deSerializeWideString(std::istream &is)
101 if (is.gcount() != 2)
102 throw SerializationError("deSerializeString: size not read");
104 u16 s_size = readU16((u8 *)buf);
109 for (u32 i = 0; i < s_size; i++) {
111 if (is.gcount() != 2) {
112 throw SerializationError(
113 "deSerializeWideString: couldn't read all chars");
116 wchar_t c16 = readU16((u8 *)buf);
126 std::string serializeLongString(const std::string &plain)
129 writeU32((u8*)&buf[0], plain.size());
136 std::string deSerializeLongString(std::istream &is)
142 if (is.gcount() != 4)
143 throw SerializationError("deSerializeLongString: size not read");
145 u32 s_size = readU32((u8 *)buf);
149 // We don't really want a remote attacker to force us to allocate 4GB...
150 if (s_size > LONG_STRING_MAX)
151 throw SerializationError("deSerializeLongString: string too long");
153 Buffer<char> buf2(s_size);
154 is.read(&buf2[0], s_size);
155 if (is.gcount() != s_size)
156 throw SerializationError("deSerializeString: couldn't read all chars");
159 s.append(&buf2[0], s_size);
167 std::string serializeJsonString(const std::string &plain)
169 std::ostringstream os(std::ios::binary);
172 for (size_t i = 0; i < plain.size(); i++) {
200 if (c >= 32 && c <= 126) {
204 os << "\\u" << std::hex << std::setw(4)
205 << std::setfill('0') << cnum;
216 std::string deSerializeJsonString(std::istream &is)
218 std::ostringstream os(std::ios::binary);
221 // Parse initial doublequote
224 throw SerializationError("JSON string must start with doublequote");
230 throw SerializationError("JSON string ended prematurely");
234 } else if (c == '\\') {
237 throw SerializationError("JSON string ended prematurely");
256 char hexdigits[4 + 1];
258 is.read(hexdigits, 4);
260 throw SerializationError("JSON string ended prematurely");
263 std::istringstream tmp_is(hexdigits, std::ios::binary);
264 tmp_is >> std::hex >> hexnumber;
265 os << (char)hexnumber;
281 //// String/Struct conversions
284 bool deSerializeStringToStruct(std::string valstr,
285 std::string format, void *out, size_t olen)
288 std::vector<std::string *> strs_alloced;
293 char *s = &valstr[0];
294 char *buf = new char[len];
297 char *fmtpos, *fmt = &format[0];
298 while ((f = strtok_r(fmt, ",", &fmtpos)) && s) {
301 bool is_unsigned = false;
305 width = (int)strtol(f + 1, &f, 10);
306 if (width && valtype == 's')
315 bufpos += PADDING(bufpos, u16);
316 if ((bufpos - buf) + sizeof(u16) <= len) {
318 *(u16 *)bufpos = (u16)strtoul(s, &s, 10);
320 *(s16 *)bufpos = (s16)strtol(s, &s, 10);
322 bufpos += sizeof(u16);
323 } else if (width == 32) {
324 bufpos += PADDING(bufpos, u32);
325 if ((bufpos - buf) + sizeof(u32) <= len) {
327 *(u32 *)bufpos = (u32)strtoul(s, &s, 10);
329 *(s32 *)bufpos = (s32)strtol(s, &s, 10);
331 bufpos += sizeof(u32);
332 } else if (width == 64) {
333 bufpos += PADDING(bufpos, u64);
334 if ((bufpos - buf) + sizeof(u64) <= len) {
336 *(u64 *)bufpos = (u64)strtoull(s, &s, 10);
338 *(s64 *)bufpos = (s64)strtoll(s, &s, 10);
340 bufpos += sizeof(u64);
345 snext = strchr(s, ',');
349 bufpos += PADDING(bufpos, bool);
350 if ((bufpos - buf) + sizeof(bool) <= len)
351 *(bool *)bufpos = is_yes(std::string(s));
352 bufpos += sizeof(bool);
357 bufpos += PADDING(bufpos, float);
358 if ((bufpos - buf) + sizeof(float) <= len)
359 *(float *)bufpos = strtof(s, &s);
360 bufpos += sizeof(float);
365 while (*s == ' ' || *s == '\t')
367 if (*s++ != '"') //error, expected string
371 while (snext[0] && !(snext[-1] != '\\' && snext[0] == '"'))
375 bufpos += PADDING(bufpos, std::string *);
377 str = new std::string(s);
379 while ((pos = str->find("\\\"", pos)) != std::string::npos)
382 if ((bufpos - buf) + sizeof(std::string *) <= len)
383 *(std::string **)bufpos = str;
384 bufpos += sizeof(std::string *);
385 strs_alloced.push_back(str);
387 s = *snext ? snext + 1 : NULL;
390 while (*s == ' ' || *s == '\t')
392 if (*s++ != '(') //error, expected vector
396 bufpos += PADDING(bufpos, v2f);
398 if ((bufpos - buf) + sizeof(v2f) <= len) {
399 v2f *v = (v2f *)bufpos;
400 v->X = strtof(s, &s);
402 v->Y = strtof(s, &s);
405 bufpos += sizeof(v2f);
406 } else if (width == 3) {
407 bufpos += PADDING(bufpos, v3f);
408 if ((bufpos - buf) + sizeof(v3f) <= len) {
409 v3f *v = (v3f *)bufpos;
410 v->X = strtof(s, &s);
412 v->Y = strtof(s, &s);
414 v->Z = strtof(s, &s);
417 bufpos += sizeof(v3f);
421 default: //error, invalid format specifier
428 if ((size_t)(bufpos - buf) > len) //error, buffer too small
432 if (f && *f) { //error, mismatched number of fields and values
434 for (size_t i = 0; i != strs_alloced.size(); i++)
435 delete strs_alloced[i];
440 memcpy(out, buf, olen);
445 // Casts *buf to a signed or unsigned fixed-width integer of 'w' width
446 #define SIGN_CAST(w, buf) (is_unsigned ? *((u##w *) buf) : *((s##w *) buf))
448 bool serializeStructToString(std::string *out,
449 std::string format, void *value)
451 std::ostringstream os;
456 char *bufpos = (char *) value;
457 char *fmtpos, *fmt = &format[0];
458 while ((f = strtok_r(fmt, ",", &fmtpos))) {
460 bool is_unsigned = false;
464 width = (int)strtol(f + 1, &f, 10);
465 if (width && valtype == 's')
474 bufpos += PADDING(bufpos, u16);
475 os << SIGN_CAST(16, bufpos);
476 bufpos += sizeof(u16);
477 } else if (width == 32) {
478 bufpos += PADDING(bufpos, u32);
479 os << SIGN_CAST(32, bufpos);
480 bufpos += sizeof(u32);
481 } else if (width == 64) {
482 bufpos += PADDING(bufpos, u64);
483 os << SIGN_CAST(64, bufpos);
484 bufpos += sizeof(u64);
488 bufpos += PADDING(bufpos, bool);
489 os << std::boolalpha << *((bool *) bufpos);
490 bufpos += sizeof(bool);
493 bufpos += PADDING(bufpos, float);
494 os << *((float *) bufpos);
495 bufpos += sizeof(float);
498 bufpos += PADDING(bufpos, std::string *);
499 str = **((std::string **) bufpos);
502 while ((strpos = str.find('"', strpos)) != std::string::npos) {
503 str.insert(strpos, 1, '\\');
508 bufpos += sizeof(std::string *);
512 bufpos += PADDING(bufpos, v2f);
513 v2f *v = (v2f *) bufpos;
514 os << '(' << v->X << ", " << v->Y << ')';
515 bufpos += sizeof(v2f);
517 bufpos += PADDING(bufpos, v3f);
518 v3f *v = (v3f *) bufpos;
519 os << '(' << v->X << ", " << v->Y << ", " << v->Z << ')';
520 bufpos += sizeof(v3f);
530 // Trim off the trailing comma and space
531 if (out->size() >= 2)
532 out->resize(out->size() - 2);
543 std::string serializeHexString(const std::string &data, bool insert_spaces)
546 result.reserve(data.size() * (2 + insert_spaces));
548 static const char hex_chars[] = "0123456789abcdef";
550 const size_t len = data.size();
551 for (size_t i = 0; i != len; i++) {
553 result.push_back(hex_chars[(byte >> 4) & 0x0F]);
554 result.push_back(hex_chars[(byte >> 0) & 0x0F]);
555 if (insert_spaces && i != len - 1)
556 result.push_back(' ');