TrinityCore
Loading...
Searching...
No Matches
ByteBuffer.cpp
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#include "ByteBuffer.h"
19#include "Errors.h"
20#include "MessageBuffer.h"
21#include "Common.h"
22#include "Log.h"
23#include "Util.h"
24#include <utf8.h>
25#include <sstream>
26#include <cmath>
27
28ByteBuffer::ByteBuffer(MessageBuffer&& buffer) : _rpos(0), _wpos(0), _storage(buffer.Move())
29{
30}
31
32ByteBufferPositionException::ByteBufferPositionException(bool add, size_t pos, size_t size, size_t valueSize)
33{
34 std::ostringstream ss;
35
36 ss << "Attempted to " << (add ? "put" : "get") << " value with size: "
37 << valueSize << " in ByteBuffer (pos: " << pos << " size: " << size
38 << ")";
39
40 message().assign(ss.str());
41}
42
43ByteBufferSourceException::ByteBufferSourceException(size_t pos, size_t size, size_t valueSize)
44{
45 std::ostringstream ss;
46
47 ss << "Attempted to put a "
48 << (valueSize > 0 ? "NULL-pointer" : "zero-sized value")
49 << " in ByteBuffer (pos: " << pos << " size: " << size << ")";
50
51 message().assign(ss.str());
52}
53
55{
56 message().assign(Trinity::StringFormat("Invalid {} value ({}) found in ByteBuffer", type, value));
57}
58
60{
61 value = read<float>();
62 if (!std::isfinite(value))
63 throw ByteBufferInvalidValueException("float", "infinity");
64 return *this;
65}
66
68{
69 value = read<double>();
70 if (!std::isfinite(value))
71 throw ByteBufferInvalidValueException("double", "infinity");
72 return *this;
73}
74
75std::string ByteBuffer::ReadCString(bool requireValidUtf8 /*= true*/)
76{
77 std::string value;
78 while (rpos() < size()) // prevent crash at wrong string format in packet
79 {
80 char c = read<char>();
81 if (c == 0)
82 break;
83 value += c;
84 }
85 if (requireValidUtf8 && !utf8::is_valid(value.begin(), value.end()))
86 throw ByteBufferInvalidValueException("string", value.c_str());
87 return value;
88}
89
90void ByteBuffer::append(uint8 const* src, size_t cnt)
91{
92 ASSERT(src, "Attempted to put a NULL-pointer in ByteBuffer (pos: " SZFMTD " size: " SZFMTD ")", _wpos, size());
93 ASSERT(cnt, "Attempted to put a zero-sized value in ByteBuffer (pos: " SZFMTD " size: " SZFMTD ")", _wpos, size());
94 ASSERT(size() < 10000000);
95
96 size_t const newSize = _wpos + cnt;
97 if (_storage.capacity() < newSize) // custom memory allocation rules
98 {
99 if (newSize < 100)
100 _storage.reserve(300);
101 else if (newSize < 750)
102 _storage.reserve(2500);
103 else if (newSize < 6000)
104 _storage.reserve(10000);
105 else
106 _storage.reserve(400000);
107 }
108
109 if (_storage.size() < newSize)
110 _storage.resize(newSize);
111 std::memcpy(&_storage[_wpos], src, cnt);
112 _wpos = newSize;
113}
114
115void ByteBuffer::put(size_t pos, uint8 const* src, size_t cnt)
116{
117 ASSERT(pos + cnt <= size(), "Attempted to put value with size: " SZFMTD " in ByteBuffer (pos: " SZFMTD " size: " SZFMTD ")", cnt, pos, size());
118 ASSERT(src, "Attempted to put a NULL-pointer in ByteBuffer (pos: " SZFMTD " size: " SZFMTD ")", pos, size());
119 ASSERT(cnt, "Attempted to put a zero-sized value in ByteBuffer (pos: " SZFMTD " size: " SZFMTD ")", pos, size());
120
121 std::memcpy(&_storage[pos], src, cnt);
122}
123
125{
126 if (!sLog->ShouldLog("network", LOG_LEVEL_TRACE)) // optimize disabled trace output
127 return;
128
129 std::ostringstream o;
130 o << "STORAGE_SIZE: " << size();
131 for (uint32 i = 0; i < size(); ++i)
132 o << read<uint8>(i) << " - ";
133 o << " ";
134
135 TC_LOG_TRACE("network", "{}", o.str());
136}
137
139{
140 if (!sLog->ShouldLog("network", LOG_LEVEL_TRACE)) // optimize disabled trace output
141 return;
142
143 std::ostringstream o;
144 o << "STORAGE_SIZE: " << size();
145 for (uint32 i = 0; i < size(); ++i)
146 {
147 char buf[2];
148 snprintf(buf, 2, "%c", read<uint8>(i));
149 o << buf;
150 }
151 o << " ";
152 TC_LOG_TRACE("network", "{}", o.str());
153}
154
156{
157 if (!sLog->ShouldLog("network", LOG_LEVEL_TRACE)) // optimize disabled trace output
158 return;
159
160 uint32 j = 1, k = 1;
161
162 std::ostringstream o;
163 o << "STORAGE_SIZE: " << size();
164
165 for (uint32 i = 0; i < size(); ++i)
166 {
167 char buf[4];
168 snprintf(buf, 4, "%2X ", read<uint8>(i));
169 if ((i == (j * 8)) && ((i != (k * 16))))
170 {
171 o << "| ";
172 ++j;
173 }
174 else if (i == (k * 16))
175 {
176 o << "\n";
177 ++k;
178 ++j;
179 }
180
181 o << buf;
182 }
183 o << " ";
184 TC_LOG_TRACE("network", "{}", o.str());
185}
uint8_t uint8
Definition Define.h:135
uint32_t uint32
Definition Define.h:133
#define SZFMTD
Definition Define.h:123
#define ASSERT
Definition Errors.h:68
@ LOG_LEVEL_TRACE
Definition LogCommon.h:27
#define TC_LOG_TRACE(filterType__,...)
Definition Log.h:153
#define sLog
Definition Log.h:130
std::string & message() noexcept
Definition ByteBuffer.h:39
ByteBufferInvalidValueException(char const *type, char const *value)
ByteBufferPositionException(bool add, size_t pos, size_t size, size_t valueSize)
ByteBufferSourceException(size_t pos, size_t size, size_t valueSize)
size_t _wpos
Definition ByteBuffer.h:496
size_t rpos() const
Definition ByteBuffer.h:312
std::vector< uint8 > _storage
Definition ByteBuffer.h:497
void hexlike() const
void print_storage() const
void append(T value)
Definition ByteBuffer.h:133
std::string ReadCString(bool requireValidUtf8=true)
void textlike() const
void put(std::size_t pos, T value)
Definition ByteBuffer.h:141
size_t size() const
Definition ByteBuffer.h:413
ByteBuffer & operator>>(bool &value)
Definition ByteBuffer.h:234
std::string StringFormat(FormatString< Args... > fmt, Args &&... args)
Default TC string format function.