2012-06-16 15:29:13 -07:00
|
|
|
/*
|
2013-02-24 09:40:43 -08:00
|
|
|
Minetest
|
2013-02-24 10:38:45 -08:00
|
|
|
Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
|
2012-06-16 15:29:13 -07:00
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2.1 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU Lesser General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public License along
|
|
|
|
with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "serialize.h"
|
2013-08-10 19:09:45 -07:00
|
|
|
#include "pointer.h"
|
2014-02-15 15:20:15 -08:00
|
|
|
#include "porting.h"
|
|
|
|
#include "util/string.h"
|
2017-11-08 14:56:20 -08:00
|
|
|
#include "exceptions.h"
|
|
|
|
#include "irrlichttypes.h"
|
2012-06-16 15:29:13 -07:00
|
|
|
|
|
|
|
#include <sstream>
|
|
|
|
#include <iomanip>
|
2014-02-15 15:20:15 -08:00
|
|
|
#include <vector>
|
2012-06-16 15:29:13 -07:00
|
|
|
|
2018-12-13 02:20:57 -08:00
|
|
|
FloatType g_serialize_f32_type = FLOATTYPE_UNKNOWN;
|
|
|
|
|
2015-08-05 21:26:18 -07:00
|
|
|
|
2015-07-11 14:48:05 -07:00
|
|
|
////
|
|
|
|
//// String
|
|
|
|
////
|
|
|
|
|
2013-08-10 19:09:45 -07:00
|
|
|
std::string serializeString(const std::string &plain)
|
|
|
|
{
|
|
|
|
std::string s;
|
2015-07-11 14:48:05 -07:00
|
|
|
char buf[2];
|
2013-08-10 19:09:45 -07:00
|
|
|
|
2015-07-31 21:34:09 -07:00
|
|
|
if (plain.size() > STRING_MAX_LEN)
|
2013-08-10 19:09:45 -07:00
|
|
|
throw SerializationError("String too long for serializeString");
|
2020-05-26 08:38:31 -07:00
|
|
|
s.reserve(2 + plain.size());
|
2015-07-11 14:48:05 -07:00
|
|
|
|
|
|
|
writeU16((u8 *)&buf[0], plain.size());
|
2013-08-10 19:09:45 -07:00
|
|
|
s.append(buf, 2);
|
2015-07-11 14:48:05 -07:00
|
|
|
|
|
|
|
s.append(plain);
|
2013-08-10 19:09:45 -07:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string deSerializeString(std::istream &is)
|
|
|
|
{
|
2015-07-11 14:48:05 -07:00
|
|
|
std::string s;
|
2013-08-10 19:09:45 -07:00
|
|
|
char buf[2];
|
2015-07-11 14:48:05 -07:00
|
|
|
|
2013-08-10 19:09:45 -07:00
|
|
|
is.read(buf, 2);
|
2015-07-11 14:48:05 -07:00
|
|
|
if (is.gcount() != 2)
|
2013-08-10 19:09:45 -07:00
|
|
|
throw SerializationError("deSerializeString: size not read");
|
2015-07-11 14:48:05 -07:00
|
|
|
|
|
|
|
u16 s_size = readU16((u8 *)buf);
|
|
|
|
if (s_size == 0)
|
2014-11-16 18:52:24 -08:00
|
|
|
return s;
|
2015-07-11 14:48:05 -07:00
|
|
|
|
2020-05-26 08:38:31 -07:00
|
|
|
s.resize(s_size);
|
|
|
|
is.read(&s[0], s_size);
|
2015-07-11 14:48:05 -07:00
|
|
|
if (is.gcount() != s_size)
|
|
|
|
throw SerializationError("deSerializeString: couldn't read all chars");
|
|
|
|
|
2013-08-10 19:09:45 -07:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2015-07-11 14:48:05 -07:00
|
|
|
////
|
|
|
|
//// Wide String
|
|
|
|
////
|
|
|
|
|
|
|
|
std::string serializeWideString(const std::wstring &plain)
|
|
|
|
{
|
|
|
|
std::string s;
|
|
|
|
char buf[2];
|
|
|
|
|
2015-07-31 21:34:09 -07:00
|
|
|
if (plain.size() > WIDE_STRING_MAX_LEN)
|
|
|
|
throw SerializationError("String too long for serializeWideString");
|
2020-05-26 08:38:31 -07:00
|
|
|
s.reserve(2 + 2 * plain.size());
|
2015-07-11 14:48:05 -07:00
|
|
|
|
|
|
|
writeU16((u8 *)buf, plain.size());
|
|
|
|
s.append(buf, 2);
|
|
|
|
|
2017-08-19 13:23:47 -07:00
|
|
|
for (wchar_t i : plain) {
|
|
|
|
writeU16((u8 *)buf, i);
|
2015-07-11 14:48:05 -07:00
|
|
|
s.append(buf, 2);
|
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2013-08-10 19:09:45 -07:00
|
|
|
std::wstring deSerializeWideString(std::istream &is)
|
|
|
|
{
|
2015-07-11 14:48:05 -07:00
|
|
|
std::wstring s;
|
2013-08-10 19:09:45 -07:00
|
|
|
char buf[2];
|
2015-07-11 14:48:05 -07:00
|
|
|
|
2013-08-10 19:09:45 -07:00
|
|
|
is.read(buf, 2);
|
2015-07-11 14:48:05 -07:00
|
|
|
if (is.gcount() != 2)
|
2015-07-31 21:34:09 -07:00
|
|
|
throw SerializationError("deSerializeWideString: size not read");
|
2015-07-11 14:48:05 -07:00
|
|
|
|
|
|
|
u16 s_size = readU16((u8 *)buf);
|
|
|
|
if (s_size == 0)
|
2014-11-16 18:52:24 -08:00
|
|
|
return s;
|
2015-07-11 14:48:05 -07:00
|
|
|
|
2013-08-10 19:09:45 -07:00
|
|
|
s.reserve(s_size);
|
2015-07-11 14:48:05 -07:00
|
|
|
for (u32 i = 0; i < s_size; i++) {
|
2013-08-10 19:09:45 -07:00
|
|
|
is.read(&buf[0], 2);
|
2015-07-11 14:48:05 -07:00
|
|
|
if (is.gcount() != 2) {
|
|
|
|
throw SerializationError(
|
|
|
|
"deSerializeWideString: couldn't read all chars");
|
|
|
|
}
|
|
|
|
|
|
|
|
wchar_t c16 = readU16((u8 *)buf);
|
2013-08-10 19:09:45 -07:00
|
|
|
s.append(&c16, 1);
|
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2015-07-11 14:48:05 -07:00
|
|
|
////
|
|
|
|
//// Long String
|
|
|
|
////
|
|
|
|
|
2013-08-10 19:09:45 -07:00
|
|
|
std::string serializeLongString(const std::string &plain)
|
|
|
|
{
|
2020-05-26 08:38:31 -07:00
|
|
|
std::string s;
|
2013-08-10 19:09:45 -07:00
|
|
|
char buf[4];
|
2015-07-14 00:22:16 -07:00
|
|
|
|
2015-07-31 21:34:09 -07:00
|
|
|
if (plain.size() > LONG_STRING_MAX_LEN)
|
2015-07-14 00:22:16 -07:00
|
|
|
throw SerializationError("String too long for serializeLongString");
|
2020-05-26 08:38:31 -07:00
|
|
|
s.reserve(4 + plain.size());
|
2015-07-14 00:22:16 -07:00
|
|
|
|
2013-08-10 19:09:45 -07:00
|
|
|
writeU32((u8*)&buf[0], plain.size());
|
|
|
|
s.append(buf, 4);
|
|
|
|
s.append(plain);
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string deSerializeLongString(std::istream &is)
|
|
|
|
{
|
2015-07-11 14:48:05 -07:00
|
|
|
std::string s;
|
2013-08-10 19:09:45 -07:00
|
|
|
char buf[4];
|
2015-07-11 14:48:05 -07:00
|
|
|
|
2013-08-10 19:09:45 -07:00
|
|
|
is.read(buf, 4);
|
2015-07-11 14:48:05 -07:00
|
|
|
if (is.gcount() != 4)
|
2013-08-10 19:09:45 -07:00
|
|
|
throw SerializationError("deSerializeLongString: size not read");
|
2015-07-11 14:48:05 -07:00
|
|
|
|
|
|
|
u32 s_size = readU32((u8 *)buf);
|
|
|
|
if (s_size == 0)
|
2014-11-16 18:52:24 -08:00
|
|
|
return s;
|
2015-07-11 14:48:05 -07:00
|
|
|
|
|
|
|
// We don't really want a remote attacker to force us to allocate 4GB...
|
2015-07-31 21:34:09 -07:00
|
|
|
if (s_size > LONG_STRING_MAX_LEN) {
|
2015-07-14 00:22:16 -07:00
|
|
|
throw SerializationError("deSerializeLongString: "
|
|
|
|
"string too long: " + itos(s_size) + " bytes");
|
|
|
|
}
|
2015-07-11 14:48:05 -07:00
|
|
|
|
2020-05-26 08:38:31 -07:00
|
|
|
s.resize(s_size);
|
|
|
|
is.read(&s[0], s_size);
|
2015-11-08 13:19:34 -08:00
|
|
|
if ((u32)is.gcount() != s_size)
|
2015-07-31 21:34:09 -07:00
|
|
|
throw SerializationError("deSerializeLongString: couldn't read all chars");
|
2015-07-11 14:48:05 -07:00
|
|
|
|
2013-08-10 19:09:45 -07:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2015-07-11 14:48:05 -07:00
|
|
|
////
|
|
|
|
//// JSON
|
|
|
|
////
|
|
|
|
|
2012-06-16 15:29:13 -07:00
|
|
|
std::string serializeJsonString(const std::string &plain)
|
|
|
|
{
|
|
|
|
std::ostringstream os(std::ios::binary);
|
2015-07-11 14:48:05 -07:00
|
|
|
os << "\"";
|
|
|
|
|
2017-08-19 13:23:47 -07:00
|
|
|
for (char c : plain) {
|
2015-07-11 14:48:05 -07:00
|
|
|
switch (c) {
|
|
|
|
case '"':
|
|
|
|
os << "\\\"";
|
|
|
|
break;
|
|
|
|
case '\\':
|
|
|
|
os << "\\\\";
|
|
|
|
break;
|
|
|
|
case '/':
|
|
|
|
os << "\\/";
|
|
|
|
break;
|
|
|
|
case '\b':
|
|
|
|
os << "\\b";
|
|
|
|
break;
|
|
|
|
case '\f':
|
|
|
|
os << "\\f";
|
|
|
|
break;
|
|
|
|
case '\n':
|
|
|
|
os << "\\n";
|
|
|
|
break;
|
|
|
|
case '\r':
|
|
|
|
os << "\\r";
|
|
|
|
break;
|
|
|
|
case '\t':
|
|
|
|
os << "\\t";
|
|
|
|
break;
|
|
|
|
default: {
|
|
|
|
if (c >= 32 && c <= 126) {
|
|
|
|
os << c;
|
|
|
|
} else {
|
|
|
|
u32 cnum = (u8)c;
|
|
|
|
os << "\\u" << std::hex << std::setw(4)
|
|
|
|
<< std::setfill('0') << cnum;
|
2012-06-16 15:29:13 -07:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-07-11 14:48:05 -07:00
|
|
|
|
|
|
|
os << "\"";
|
2012-06-16 15:29:13 -07:00
|
|
|
return os.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string deSerializeJsonString(std::istream &is)
|
|
|
|
{
|
|
|
|
std::ostringstream os(std::ios::binary);
|
|
|
|
char c, c2;
|
|
|
|
|
|
|
|
// Parse initial doublequote
|
|
|
|
is >> c;
|
2015-07-11 14:48:05 -07:00
|
|
|
if (c != '"')
|
2012-06-16 15:29:13 -07:00
|
|
|
throw SerializationError("JSON string must start with doublequote");
|
|
|
|
|
|
|
|
// Parse characters
|
2015-07-11 14:48:05 -07:00
|
|
|
for (;;) {
|
2012-06-16 15:29:13 -07:00
|
|
|
c = is.get();
|
2015-07-11 14:48:05 -07:00
|
|
|
if (is.eof())
|
2012-06-16 15:29:13 -07:00
|
|
|
throw SerializationError("JSON string ended prematurely");
|
2015-07-11 14:48:05 -07:00
|
|
|
|
|
|
|
if (c == '"') {
|
2012-06-16 15:29:13 -07:00
|
|
|
return os.str();
|
2017-08-19 13:23:47 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (c == '\\') {
|
2012-06-16 15:29:13 -07:00
|
|
|
c2 = is.get();
|
2015-07-11 14:48:05 -07:00
|
|
|
if (is.eof())
|
2012-06-16 15:29:13 -07:00
|
|
|
throw SerializationError("JSON string ended prematurely");
|
2015-07-11 14:48:05 -07:00
|
|
|
switch (c2) {
|
|
|
|
case 'b':
|
|
|
|
os << '\b';
|
|
|
|
break;
|
|
|
|
case 'f':
|
|
|
|
os << '\f';
|
|
|
|
break;
|
|
|
|
case 'n':
|
|
|
|
os << '\n';
|
|
|
|
break;
|
|
|
|
case 'r':
|
|
|
|
os << '\r';
|
|
|
|
break;
|
|
|
|
case 't':
|
|
|
|
os << '\t';
|
|
|
|
break;
|
|
|
|
case 'u': {
|
|
|
|
int hexnumber;
|
|
|
|
char hexdigits[4 + 1];
|
|
|
|
|
2012-06-16 15:29:13 -07:00
|
|
|
is.read(hexdigits, 4);
|
2015-07-11 14:48:05 -07:00
|
|
|
if (is.eof())
|
2012-06-16 15:29:13 -07:00
|
|
|
throw SerializationError("JSON string ended prematurely");
|
|
|
|
hexdigits[4] = 0;
|
2015-07-11 14:48:05 -07:00
|
|
|
|
2012-06-16 15:29:13 -07:00
|
|
|
std::istringstream tmp_is(hexdigits, std::ios::binary);
|
|
|
|
tmp_is >> std::hex >> hexnumber;
|
2015-07-11 14:48:05 -07:00
|
|
|
os << (char)hexnumber;
|
2012-06-16 15:29:13 -07:00
|
|
|
break;
|
|
|
|
}
|
2015-07-11 14:48:05 -07:00
|
|
|
default:
|
|
|
|
os << c2;
|
|
|
|
break;
|
2012-06-16 15:29:13 -07:00
|
|
|
}
|
2015-07-11 14:48:05 -07:00
|
|
|
} else {
|
|
|
|
os << c;
|
2012-06-16 15:29:13 -07:00
|
|
|
}
|
|
|
|
}
|
2015-07-11 14:48:05 -07:00
|
|
|
|
2012-06-16 15:29:13 -07:00
|
|
|
return os.str();
|
|
|
|
}
|
|
|
|
|
2017-01-31 11:49:01 -08:00
|
|
|
std::string serializeJsonStringIfNeeded(const std::string &s)
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < s.size(); ++i) {
|
|
|
|
if (s[i] <= 0x1f || s[i] >= 0x7f || s[i] == ' ' || s[i] == '\"')
|
|
|
|
return serializeJsonString(s);
|
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string deSerializeJsonStringIfNeeded(std::istream &is)
|
|
|
|
{
|
|
|
|
std::ostringstream tmp_os;
|
|
|
|
bool expect_initial_quote = true;
|
|
|
|
bool is_json = false;
|
|
|
|
bool was_backslash = false;
|
|
|
|
for (;;) {
|
|
|
|
char c = is.get();
|
|
|
|
if (is.eof())
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (expect_initial_quote && c == '"') {
|
|
|
|
tmp_os << c;
|
|
|
|
is_json = true;
|
|
|
|
} else if(is_json) {
|
|
|
|
tmp_os << c;
|
|
|
|
if (was_backslash)
|
|
|
|
was_backslash = false;
|
|
|
|
else if (c == '\\')
|
|
|
|
was_backslash = true;
|
|
|
|
else if (c == '"')
|
|
|
|
break; // Found end of string
|
|
|
|
} else {
|
|
|
|
if (c == ' ') {
|
|
|
|
// Found end of word
|
|
|
|
is.unget();
|
|
|
|
break;
|
|
|
|
}
|
2017-08-19 13:23:47 -07:00
|
|
|
|
|
|
|
tmp_os << c;
|
2017-01-31 11:49:01 -08:00
|
|
|
}
|
|
|
|
expect_initial_quote = false;
|
|
|
|
}
|
|
|
|
if (is_json) {
|
|
|
|
std::istringstream tmp_is(tmp_os.str(), std::ios::binary);
|
|
|
|
return deSerializeJsonString(tmp_is);
|
2017-08-19 13:23:47 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return tmp_os.str();
|
2017-01-31 11:49:01 -08:00
|
|
|
}
|
|
|
|
|