diff options
Diffstat (limited to 'src/util/serialize.cpp')
-rw-r--r-- | src/util/serialize.cpp | 73 |
1 files changed, 72 insertions, 1 deletions
diff --git a/src/util/serialize.cpp b/src/util/serialize.cpp index c0168776e..99cb990f1 100644 --- a/src/util/serialize.cpp +++ b/src/util/serialize.cpp @@ -28,6 +28,77 @@ with this program; if not, write to the Free Software Foundation, Inc., #include <iomanip> #include <vector> +SerializationError eof_ser_err("Attempted read past end of data"); + +//// +//// BufReader +//// + +bool BufReader::getStringNoEx(std::string *val) +{ + u16 num_chars; + if (!getU16NoEx(&num_chars)) + return false; + + if (pos + num_chars > size) { + pos -= sizeof(num_chars); + return false; + } + + val->assign((const char *)data + pos, num_chars); + pos += num_chars; + + return true; +} + +bool BufReader::getWideStringNoEx(std::wstring *val) +{ + u16 num_chars; + if (!getU16NoEx(&num_chars)) + return false; + + if (pos + num_chars * 2 > size) { + pos -= sizeof(num_chars); + return false; + } + + for (size_t i = 0; i != num_chars; i++) { + val->push_back(readU16(data + pos)); + pos += 2; + } + + return true; +} + +bool BufReader::getLongStringNoEx(std::string *val) +{ + u32 num_chars; + if (!getU32NoEx(&num_chars)) + return false; + + if (pos + num_chars > size) { + pos -= sizeof(num_chars); + return false; + } + + val->assign((const char *)data + pos, num_chars); + pos += num_chars; + + return true; +} + +bool BufReader::getRawDataNoEx(void *val, size_t len) +{ + if (pos + len > size) + return false; + + memcpy(val, data + pos, len); + pos += len; + + return true; +} + + //// //// String //// @@ -158,7 +229,7 @@ std::string deSerializeLongString(std::istream &is) Buffer<char> buf2(s_size); is.read(&buf2[0], s_size); - if (is.gcount() != s_size) + if ((u32)is.gcount() != s_size) throw SerializationError("deSerializeLongString: couldn't read all chars"); s.reserve(s_size); |