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)
28 :
29 _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 
)
112{
113 ASSERT(src, "Attempted to put a NULL-pointer in ByteBuffer (pos: {} size: {})", _wpos, size());
114 ASSERT(cnt, "Attempted to put a zero-sized value in ByteBuffer (pos: {} size: {})", _wpos, size());
115 ASSERT(size() < 10000000);
116
117 size_t const newSize = _wpos + cnt;
118
119 if (_storage.capacity() < newSize) // custom memory allocation rules
120 {
121 if (newSize < 100)
122 _storage.reserve(300);
123 else if (newSize < 750)
124 _storage.reserve(2500);
125 else if (newSize < 6000)
126 _storage.reserve(10000);
127 else
128 _storage.reserve(400000);
129 }
130
131 if (_storage.size() < newSize)
132 _storage.resize(newSize);
133
134 std::memcpy(&_storage[_wpos], src, cnt);
135 _wpos = newSize;
136}
#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
190{
191 if (!sLog->ShouldLog("network.opcode.buffer", LogLevel::LOG_LEVEL_TRACE)) // optimize disabled trace output
192 return;
193
194 uint32 j = 1, k = 1;
195
196 std::ostringstream o;
197 o << "STORAGE_SIZE: " << size();
198
199 for (uint32 i = 0; i < size(); ++i)
200 {
201 char buf[4];
202 snprintf(buf, 4, "%2X ", read<uint8>(i));
203
204 if ((i == (j * 8)) && ((i != (k * 16))))
205 {
206 o << "| ";
207 ++j;
208 }
209 else if (i == (k * 16))
210 {
211 o << "\n";
212 ++k;
213 ++j;
214 }
215
216 o << buf;
217 }
218
219 o << " ";
220
221 LOG_TRACE("network.opcode.buffer", "{}", o.str());
222}
#define sLog
Definition: Log.h:128
#define LOG_TRACE(filterType__,...)
Definition: Log.h:175

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)
69{
70 value = read<double>();
71
72 if (!std::isfinite(value))
73 throw ByteBufferInvalidValueException("double", "infinity");
74
75 return *this;
76}
Definition: ByteBuffer.h:62

◆ operator>>() [3/12]

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

◆ 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:78

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

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 
)
145{
146 ASSERT(pos + cnt <= size(), "Attempted to put value with size: {} in ByteBuffer (pos: {} size: {})", cnt, pos, size());
147 ASSERT(src, "Attempted to put a NULL-pointer in ByteBuffer (pos: {} size: {})", pos, size());
148 ASSERT(cnt, "Attempted to put a zero-sized value in ByteBuffer (pos: {} size: {})", pos, size());
149
150 std::memcpy(&_storage[pos], src, cnt);
151}

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

References rpos(), and size().

Referenced by WorldSession::HandleMessagechatOpcode().

◆ ReadPackedTime() [1/2]

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

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:96

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

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