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