diff options
author | Shauren <shauren.trinity@gmail.com> | 2012-09-01 17:52:19 +0200 |
---|---|---|
committer | Shauren <shauren.trinity@gmail.com> | 2012-09-01 17:52:19 +0200 |
commit | d218afa19849e1be810bb06c96402cff956dc036 (patch) | |
tree | f1d0f1c7d31cdd4c76d19de5069ebfab583842fc /src | |
parent | 3d0c6a3f4a258840e536ab040e146788bb1a111f (diff) |
Core/PacketIO: Implemented ByteBuffer::PutBits allowing to put a value at specified position with bit precision
Diffstat (limited to 'src')
-rwxr-xr-x | src/server/shared/Packets/ByteBuffer.h | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/src/server/shared/Packets/ByteBuffer.h b/src/server/shared/Packets/ByteBuffer.h index 0b08b4118d4..bed09e8e372 100755 --- a/src/server/shared/Packets/ByteBuffer.h +++ b/src/server/shared/Packets/ByteBuffer.h @@ -189,6 +189,32 @@ class ByteBuffer put(pos, (uint8 *)&value, sizeof(value)); } + /** + * @name PutBits + * @brief Places specified amount of bits of value at specified position in packet + + * @param pos Position to place the value at, in bits. The entire value must fit in the packet + * It is advised to obtain the position using bitwpos() function. + + * @param value Data to write. + * @param bitCount Number of bits to store the value on. + */ + template <typename T> void PutBits(size_t pos, T value, uint32 bitCount) + { + if (pos + bitCount > size() * 8) + throw ByteBufferPositionException(false, (pos + bitCount) / 8, size()); + + for (int32 i = 0; i < bitCount; ++i) + { + size_t wp = (pos + i) / 8; + size_t bit = (pos + i) % 8; + if ((value >> (bitCount - i - 1)) & 1) + _storage[wp] |= 1 << (7 - bit); + else + _storage[wp] &= ~(1 << (7 - bit)); + } + } + ByteBuffer &operator<<(uint8 value) { append<uint8>(value); @@ -382,6 +408,16 @@ class ByteBuffer return _wpos; } + /// Returns position of last written bit + size_t bitwpos() const { return _wpos * 8 + 8 - _bitpos; } + + size_t bitwpos(size_t newPos) + { + _wpos = newPos / 8; + _bitpos = 8 - (newPos % 8); + return _wpos * 8 + 8 - _bitpos; + } + template<typename T> void read_skip() { read_skip(sizeof(T)); } |