Skip to content

Commit

Permalink
Core/PacketIO: Improved bit functions - no longer writes each bit sep…
Browse files Browse the repository at this point in the history
…arately
  • Loading branch information
Shauren committed Dec 19, 2024
1 parent 5a1fe84 commit 5f212c6
Showing 1 changed file with 39 additions and 11 deletions.
50 changes: 39 additions & 11 deletions src/server/shared/Packets/ByteBuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -194,18 +194,50 @@ class TC_SHARED_API ByteBuffer
return ((_curbitval >> (7 - _bitpos)) & 1) != 0;
}

void WriteBits(std::size_t value, int32 bits)
void WriteBits(uint64 value, int32 bits)
{
for (int32 i = bits - 1; i >= 0; --i)
WriteBit((value >> i) & 1);
// remove bits that don't fit
value &= (1 << bits) - 1;

if (bits > int32(_bitpos))
{
// first write to fill bit buffer
_curbitval |= value >> (bits - _bitpos);
bits -= _bitpos;
_bitpos = 8; // required "unneccessary" write to avoid double flushing
append(&_curbitval, sizeof(_curbitval));

// then append as many full bytes as possible
while (bits >= 8)
{
bits -= 8;
append<uint8>(value >> bits);
}

// store remaining bits in the bit buffer
_bitpos = 8 - bits;
_curbitval = (value & ((1 << bits) - 1)) << _bitpos;
}
else
{
// entire value fits in the bit buffer
_bitpos -= bits;
_curbitval |= value << _bitpos;

if (_bitpos == 0)
{
_bitpos = 8;
append(&_curbitval, sizeof(_curbitval));
_curbitval = 0;
}
}
}

uint32 ReadBits(int32 bits)
{
uint32 value = 0;
for (int32 i = bits - 1; i >= 0; --i)
if (ReadBit())
value |= (1 << (i));
value |= uint32(ReadBit()) << i;

return value;
}
Expand Down Expand Up @@ -552,11 +584,6 @@ class TC_SHARED_API ByteBuffer
_storage.shrink_to_fit();
}

void append(char const* src, size_t cnt)
{
return append(reinterpret_cast<uint8 const*>(src), cnt);
}

template <typename T>
void append(T const* src, size_t cnt)
{
Expand Down Expand Up @@ -634,7 +661,8 @@ class TC_SHARED_API ByteBuffer
void hexlike() const;

protected:
size_t _rpos, _wpos, _bitpos;
size_t _rpos, _wpos;
uint8 _bitpos;
uint8 _curbitval;
std::vector<uint8> _storage;
};
Expand Down

0 comments on commit 5f212c6

Please sign in to comment.