diff options
Diffstat (limited to 'src/server/shared/Packets/ByteBuffer.h')
| -rwxr-xr-x | src/server/shared/Packets/ByteBuffer.h | 179 |
1 files changed, 175 insertions, 4 deletions
diff --git a/src/server/shared/Packets/ByteBuffer.h b/src/server/shared/Packets/ByteBuffer.h index a4a996b299a..c970f32380e 100755 --- a/src/server/shared/Packets/ByteBuffer.h +++ b/src/server/shared/Packets/ByteBuffer.h @@ -24,6 +24,8 @@ #include "Logging/Log.h" #include "Utilities/ByteConverter.h" + + class ByteBufferException { public: @@ -45,6 +47,45 @@ class ByteBufferException size_t size; }; +class BitStream +{ + public: + BitStream(): _rpos(0), _wpos(0) {} + + BitStream(uint32 val, size_t len): _rpos(0), _wpos(0) + { + WriteBits(val, len); + } + + void Clear(); + uint8 GetBit(uint32 bit); + uint8 ReadBit(); + void WriteBit(uint32 bit); + template <typename T> void WriteBits(T value, size_t bits); + bool Emtpy (); + void Reverse(); + void Print(); + + size_t GetLenght () { return _data.size();} + uint32 GetReadPosition() { return _rpos; } + uint32 GetWritePosition() { return _wpos; } + void SetReadPos (uint32 pos) { _rpos = pos; } + + uint8 operator[](uint32 pos) const + { + return _data[pos]; + } + + uint8& operator[] (const uint32 pos) + { + return _data[pos]; + } + + private: + std::vector<uint8> _data; + uint32 _rpos, _wpos; +}; + class ByteBuffer { public: @@ -57,10 +98,7 @@ class ByteBuffer } // constructor - ByteBuffer(size_t res): _rpos(0), _wpos(0), _bitpos(8), _curbitval(0) - { - _storage.reserve(res); - } + ByteBuffer(size_t res, bool init = false); // copy constructor ByteBuffer(const ByteBuffer &buf): _rpos(buf._rpos), _wpos(buf._wpos), _storage(buf._storage), _bitpos(buf._bitpos), _curbitval(buf._curbitval) { } @@ -135,6 +173,14 @@ class ByteBuffer return value; } + BitStream* ReadBitStream(uint32 len) + { + BitStream* b = new BitStream(); + for (uint32 i = 0; i < len; ++i) + b->WriteBit(readBit()); + return b; + } + template <typename T> void put(size_t pos, T value) { EndianConvert(value); @@ -302,6 +348,13 @@ class ByteBuffer return read<uint8>(pos); } + uint8& operator[] (const size_t pos) + { + if (pos >= size()) + throw ByteBufferException(false, pos, 1, size()); + return _storage[pos]; + } + size_t rpos() const { return _rpos; } size_t rpos(size_t rpos_) @@ -381,6 +434,83 @@ class ByteBuffer } } + uint8 ReadUInt8() + { + uint8 u = 0; + (*this) >> u; + return u; + } + + uint16 ReadUInt16() + { + uint16 u = 0; + (*this) >> u; + return u; + } + + uint32 ReadUInt32() + { + uint32 u = 0; + (*this) >> u; + return u; + } + + uint64 ReadUInt64() + { + uint64 u = 0; + (*this) >> u; + return u; + } + + int8 ReadInt8() + { + int8 u = 0; + (*this) >> u; + return u; + } + + int16 ReadInt16() + { + int16 u = 0; + (*this) >> u; + return u; + } + + int32 ReadInt32() + { + uint32 u = 0; + (*this) >> u; + return u; + } + + int64 ReadInt64() + { + int64 u = 0; + (*this) >> u; + return u; + } + + std::string ReadString() + { + std::string s = 0; + (*this) >> s; + return s; + } + + bool ReadBoolean() + { + uint8 b = 0; + (*this) >> b; + return b > 0 ? true : false; + } + + float ReadSingle() + { + float f = 0; + (*this) >> f; + return f; + } + const uint8 *contents() const { return &_storage[0]; } size_t size() const { return _storage.size(); } @@ -657,5 +787,46 @@ inline void ByteBuffer::read_skip<std::string>() { read_skip<char*>(); } + +class BitConverter +{ + public: + + static uint8 ToUInt8(ByteBuffer buff, size_t start = 0) + { + return buff.read<uint8>(start); + } + + static uint16 ToUInt16(ByteBuffer buff, size_t start = 0) + { + return buff.read<uint16>(start); + } + + static uint32 ToUInt32(ByteBuffer buff, size_t start = 0) + { + return buff.read<uint32>(start); + } + + static uint64 ToUInt64(ByteBuffer buff, size_t start = 0) + { + return buff.read<uint64>(start); + } + + static int16 ToInt16(ByteBuffer buff, size_t start = 0) + { + return buff.read<int16>(start); + } + + static int32 ToInt32(ByteBuffer buff, size_t start = 0) + { + return buff.read<int32>(start); + } + + static int64 ToInt64(ByteBuffer buff, size_t start = 0) + { + return buff.read<int64>(start); + } +}; + #endif |
