TrinityCore
Loading...
Searching...
No Matches
ByteBuffer.h
Go to the documentation of this file.
1/*
2 * This file is part of the TrinityCore Project. See AUTHORS file for Copyright information
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#ifndef TRINITYCORE_BYTE_BUFFER_H
19#define TRINITYCORE_BYTE_BUFFER_H
20
21#include "ByteConverter.h"
22#include "Concepts.h"
23#include "Define.h"
24#include <array>
25#include <span>
26#include <string>
27#include <vector>
28#include <cstring>
29
30// Root of ByteBuffer exception hierarchy
31class TC_SHARED_API ByteBufferException : public std::exception
32{
33public:
34 explicit ByteBufferException() = default;
35 explicit ByteBufferException(std::string&& message) noexcept : msg_(std::move(message)) { }
36
37 char const* what() const noexcept override { return msg_.c_str(); }
38
39protected:
40 std::string msg_;
41};
42
44{
45public:
46 ByteBufferPositionException(size_t pos, size_t size, size_t valueSize);
47};
48
50{
51public:
52 ByteBufferInvalidValueException(char const* type, std::string_view value);
53};
54
55template <typename T>
56concept ByteBufferNumeric = std::same_as<T, int8> || std::same_as<T, uint8>
57 || std::same_as<T, int16> || std::same_as<T, uint16>
58 || std::same_as<T, int32> || std::same_as<T, uint32>
59 || std::same_as<T, int64> || std::same_as<T, uint64>
60 || std::same_as<T, float> || std::same_as<T, double>
61 || std::same_as<T, char> || std::is_enum_v<T>;
62
64{
65 public:
66 constexpr static size_t DEFAULT_SIZE = 0x1000;
67 constexpr static uint8 InitialBitPos = 8;
68
69 // reserve/resize tag
70 struct Reserve { };
71 struct Resize { };
72
73 // constructor
74 explicit ByteBuffer() : ByteBuffer(DEFAULT_SIZE, Reserve{}) { }
75
76 explicit ByteBuffer(size_t size, Reserve) : _rpos(0), _wpos(0), _bitpos(InitialBitPos), _curbitval(0)
77 {
78 _storage.reserve(size);
79 }
80
81 explicit ByteBuffer(size_t size, Resize) : _rpos(0), _wpos(size), _bitpos(InitialBitPos), _curbitval(0)
82 {
83 _storage.resize(size);
84 }
85
86 ByteBuffer(ByteBuffer const& right) = default;
87
88 ByteBuffer(ByteBuffer&& buf) noexcept : _rpos(buf._rpos), _wpos(buf._wpos),
89 _bitpos(buf._bitpos), _curbitval(buf._curbitval), _storage(std::move(buf).Release()) { }
90
91 explicit ByteBuffer(std::vector<uint8>&& buffer) noexcept : _rpos(0), _wpos(buffer.size()),
92 _bitpos(InitialBitPos), _curbitval(0), _storage(std::move(buffer)) { }
93
94 std::vector<uint8>&& Release() && noexcept
95 {
96 _rpos = 0;
97 _wpos = 0;
98 _bitpos = InitialBitPos;
99 _curbitval = 0;
100 return std::move(_storage);
101 }
102
103 ByteBuffer& operator=(ByteBuffer const& right) = default;
104
105 ByteBuffer& operator=(ByteBuffer&& right) noexcept
106 {
107 if (this != &right)
108 {
109 _rpos = right._rpos;
110 _wpos = right._wpos;
111 _bitpos = right._bitpos;
112 _curbitval = right._curbitval;
113 _storage = std::move(right).Release();
114 }
115
116 return *this;
117 }
118
119 virtual ~ByteBuffer() = default;
120
121 void clear()
122 {
123 _rpos = 0;
124 _wpos = 0;
125 _bitpos = InitialBitPos;
126 _curbitval = 0;
127 _storage.clear();
128 }
129
130 template <ByteBufferNumeric T>
131 void append(T value)
132 {
133 EndianConvert(value);
134 append(reinterpret_cast<uint8 const*>(&value), sizeof(value));
135 }
136
138 {
139 return _bitpos != 8;
140 }
141
143 {
144 if (_bitpos == 8)
145 return;
146
147 _bitpos = 8;
148
149 append(&_curbitval, sizeof(uint8));
150 _curbitval = 0;
151 }
152
154 {
155 _bitpos = 8;
156 _curbitval = 0;
157 }
158
159 bool WriteBit(bool bit)
160 {
161 --_bitpos;
162 if (bit)
163 _curbitval |= (1 << (_bitpos));
164
165 if (_bitpos == 0)
166 {
167 _bitpos = 8;
168 append(&_curbitval, sizeof(_curbitval));
169 _curbitval = 0;
170 }
171
172 return bit;
173 }
174
175 bool ReadBit()
176 {
177 if (_bitpos >= 8)
178 {
179 read(&_curbitval, 1);
180 _bitpos = 0;
181 }
182
183 return ((_curbitval >> (8 - ++_bitpos)) & 1) != 0;
184 }
185
186 void WriteBits(uint64 value, int32 bits)
187 {
188 // remove bits that don't fit
189 value &= (UI64LIT(1) << bits) - 1;
190
191 if (bits > int32(_bitpos))
192 {
193 // first write to fill bit buffer
194 _curbitval |= value >> (bits - _bitpos);
195 bits -= _bitpos;
196 _bitpos = 8; // required "unneccessary" write to avoid double flushing
197 append(&_curbitval, sizeof(_curbitval));
198
199 // then append as many full bytes as possible
200 while (bits >= 8)
201 {
202 bits -= 8;
203 append<uint8>(value >> bits);
204 }
205
206 // store remaining bits in the bit buffer
207 _bitpos = 8 - bits;
208 _curbitval = (value & ((UI64LIT(1) << bits) - 1)) << _bitpos;
209 }
210 else
211 {
212 // entire value fits in the bit buffer
213 _bitpos -= bits;
214 _curbitval |= value << _bitpos;
215
216 if (_bitpos == 0)
217 {
218 _bitpos = 8;
219 append(&_curbitval, sizeof(_curbitval));
220 _curbitval = 0;
221 }
222 }
223 }
224
226 {
227 uint32 value = 0;
228 if (bits > 8 - int32(_bitpos))
229 {
230 // first retrieve whatever is left in the bit buffer
231 int32 bitsInBuffer = 8 - _bitpos;
232 value = (_curbitval & ((UI64LIT(1) << bitsInBuffer) - 1)) << (bits - bitsInBuffer);
233 bits -= bitsInBuffer;
234
235 // then read as many full bytes as possible
236 while (bits >= 8)
237 {
238 bits -= 8;
239 value |= read<uint8>() << bits;
240 }
241
242 // and finally any remaining bits
243 if (bits)
244 {
245 read(&_curbitval, 1);
246 value |= (_curbitval >> (8 - bits)) & ((UI64LIT(1) << bits) - 1);
247 _bitpos = bits;
248 }
249 }
250 else
251 {
252 // entire value is in the bit buffer
253 value = (_curbitval >> (8 - _bitpos - bits)) & ((UI64LIT(1) << bits) - 1);
254 _bitpos += bits;
255 }
256
257 return value;
258 }
259
260 template <ByteBufferNumeric T>
261 void put(std::size_t pos, T value)
262 {
263 EndianConvert(value);
264 put(pos, reinterpret_cast<uint8 const*>(&value), sizeof(value));
265 }
266
279 void PutBits(std::size_t pos, std::size_t value, uint32 bitCount);
280
281 ByteBuffer& operator<<(bool) = delete; // prevent implicit conversions to int32
282
284 {
285 append<char>(value);
286 return *this;
287 }
288
290 {
291 append<uint8>(value);
292 return *this;
293 }
294
296 {
297 append<uint16>(value);
298 return *this;
299 }
300
302 {
303 append<uint32>(value);
304 return *this;
305 }
306
308 {
309 append<uint64>(value);
310 return *this;
311 }
312
313 // signed as in 2e complement
315 {
316 append<int8>(value);
317 return *this;
318 }
319
321 {
322 append<int16>(value);
323 return *this;
324 }
325
327 {
328 append<int32>(value);
329 return *this;
330 }
331
333 {
334 append<int64>(value);
335 return *this;
336 }
337
338 // floating points
339 ByteBuffer& operator<<(float value)
340 {
341 append<float>(value);
342 return *this;
343 }
344
345 ByteBuffer& operator<<(double value)
346 {
347 append<double>(value);
348 return *this;
349 }
350
351 ByteBuffer& operator<<(std::string_view value)
352 {
353 if (size_t len = value.length())
354 append(reinterpret_cast<uint8 const*>(value.data()), len);
355 append(static_cast<uint8>(0));
356 return *this;
357 }
358
359 ByteBuffer& operator<<(std::string const& str)
360 {
361 return operator<<(std::string_view(str));
362 }
363
364 ByteBuffer& operator<<(char const* str)
365 {
366 return operator<<(std::string_view(str ? str : ""));
367 }
368
369 ByteBuffer& operator>>(bool&) = delete;
370
371 ByteBuffer& operator>>(char& value)
372 {
373 read(&value, 1);
374 return *this;
375 }
376
378 {
379 read(&value, 1);
380 return *this;
381 }
382
384 {
385 read(&value, 1);
386 return *this;
387 }
388
390 {
391 read(&value, 1);
392 return *this;
393 }
394
396 {
397 read(&value, 1);
398 return *this;
399 }
400
401 //signed as in 2e complement
403 {
404 read(&value, 1);
405 return *this;
406 }
407
409 {
410 read(&value, 1);
411 return *this;
412 }
413
415 {
416 read(&value, 1);
417 return *this;
418 }
419
421 {
422 read(&value, 1);
423 return *this;
424 }
425
426 ByteBuffer& operator>>(float& value);
427 ByteBuffer& operator>>(double& value);
428
429 ByteBuffer& operator>>(std::string& value)
430 {
431 value = ReadCString(true);
432 return *this;
433 }
434
435 uint8& operator[](size_t const pos)
436 {
437 if (pos >= _storage.size())
438 OnInvalidPosition(pos, 1);
439 return _storage[pos];
440 }
441
442 uint8 const& operator[](size_t const pos) const
443 {
444 if (pos >= _storage.size())
445 OnInvalidPosition(pos, 1);
446 return _storage[pos];
447 }
448
449 size_t rpos() const { return _rpos; }
450
451 size_t rpos(size_t rpos_)
452 {
453 _rpos = rpos_;
454 return _rpos;
455 }
456
457 void rfinish()
458 {
459 _rpos = wpos();
460 }
461
462 size_t wpos() const { return _wpos; }
463
464 size_t wpos(size_t wpos_)
465 {
466 _wpos = wpos_;
467 return _wpos;
468 }
469
471 size_t bitwpos() const { return _wpos * 8 + 8 - _bitpos; }
472
473 size_t bitwpos(size_t newPos)
474 {
475 _wpos = newPos / 8;
476 _bitpos = 8 - (newPos % 8);
477 return _wpos * 8 + 8 - _bitpos;
478 }
479
480 template <ByteBufferNumeric T>
481 void read_skip() { read_skip(sizeof(T)); }
482
483 void read_skip(size_t skip)
484 {
485 if (_rpos + skip > _storage.size())
486 OnInvalidPosition(_rpos, skip);
487
488 ResetBitPos();
489 _rpos += skip;
490 }
491
492 template <ByteBufferNumeric T>
494 {
495 ResetBitPos();
496 T r = read<T>(_rpos);
497 _rpos += sizeof(T);
498 return r;
499 }
500
501 template <ByteBufferNumeric T>
502 T read(size_t pos) const
503 {
504 if (pos + sizeof(T) > _storage.size())
505 OnInvalidPosition(pos, sizeof(T));
506
507 T val;
508 std::memcpy(&val, &_storage[pos], sizeof(T));
509 EndianConvert(val);
510 return val;
511 }
512
513 template <ByteBufferNumeric T>
514 void read(T* dest, size_t count)
515 {
516 static_assert(std::is_trivially_copyable_v<T>, "read(T*, size_t) must be used with trivially copyable types");
517 read(reinterpret_cast<uint8*>(dest), count * sizeof(T));
518#if TRINITY_ENDIAN == TRINITY_BIGENDIAN
519 for (size_t i = 0; i < count; ++i)
520 EndianConvert(dest[i]);
521#endif
522 }
523
524 void read(uint8* dest, size_t len)
525 {
526 if (_rpos + len > _storage.size())
527 OnInvalidPosition(_rpos, len);
528
529 ResetBitPos();
530 std::memcpy(dest, &_storage[_rpos], len);
531 _rpos += len;
532 }
533
534 template <ByteBufferNumeric T, size_t Size>
535 void read(std::array<T, Size>& arr)
536 {
537 read(arr.data(), Size);
538 }
539
542 void WriteString(std::string const& str)
543 {
544 if (size_t len = str.length())
545 append(str.c_str(), len);
546 }
547
548 void WriteString(std::string_view str)
549 {
550 if (size_t len = str.length())
551 append(str.data(), len);
552 }
553
554 void WriteString(char const* str, size_t len)
555 {
556 if (len)
557 append(str, len);
558 }
559
560 void ReadSkipCString(bool requireValidUtf8 = true) { (void)ReadCString(requireValidUtf8); }
561
562 std::string_view ReadCString(bool requireValidUtf8 = true);
563
564 std::string_view ReadString(uint32 length, bool requireValidUtf8 = true);
565
566 std::span<uint8> ReadBytes(size_t length)
567 {
568 if (_rpos + length > _storage.size())
569 OnInvalidPosition(_rpos, length);
570
571 ResetBitPos();
572 uint8* data = _storage.data() + _rpos;
573 _rpos += length;
574 return { data, length };
575 }
576
577 uint8* data() { return _storage.data(); }
578 uint8 const* data() const { return _storage.data(); }
579
580 size_t size() const { return _storage.size(); }
581 bool empty() const { return _storage.empty(); }
582
583 void resize(size_t newsize)
584 {
585 _storage.resize(newsize, 0);
586 _rpos = 0;
587 _wpos = _storage.size();
588 }
589
590 void reserve(size_t ressize)
591 {
592 if (ressize > _storage.size())
593 _storage.reserve(ressize);
594 }
595
597 {
598 _storage.shrink_to_fit();
599 }
600
601 template <ByteBufferNumeric T>
602 void append(T const* src, size_t cnt)
603 {
604#if TRINITY_ENDIAN == TRINITY_LITTLEENDIAN
605 append(reinterpret_cast<uint8 const*>(src), cnt * sizeof(T));
606#else
607 for (size_t i = 0; i < cnt; ++i)
608 append<T>(src[i]);
609#endif
610 }
611
612 void append(uint8 const* src, size_t cnt);
613
614 void append(ByteBuffer const& buffer)
615 {
616 if (!buffer.empty())
617 append(buffer.data(), buffer.size());
618 }
619
620 template <ByteBufferNumeric T, std::size_t Size>
621 void append(std::array<T, Size> const& arr)
622 {
623 append(arr.data(), Size);
624 }
625
626 void put(size_t pos, uint8 const* src, size_t cnt);
627
628 void print_storage() const;
629
630 void textlike() const;
631
632 void hexlike() const;
633
634 [[noreturn]] void OnInvalidPosition(size_t pos, size_t valueSize) const;
635
636 protected:
637 size_t _rpos, _wpos;
640 std::vector<uint8> _storage;
641};
642
643extern template char ByteBuffer::read<char>();
644extern template uint8 ByteBuffer::read<uint8>();
645extern template uint16 ByteBuffer::read<uint16>();
646extern template uint32 ByteBuffer::read<uint32>();
647extern template uint64 ByteBuffer::read<uint64>();
648extern template int8 ByteBuffer::read<int8>();
649extern template int16 ByteBuffer::read<int16>();
650extern template int32 ByteBuffer::read<int32>();
651extern template int64 ByteBuffer::read<int64>();
652extern template float ByteBuffer::read<float>();
653extern template double ByteBuffer::read<double>();
654
655template <typename T>
656concept HasByteBufferShiftOperators = requires(ByteBuffer& data, T const& value) { { data << value } -> std::convertible_to<ByteBuffer&>; }
657 && requires(ByteBuffer& data, T& value) { { data >> value } -> std::convertible_to<ByteBuffer&>; };
658
659#endif
void EndianConvert(T &val)
uint8_t uint8
Definition Define.h:156
#define TC_SHARED_API
Definition Define.h:123
int64_t int64
Definition Define.h:149
int16_t int16
Definition Define.h:151
int8_t int8
Definition Define.h:152
int32_t int32
Definition Define.h:150
uint64_t uint64
Definition Define.h:153
#define UI64LIT(N)
Definition Define.h:139
uint16_t uint16
Definition Define.h:155
uint32_t uint32
Definition Define.h:154
ByteBuffer & operator<<(ByteBuffer &buf, ObjectGuid const &guid)
ByteBuffer & operator>>(ByteBuffer &buf, ObjectGuid &guid)
std::string msg_
Definition ByteBuffer.h:40
ByteBufferException(std::string &&message) noexcept
Definition ByteBuffer.h:35
char const * what() const noexcept override
Definition ByteBuffer.h:37
ByteBufferException()=default
uint32 ReadBits(int32 bits)
Definition ByteBuffer.h:225
ByteBuffer & operator<<(uint32 value)
Definition ByteBuffer.h:301
ByteBuffer & operator>>(int64 &value)
Definition ByteBuffer.h:420
void read_skip()
Definition ByteBuffer.h:481
ByteBuffer & operator<<(double value)
Definition ByteBuffer.h:345
ByteBuffer & operator<<(std::string const &str)
Definition ByteBuffer.h:359
ByteBuffer & operator>>(uint32 &value)
Definition ByteBuffer.h:389
size_t rpos() const
Definition ByteBuffer.h:449
ByteBuffer & operator<<(uint8 value)
Definition ByteBuffer.h:289
size_t bitwpos() const
Returns position of last written bit.
Definition ByteBuffer.h:471
std::vector< uint8 > _storage
Definition ByteBuffer.h:640
ByteBuffer & operator<<(int32 value)
Definition ByteBuffer.h:326
void reserve(size_t ressize)
Definition ByteBuffer.h:590
void WriteString(std::string const &str)
Definition ByteBuffer.h:542
void WriteString(char const *str, size_t len)
Definition ByteBuffer.h:554
ByteBuffer & operator<<(int64 value)
Definition ByteBuffer.h:332
uint8 _bitpos
Definition ByteBuffer.h:638
std::span< uint8 > ReadBytes(size_t length)
Definition ByteBuffer.h:566
ByteBuffer & operator<<(std::string_view value)
Definition ByteBuffer.h:351
T read(size_t pos) const
Definition ByteBuffer.h:502
void resize(size_t newsize)
Definition ByteBuffer.h:583
ByteBuffer & operator>>(int16 &value)
Definition ByteBuffer.h:408
void read_skip(size_t skip)
Definition ByteBuffer.h:483
void shrink_to_fit()
Definition ByteBuffer.h:596
ByteBuffer & operator>>(uint8 &value)
Definition ByteBuffer.h:377
void append(T const *src, size_t cnt)
Definition ByteBuffer.h:602
std::vector< uint8 > && Release() &&noexcept
Definition ByteBuffer.h:94
void append(T value)
Definition ByteBuffer.h:131
ByteBuffer & operator<<(float value)
Definition ByteBuffer.h:339
ByteBuffer & operator>>(int8 &value)
Definition ByteBuffer.h:402
ByteBuffer & operator>>(uint16 &value)
Definition ByteBuffer.h:383
void rfinish()
Definition ByteBuffer.h:457
void ResetBitPos()
Definition ByteBuffer.h:153
ByteBuffer(std::vector< uint8 > &&buffer) noexcept
Definition ByteBuffer.h:91
uint8 & operator[](size_t const pos)
Definition ByteBuffer.h:435
ByteBuffer & operator>>(bool &)=delete
uint8 const & operator[](size_t const pos) const
Definition ByteBuffer.h:442
void WriteString(std::string_view str)
Definition ByteBuffer.h:548
ByteBuffer & operator=(ByteBuffer const &right)=default
ByteBuffer & operator<<(uint16 value)
Definition ByteBuffer.h:295
size_t wpos() const
Definition ByteBuffer.h:462
void append(ByteBuffer const &buffer)
Definition ByteBuffer.h:614
void ReadSkipCString(bool requireValidUtf8=true)
Definition ByteBuffer.h:560
ByteBuffer & operator<<(bool)=delete
uint8 _curbitval
Definition ByteBuffer.h:639
ByteBuffer & operator>>(std::string &value)
Definition ByteBuffer.h:429
bool WriteBit(bool bit)
Definition ByteBuffer.h:159
ByteBuffer & operator=(ByteBuffer &&right) noexcept
Definition ByteBuffer.h:105
size_t rpos(size_t rpos_)
Definition ByteBuffer.h:451
void read(std::array< T, Size > &arr)
Definition ByteBuffer.h:535
ByteBuffer(size_t size, Resize)
Definition ByteBuffer.h:81
void put(std::size_t pos, T value)
Definition ByteBuffer.h:261
ByteBuffer(ByteBuffer &&buf) noexcept
Definition ByteBuffer.h:88
ByteBuffer & operator>>(int32 &value)
Definition ByteBuffer.h:414
void clear()
Definition ByteBuffer.h:121
ByteBuffer & operator<<(uint64 value)
Definition ByteBuffer.h:307
ByteBuffer & operator<<(int16 value)
Definition ByteBuffer.h:320
size_t size() const
Definition ByteBuffer.h:580
void read(T *dest, size_t count)
Definition ByteBuffer.h:514
bool empty() const
Definition ByteBuffer.h:581
void append(std::array< T, Size > const &arr)
Definition ByteBuffer.h:621
ByteBuffer(ByteBuffer const &right)=default
uint8 const * data() const
Definition ByteBuffer.h:578
void FlushBits()
Definition ByteBuffer.h:142
size_t _rpos
Definition ByteBuffer.h:637
size_t bitwpos(size_t newPos)
Definition ByteBuffer.h:473
size_t wpos(size_t wpos_)
Definition ByteBuffer.h:464
ByteBuffer & operator<<(int8 value)
Definition ByteBuffer.h:314
bool HasUnfinishedBitPack() const
Definition ByteBuffer.h:137
ByteBuffer & operator>>(char &value)
Definition ByteBuffer.h:371
ByteBuffer & operator<<(char value)
Definition ByteBuffer.h:283
ByteBuffer(size_t size, Reserve)
Definition ByteBuffer.h:76
ByteBuffer & operator>>(uint64 &value)
Definition ByteBuffer.h:395
ByteBuffer & operator<<(char const *str)
Definition ByteBuffer.h:364
uint8 * data()
Definition ByteBuffer.h:577
bool ReadBit()
Definition ByteBuffer.h:175
virtual ~ByteBuffer()=default
void read(uint8 *dest, size_t len)
Definition ByteBuffer.h:524
void WriteBits(uint64 value, int32 bits)
Definition ByteBuffer.h:186