aboutsummaryrefslogtreecommitdiff
path: root/src/server/shared/Packets/ByteBuffer.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/server/shared/Packets/ByteBuffer.h')
-rwxr-xr-xsrc/server/shared/Packets/ByteBuffer.h85
1 files changed, 60 insertions, 25 deletions
diff --git a/src/server/shared/Packets/ByteBuffer.h b/src/server/shared/Packets/ByteBuffer.h
index 3506913fc23..24ca412d6f2 100755
--- a/src/server/shared/Packets/ByteBuffer.h
+++ b/src/server/shared/Packets/ByteBuffer.h
@@ -29,22 +29,52 @@
class ByteBufferException
{
public:
- ByteBufferException(bool _add, size_t _pos, size_t _esize, size_t _size)
- : add(_add), pos(_pos), esize(_esize), size(_size)
+ ByteBufferException(size_t pos, size_t size, size_t valueSize)
+ : Pos(pos), Size(size), ValueSize(valueSize)
{
- PrintPosError();
}
- void PrintPosError() const
+ protected:
+ size_t Pos;
+ size_t Size;
+ size_t ValueSize;
+};
+
+class ByteBufferPositionException : public ByteBufferException
+{
+ public:
+ ByteBufferPositionException(bool add, size_t pos, size_t size, size_t valueSize)
+ : ByteBufferException(pos, size, valueSize), _add(add)
+ {
+ PrintError();
+ }
+
+ protected:
+ void PrintError() const
{
- sLog->outError("Attempted to %s in ByteBuffer (pos: " SIZEFMTD " size: "SIZEFMTD") value with size: " SIZEFMTD,
- (add ? "put" : "get"), pos, size, esize);
+ sLog->outError("Attempted to %s value with size: "SIZEFMTD" in ByteBuffer (pos: " SIZEFMTD " size: "SIZEFMTD") " ,
+ (_add ? "put" : "get"), ValueSize, Pos, Size);
}
+
private:
- bool add;
- size_t pos;
- size_t esize;
- size_t size;
+ bool _add;
+};
+
+class ByteBufferSourceException : public ByteBufferException
+{
+ public:
+ ByteBufferSourceException(size_t pos, size_t size, size_t valueSize)
+ : ByteBufferException(pos, size, valueSize)
+ {
+ PrintError();
+ }
+
+ protected:
+ void PrintError() const
+ {
+ sLog->outError("Attempted to put a %s in ByteBuffer (pos: "SIZEFMTD" size: "SIZEFMTD")",
+ (ValueSize > 0 ? "NULL-pointer" : "zero-sized value"), Pos, Size);
+ }
};
class BitStream
@@ -288,14 +318,17 @@ class ByteBuffer
ByteBuffer &operator<<(const std::string &value)
{
- append((uint8 const*)value.c_str(), value.length());
+ if (size_t len = value.length())
+ append((uint8 const*)value.c_str(), len);
append((uint8)0);
return *this;
}
ByteBuffer &operator<<(const char *str)
{
- append((uint8 const*)str, str ? strlen(str) : 0);
+ size_t len = 0;
+ if (str && (len = strlen(str)))
+ append((uint8 const*)str, len);
append((uint8)0);
return *this;
}
@@ -421,7 +454,7 @@ class ByteBuffer
void read_skip(size_t skip)
{
if (_rpos + skip > size())
- throw ByteBufferException(false, _rpos, skip, size());
+ throw ByteBufferPositionException(false, _rpos, skip, size());
_rpos += skip;
}
@@ -435,7 +468,7 @@ class ByteBuffer
template <typename T> T read(size_t pos) const
{
if (pos + sizeof(T) > size())
- throw ByteBufferException(false, pos, sizeof(T), size());
+ throw ByteBufferPositionException(false, pos, sizeof(T), size());
T val = *((T const*)&_storage[pos]);
EndianConvert(val);
return val;
@@ -444,7 +477,7 @@ class ByteBuffer
void read(uint8 *dest, size_t len)
{
if (_rpos + len > size())
- throw ByteBufferException(false, _rpos, len, size());
+ throw ByteBufferPositionException(false, _rpos, len, size());
memcpy(dest, &_storage[_rpos], len);
_rpos += len;
}
@@ -452,7 +485,7 @@ class ByteBuffer
void readPackGUID(uint64& guid)
{
if (rpos() + 1 > size())
- throw ByteBufferException(false, _rpos, 1, size());
+ throw ByteBufferPositionException(false, _rpos, 1, size());
guid = 0;
@@ -464,7 +497,7 @@ class ByteBuffer
if (guidmark & (uint8(1) << i))
{
if (rpos() + 1 > size())
- throw ByteBufferException(false, _rpos, 1, size());
+ throw ByteBufferPositionException(false, _rpos, 1, size());
uint8 bit;
(*this) >> bit;
@@ -557,7 +590,7 @@ class ByteBuffer
void resize(size_t newsize)
{
- _storage.resize(newsize);
+ _storage.resize(newsize, 0);
_rpos = 0;
_wpos = size();
}
@@ -568,11 +601,6 @@ class ByteBuffer
_storage.reserve(ressize);
}
- void append(const std::string& str)
- {
- append((uint8 const*)str.c_str(), str.size() + 1);
- }
-
void append(const char *src, size_t cnt)
{
return append((const uint8 *)src, cnt);
@@ -586,7 +614,10 @@ class ByteBuffer
void append(const uint8 *src, size_t cnt)
{
if (!cnt)
- return;
+ throw ByteBufferSourceException(_wpos, size(), cnt);
+
+ if (!src)
+ throw ByteBufferSourceException(_wpos, size(), cnt);
ASSERT(size() < 10000000);
@@ -634,7 +665,11 @@ class ByteBuffer
void put(size_t pos, const uint8 *src, size_t cnt)
{
if (pos + cnt > size())
- throw ByteBufferException(true, pos, cnt, size());
+ throw ByteBufferPositionException(true, pos, cnt, size());
+
+ if (!src)
+ throw ByteBufferSourceException(_wpos, size(), cnt);
+
memcpy(&_storage[pos], src, cnt);
}