From 1a5b4b38f3457a8f1423305cef4e85d05da47d62 Mon Sep 17 00:00:00 2001 From: kwolekr Date: Thu, 6 Aug 2015 00:26:18 -0400 Subject: Add BufReader and vector-based serialization methods --- src/util/serialize.cpp | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) (limited to 'src/util/serialize.cpp') diff --git a/src/util/serialize.cpp b/src/util/serialize.cpp index c0168776e..ced5fc7cf 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 #include +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 //// -- cgit v1.2.3