AzerothCore 3.3.5a
OpenSource WoW Emulator
Loading...
Searching...
No Matches
ByteBuffer Class Reference

#include "ByteBuffer.h"

Inheritance diagram for ByteBuffer:
WorldPacket EncryptablePacket

Public Member Functions

 ByteBuffer ()
 
 ByteBuffer (size_t reserve)
 
 ByteBuffer (ByteBuffer &&buf) noexcept
 
 ByteBuffer (ByteBuffer const &right)=default
 
 ByteBuffer (MessageBuffer &&buffer)
 
virtual ~ByteBuffer ()=default
 
ByteBufferoperator= (ByteBuffer const &right)
 
ByteBufferoperator= (ByteBuffer &&right) noexcept
 
void clear ()
 
template<typename T >
void append (T value)
 
template<typename T >
void put (std::size_t pos, T value)
 
ByteBufferoperator<< (bool value)
 
ByteBufferoperator<< (uint8 value)
 
ByteBufferoperator<< (uint16 value)
 
ByteBufferoperator<< (uint32 value)
 
ByteBufferoperator<< (uint64 value)
 
ByteBufferoperator<< (int8 value)
 
ByteBufferoperator<< (int16 value)
 
ByteBufferoperator<< (int32 value)
 
ByteBufferoperator<< (int64 value)
 
ByteBufferoperator<< (float value)
 
ByteBufferoperator<< (double value)
 
ByteBufferoperator<< (std::string_view value)
 
ByteBufferoperator<< (std::string const &str)
 
ByteBufferoperator<< (char const *str)
 
ByteBufferoperator>> (bool &value)
 
ByteBufferoperator>> (uint8 &value)
 
ByteBufferoperator>> (uint16 &value)
 
ByteBufferoperator>> (uint32 &value)
 
ByteBufferoperator>> (uint64 &value)
 
ByteBufferoperator>> (int8 &value)
 
ByteBufferoperator>> (int16 &value)
 
ByteBufferoperator>> (int32 &value)
 
ByteBufferoperator>> (int64 &value)
 
ByteBufferoperator>> (float &value)
 
ByteBufferoperator>> (double &value)
 
ByteBufferoperator>> (std::string &value)
 
uint8operator[] (size_t const pos)
 
uint8 const & operator[] (size_t const pos) const
 
size_t rpos () const
 
size_t rpos (size_t rpos_)
 
void rfinish ()
 
size_t wpos () const
 
size_t wpos (size_t wpos_)
 
template<typename T >
void read_skip ()
 
void read_skip (size_t skip)
 
template<typename T >
read ()
 
template<typename T >
read (size_t pos) const
 
void read (uint8 *dest, size_t len)
 
template<size_t Size>
void read (std::array< uint8, Size > &arr)
 
void readPackGUID (uint64 &guid)
 
std::string ReadCString (bool requireValidUtf8=true)
 
uint32 ReadPackedTime ()
 
ByteBufferReadPackedTime (uint32 &time)
 
uint8contents ()
 
uint8 const * contents () const
 
size_t size () const
 
bool empty () const
 
void resize (size_t newsize)
 
void reserve (size_t ressize)
 
void shrink_to_fit ()
 
void append (const char *src, size_t cnt)
 
template<class T >
void append (const T *src, size_t cnt)
 
void append (uint8 const *src, size_t cnt)
 
void append (ByteBuffer const &buffer)
 
template<size_t Size>
void append (std::array< uint8, Size > const &arr)
 
void appendPackXYZ (float x, float y, float z)
 
void appendPackGUID (uint64 guid)
 
void AppendPackedTime (time_t time)
 
void put (size_t pos, const uint8 *src, size_t cnt)
 
void print_storage () const
 
void textlike () const
 
void hexlike () const
 
template<>
void read_skip ()
 
template<>
void read_skip ()
 

Static Public Attributes

static constexpr size_t DEFAULT_SIZE = 0x1000
 

Protected Attributes

size_t _rpos {0}
 
size_t _wpos {0}
 
std::vector< uint8_storage
 

Detailed Description

Constructor & Destructor Documentation

◆ ByteBuffer() [1/5]

ByteBuffer::ByteBuffer ( )
inline
76 {
77 _storage.reserve(DEFAULT_SIZE);
78 }
std::vector< uint8 > _storage
Definition: ByteBuffer.h:532
static constexpr size_t DEFAULT_SIZE
Definition: ByteBuffer.h:72

◆ ByteBuffer() [2/5]

ByteBuffer::ByteBuffer ( size_t  reserve)
inline
80 : _rpos(0), _wpos(0)
81 {
82 _storage.reserve(reserve);
83 }
size_t _wpos
Definition: ByteBuffer.h:531
void reserve(size_t ressize)
Definition: ByteBuffer.h:454
size_t _rpos
Definition: ByteBuffer.h:531

◆ ByteBuffer() [3/5]

ByteBuffer::ByteBuffer ( ByteBuffer &&  buf)
inlinenoexcept
85 :
86 _rpos(buf._rpos), _wpos(buf._wpos), _storage(std::move(buf._storage))
87 {
88 buf._rpos = 0;
89 buf._wpos = 0;
90 }

◆ ByteBuffer() [4/5]

ByteBuffer::ByteBuffer ( ByteBuffer const &  right)
default

◆ ByteBuffer() [5/5]

ByteBuffer::ByteBuffer ( MessageBuffer &&  buffer)
27 :
28 _rpos(0), _wpos(0), _storage(buffer.Move()) { }
std::vector< uint8 > && Move()
Definition: MessageBuffer.h:102

◆ ~ByteBuffer()

virtual ByteBuffer::~ByteBuffer ( )
virtualdefault

Member Function Documentation

◆ append() [1/6]

void ByteBuffer::append ( ByteBuffer const &  buffer)
inline
480 {
481 if (buffer.wpos())
482 {
483 append(buffer.contents(), buffer.wpos());
484 }
485 }
void append(T value)
Definition: ByteBuffer.h:129

References contents(), and wpos().

◆ append() [2/6]

void ByteBuffer::append ( const char *  src,
size_t  cnt 
)
inline
468 {
469 return append((const uint8 *)src, cnt);
470 }
std::uint8_t uint8
Definition: Define.h:110

◆ append() [3/6]

template<class T >
void ByteBuffer::append ( const T *  src,
size_t  cnt 
)
inline
473 {
474 return append((const uint8*)src, cnt * sizeof(T));
475 }

◆ append() [4/6]

template<size_t Size>
void ByteBuffer::append ( std::array< uint8, Size > const &  arr)
inline
489 {
490 append(arr.data(), Size);
491 }

◆ append() [5/6]

◆ append() [6/6]

void ByteBuffer::append ( uint8 const *  src,
size_t  cnt 
)
111{
112 ASSERT(src, "Attempted to put a NULL-pointer in ByteBuffer (pos: {} size: {})", _wpos, size());
113 ASSERT(cnt, "Attempted to put a zero-sized value in ByteBuffer (pos: {} size: {})", _wpos, size());
114 ASSERT(size() < 10000000);
115
116 size_t const newSize = _wpos + cnt;
117
118 if (_storage.capacity() < newSize) // custom memory allocation rules
119 {
120 if (newSize < 100)
121 _storage.reserve(300);
122 else if (newSize < 750)
123 _storage.reserve(2500);
124 else if (newSize < 6000)
125 _storage.reserve(10000);
126 else
127 _storage.reserve(400000);
128 }
129
130 if (_storage.size() < newSize)
131 _storage.resize(newSize);
132
133 std::memcpy(&_storage[_wpos], src, cnt);
134 _wpos = newSize;
135}
#define ASSERT
Definition: Errors.h:68
size_t size() const
Definition: ByteBuffer.h:444

References _storage, _wpos, ASSERT, and size().

◆ AppendPackedTime()

◆ appendPackGUID()

void ByteBuffer::appendPackGUID ( uint64  guid)
inline
504 {
505 uint8 packGUID[8 + 1];
506 packGUID[0] = 0;
507 size_t size = 1;
508
509 for (uint8 i = 0; guid != 0;++i)
510 {
511 if (guid & 0xFF)
512 {
513 packGUID[0] |= uint8(1 << i);
514 packGUID[size] = uint8(guid & 0xFF);
515 ++size;
516 }
517
518 guid >>= 8;
519 }
520
521 append(packGUID, size);
522 }

Referenced by AchievementMgr::BuildAllDataPacket(), Guild::MassInviteToEvent(), PackedGuid::PackedGuid(), AchievementMgr::SendCriteriaUpdate(), Player::SendEquipmentSetList(), PackedGuid::Set(), and Player::SetEquipmentSet().

◆ appendPackXYZ()

void ByteBuffer::appendPackXYZ ( float  x,
float  y,
float  z 
)
inline
495 {
496 uint32 packed = 0;
497 packed |= ((int)(x / 0.25f) & 0x7FF);
498 packed |= ((int)(y / 0.25f) & 0x7FF) << 11;
499 packed |= ((int)(z / 0.25f) & 0x3FF) << 22;
500 *this << packed;
501 }
std::uint32_t uint32
Definition: Define.h:108

Referenced by Movement::WriteLinearPath().

◆ clear()

◆ contents() [1/2]

◆ contents() [2/2]

uint8 const * ByteBuffer::contents ( ) const
inline
435 {
436 if (_storage.empty())
437 {
438 throw ByteBufferException();
439 }
440
441 return _storage.data();
442 }

◆ empty()

◆ hexlike()

void ByteBuffer::hexlike ( ) const
189{
190 if (!sLog->ShouldLog("network.opcode.buffer", LogLevel::LOG_LEVEL_TRACE)) // optimize disabled trace output
191 return;
192
193 uint32 j = 1, k = 1;
194
195 std::ostringstream o;
196 o << "STORAGE_SIZE: " << size();
197
198 for (uint32 i = 0; i < size(); ++i)
199 {
200 char buf[4];
201 snprintf(buf, 4, "%2X ", read<uint8>(i));
202
203 if ((i == (j * 8)) && ((i != (k * 16))))
204 {
205 o << "| ";
206 ++j;
207 }
208 else if (i == (k * 16))
209 {
210 o << "\n";
211 ++k;
212 ++j;
213 }
214
215 o << buf;
216 }
217
218 o << " ";
219
220 LOG_TRACE("network.opcode.buffer", "{}", o.str());
221}
#define sLog
Definition: Log.h:126
#define LOG_TRACE(filterType__,...)
Definition: Log.h:173

References LOG_TRACE, size(), and sLog.

Referenced by debug_commandscript::HandleDebugSendOpcodeCommand(), WorldSession::HandleWardenDataOpcode(), WardenMac::RequestChecks(), and WorldSession::Update().

◆ operator<<() [1/14]

ByteBuffer & ByteBuffer::operator<< ( bool  value)
inline
145 {
146 append<uint8>(value ? 1 : 0);
147 return *this;
148 }

◆ operator<<() [2/14]

ByteBuffer & ByteBuffer::operator<< ( char const *  str)
inline
229 {
230 return operator<<(std::string_view(str ? str : ""));
231 }
ByteBuffer & operator<<(bool value)
Definition: ByteBuffer.h:144

References operator<<().

◆ operator<<() [3/14]

ByteBuffer & ByteBuffer::operator<< ( double  value)
inline
207 {
208 append<double>(value);
209 return *this;
210 }

◆ operator<<() [4/14]

ByteBuffer & ByteBuffer::operator<< ( float  value)
inline
201 {
202 append<float>(value);
203 return *this;
204 }

◆ operator<<() [5/14]

ByteBuffer & ByteBuffer::operator<< ( int16  value)
inline
182 {
183 append<int16>(value);
184 return *this;
185 }

◆ operator<<() [6/14]

ByteBuffer & ByteBuffer::operator<< ( int32  value)
inline
188 {
189 append<int32>(value);
190 return *this;
191 }

◆ operator<<() [7/14]

ByteBuffer & ByteBuffer::operator<< ( int64  value)
inline
194 {
195 append<int64>(value);
196 return *this;
197 }

◆ operator<<() [8/14]

ByteBuffer & ByteBuffer::operator<< ( int8  value)
inline
176 {
177 append<int8>(value);
178 return *this;
179 }

◆ operator<<() [9/14]

ByteBuffer & ByteBuffer::operator<< ( std::string const &  str)
inline
224 {
225 return operator<<(std::string_view(str));
226 }

References operator<<().

◆ operator<<() [10/14]

ByteBuffer & ByteBuffer::operator<< ( std::string_view  value)
inline
213 {
214 if (size_t len = value.length())
215 {
216 append(reinterpret_cast<uint8 const*>(value.data()), len);
217 }
218
219 append(static_cast<uint8>(0));
220 return *this;
221 }

◆ operator<<() [11/14]

ByteBuffer & ByteBuffer::operator<< ( uint16  value)
inline
157 {
158 append<uint16>(value);
159 return *this;
160 }

◆ operator<<() [12/14]

ByteBuffer & ByteBuffer::operator<< ( uint32  value)
inline
163 {
164 append<uint32>(value);
165 return *this;
166 }

◆ operator<<() [13/14]

ByteBuffer & ByteBuffer::operator<< ( uint64  value)
inline
169 {
170 append<uint64>(value);
171 return *this;
172 }

◆ operator<<() [14/14]

ByteBuffer & ByteBuffer::operator<< ( uint8  value)
inline
151 {
152 append<uint8>(value);
153 return *this;
154 }

◆ operator=() [1/2]

ByteBuffer & ByteBuffer::operator= ( ByteBuffer &&  right)
inlinenoexcept
109 {
110 if (this != &right)
111 {
112 _rpos = right._rpos;
113 right._rpos = 0;
114 _wpos = right._wpos;
115 right._wpos = 0;
116 _storage = std::move(right._storage);
117 }
118
119 return *this;
120 }

References _rpos.

◆ operator=() [2/2]

ByteBuffer & ByteBuffer::operator= ( ByteBuffer const &  right)
inline
97 {
98 if (this != &right)
99 {
100 _rpos = right._rpos;
101 _wpos = right._wpos;
102 _storage = right._storage;
103 }
104
105 return *this;
106 }

References _rpos, _storage, and _wpos.

Referenced by WorldPacket::operator=().

◆ operator>>() [1/12]

ByteBuffer & ByteBuffer::operator>> ( bool &  value)
inline
234 {
235 value = read<char>() > 0;
236 return *this;
237 }

◆ operator>>() [2/12]

ByteBuffer & ByteBuffer::operator>> ( double &  value)
68{
69 value = read<double>();
70
71 if (!std::isfinite(value))
72 throw ByteBufferInvalidValueException("double", "infinity");
73
74 return *this;
75}
Definition: ByteBuffer.h:62

◆ operator>>() [3/12]

ByteBuffer & ByteBuffer::operator>> ( float &  value)
58{
59 value = read<float>();
60
61 if (!std::isfinite(value))
62 throw ByteBufferInvalidValueException("float", "infinity");
63
64 return *this;
65}

◆ operator>>() [4/12]

ByteBuffer & ByteBuffer::operator>> ( int16 value)
inline
271 {
272 value = read<int16>();
273 return *this;
274 }

◆ operator>>() [5/12]

ByteBuffer & ByteBuffer::operator>> ( int32 value)
inline
277 {
278 value = read<int32>();
279 return *this;
280 }

◆ operator>>() [6/12]

ByteBuffer & ByteBuffer::operator>> ( int64 value)
inline
283 {
284 value = read<int64>();
285 return *this;
286 }

◆ operator>>() [7/12]

ByteBuffer & ByteBuffer::operator>> ( int8 value)
inline
265 {
266 value = read<int8>();
267 return *this;
268 }

◆ operator>>() [8/12]

ByteBuffer & ByteBuffer::operator>> ( std::string &  value)
inline
292 {
293 value = ReadCString(true);
294 return *this;
295 }
std::string ReadCString(bool requireValidUtf8=true)
Definition: ByteBuffer.cpp:77

◆ operator>>() [9/12]

ByteBuffer & ByteBuffer::operator>> ( uint16 value)
inline
246 {
247 value = read<uint16>();
248 return *this;
249 }

◆ operator>>() [10/12]

ByteBuffer & ByteBuffer::operator>> ( uint32 value)
inline
252 {
253 value = read<uint32>();
254 return *this;
255 }

◆ operator>>() [11/12]

ByteBuffer & ByteBuffer::operator>> ( uint64 value)
inline
258 {
259 value = read<uint64>();
260 return *this;
261 }

◆ operator>>() [12/12]

ByteBuffer & ByteBuffer::operator>> ( uint8 value)
inline
240 {
241 value = read<uint8>();
242 return *this;
243 }

◆ operator[]() [1/2]

uint8 & ByteBuffer::operator[] ( size_t const  pos)
inline
298 {
299 if (pos >= size())
300 {
301 throw ByteBufferPositionException(false, pos, 1, size());
302 }
303
304 return _storage[pos];
305 }
Definition: ByteBuffer.h:46

◆ operator[]() [2/2]

uint8 const & ByteBuffer::operator[] ( size_t const  pos) const
inline
308 {
309 if (pos >= size())
310 {
311 throw ByteBufferPositionException(false, pos, 1, size());
312 }
313
314 return _storage[pos];
315 }

◆ print_storage()

void ByteBuffer::print_storage ( ) const
153{
154 if (!sLog->ShouldLog("network.opcode.buffer", LogLevel::LOG_LEVEL_TRACE)) // optimize disabled trace output
155 return;
156
157 std::ostringstream o;
158 o << "STORAGE_SIZE: " << size();
159
160 for (uint32 i = 0; i < size(); ++i)
161 o << read<uint8>(i) << " - ";
162
163 o << " ";
164
165 LOG_TRACE("network.opcode.buffer", "{}", o.str());
166}

References LOG_TRACE, size(), and sLog.

Referenced by WorldSession::LogUnprocessedTail().

◆ put() [1/2]

void ByteBuffer::put ( size_t  pos,
const uint8 src,
size_t  cnt 
)
144{
145 ASSERT(pos + cnt <= size(), "Attempted to put value with size: {} in ByteBuffer (pos: {} size: {})", cnt, pos, size());
146 ASSERT(src, "Attempted to put a NULL-pointer in ByteBuffer (pos: {} size: {})", pos, size());
147 ASSERT(cnt, "Attempted to put a zero-sized value in ByteBuffer (pos: {} size: {})", pos, size());
148
149 std::memcpy(&_storage[pos], src, cnt);
150}

References _storage, ASSERT, and size().

◆ put() [2/2]

◆ read() [1/4]

◆ read() [2/4]

template<typename T >
T ByteBuffer::read ( size_t  pos) const
inline
359 {
360 if (pos + sizeof(T) > size())
361 {
362 throw ByteBufferPositionException(false, pos, sizeof(T), size());
363 }
364
365 T val = *((T const*)&_storage[pos]);
366 EndianConvert(val);
367 return val;
368 }

References EndianConvert().

◆ read() [3/4]

template<size_t Size>
void ByteBuffer::read ( std::array< uint8, Size > &  arr)
inline
383 {
384 read(arr.data(), Size);
385 }
T read()
Definition: ByteBuffer.h:351

◆ read() [4/4]

void ByteBuffer::read ( uint8 dest,
size_t  len 
)
inline
371 {
372 if (_rpos + len > size())
373 {
374 throw ByteBufferPositionException(false, _rpos, len, size());
375 }
376
377 std::memcpy(dest, &_storage[_rpos], len);
378 _rpos += len;
379 }

◆ read_skip() [1/4]

◆ read_skip() [2/4]

template<>
void ByteBuffer::read_skip ( )
inline
546{
547 std::string temp;
548 *this >> temp;
549}

◆ read_skip() [3/4]

template<>
void ByteBuffer::read_skip ( )
inline
553{
554 read_skip<char*>();
555}

◆ read_skip() [4/4]

void ByteBuffer::read_skip ( size_t  skip)
inline
342 {
343 if (_rpos + skip > size())
344 {
345 throw ByteBufferPositionException(false, _rpos, skip, size());
346 }
347
348 _rpos += skip;
349 }

◆ ReadCString()

std::string ByteBuffer::ReadCString ( bool  requireValidUtf8 = true)
78{
79 std::string value;
80
81 while (rpos() < size()) // prevent crash at wrong string format in packet
82 {
83 char c = read<char>();
84 if (c == 0)
85 break;
86 value += c;
87 }
88
89 if (requireValidUtf8 && !utf8::is_valid(value.begin(), value.end()))
90 throw ByteBufferInvalidValueException("string", value.c_str());
91
92 return value;
93}
size_t rpos() const
Definition: ByteBuffer.h:317

References rpos(), and size().

Referenced by WorldSession::HandleMessagechatOpcode().

◆ ReadPackedTime() [1/2]

uint32 ByteBuffer::ReadPackedTime ( )
96{
97 uint32 packedDate = read<uint32>();
98 tm lt = tm();
99
100 lt.tm_min = packedDate & 0x3F;
101 lt.tm_hour = (packedDate >> 6) & 0x1F;
102 //lt.tm_wday = (packedDate >> 11) & 7;
103 lt.tm_mday = ((packedDate >> 14) & 0x3F) + 1;
104 lt.tm_mon = (packedDate >> 20) & 0xF;
105 lt.tm_year = ((packedDate >> 24) & 0x1F) + 100;
106
107 return uint32(mktime(&lt));
108}

Referenced by WorldSession::HandleCalendarAddEvent(), WorldSession::HandleCalendarCopyEvent(), and WorldSession::HandleCalendarUpdateEvent().

◆ ReadPackedTime() [2/2]

ByteBuffer & ByteBuffer::ReadPackedTime ( uint32 time)
inline
419 {
420 time = ReadPackedTime();
421 return *this;
422 }
uint32 ReadPackedTime()
Definition: ByteBuffer.cpp:95

◆ readPackGUID()

void ByteBuffer::readPackGUID ( uint64 guid)
inline
388 {
389 if (rpos() + 1 > size())
390 {
391 throw ByteBufferPositionException(false, _rpos, 1, size());
392 }
393
394 guid = 0;
395
396 uint8 guidmark = 0;
397 (*this) >> guidmark;
398
399 for (int i = 0; i < 8; ++i)
400 {
401 if (guidmark & (uint8(1) << i))
402 {
403 if (rpos() + 1 > size())
404 {
405 throw ByteBufferPositionException(false, _rpos, 1, size());
406 }
407
408 uint8 bit;
409 (*this) >> bit;
410 guid |= (uint64(bit) << (i * 8));
411 }
412 }
413 }
std::uint64_t uint64
Definition: Define.h:107

Referenced by WorldSession::HandleEquipmentSetDelete(), WorldSession::HandleEquipmentSetSave(), and operator>>().

◆ reserve()

void ByteBuffer::reserve ( size_t  ressize)
inline
455 {
456 if (ressize > size())
457 {
458 _storage.reserve(ressize);
459 }
460 }

Referenced by WorldPackets::Guild::GuildEventLogQueryResults::Write(), and WorldPackets::WorldState::InitWorldStates::Write().

◆ resize()

◆ rfinish()

◆ rpos() [1/2]

◆ rpos() [2/2]

size_t ByteBuffer::rpos ( size_t  rpos_)
inline
320 {
321 _rpos = rpos_;
322 return _rpos;
323 }

◆ shrink_to_fit()

void ByteBuffer::shrink_to_fit ( )
inline
463 {
464 _storage.shrink_to_fit();
465 }

◆ size()

◆ textlike()

void ByteBuffer::textlike ( ) const
169{
170 if (!sLog->ShouldLog("network.opcode.buffer", LogLevel::LOG_LEVEL_TRACE)) // optimize disabled trace output
171 return;
172
173 std::ostringstream o;
174 o << "STORAGE_SIZE: " << size();
175
176 for (uint32 i = 0; i < size(); ++i)
177 {
178 char buf[2];
179 snprintf(buf, 2, "%c", read<uint8>(i));
180 o << buf;
181 }
182
183 o << " ";
184
185 LOG_TRACE("network.opcode.buffer", "{}", o.str());
186}

References LOG_TRACE, size(), and sLog.

◆ wpos() [1/2]

◆ wpos() [2/2]

size_t ByteBuffer::wpos ( size_t  wpos_)
inline
333 {
334 _wpos = wpos_;
335 return _wpos;
336 }

Member Data Documentation

◆ _rpos

size_t ByteBuffer::_rpos {0}
protected

Referenced by operator=().

◆ _storage

std::vector<uint8> ByteBuffer::_storage
protected

◆ _wpos

size_t ByteBuffer::_wpos {0}
protected

Referenced by append(), and operator=().

◆ DEFAULT_SIZE

constexpr size_t ByteBuffer::DEFAULT_SIZE = 0x1000
staticconstexpr