-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpacket_buf.cpp
More file actions
203 lines (148 loc) · 4.81 KB
/
Copy pathpacket_buf.cpp
File metadata and controls
203 lines (148 loc) · 4.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
//
// Created by Micael Cossa on 25/07/2025.
//
#include "packet_buf.h"
#include <algorithm>
#include <chrono>
#include <iostream>
#define MAXIMUM_VARINT_BITS 5
#define COMPLETION_BIT_MASK 0x80
#define SEGMENT_BITS 0x7F
constexpr int UUID_SIZE = 16;
int ReadPacketBuffer::readVarInt() {
int final_result = 0, shifts = 0;
while(shifts < MAXIMUM_VARINT_BITS && position < size) {
unsigned char currentByte = buffer[position];
final_result |= (currentByte & SEGMENT_BITS) << (shifts * 7);
position++;
if ((currentByte & COMPLETION_BIT_MASK) == 0) break;
shifts++;
}
return final_result;
}
char *ReadPacketBuffer::getBuffer() {
return buffer;
}
int ReadPacketBuffer::getSize() {
return size;
}
long long int ReadPacketBuffer::readLong() {
int length = sizeof(long long int);
if(position + length > size) {
printf("Unable to readLong, buffer size not big enough position: %d , bufferSize: %d \n", (position + length), size);
return 0;
}
long long value;
for (int i = 0; i < length; i++) {
reinterpret_cast<unsigned char*>(&value)[i] =
static_cast<unsigned char>(buffer[position + (7 - i)]);
}
position+=length;
return value;
}
std::string ReadPacketBuffer::readString() {
int stringLength = readVarInt();
if(position + stringLength > size) {
printf("String length bigger than buffer size , Position: %d String Length: %d Buffer Size: %d", position, stringLength, size);
return "";
}
std::string result;
result.reserve(stringLength); // Pre-allocate to avoid reallocations
result.assign(buffer + position, buffer + position + stringLength);
position += stringLength;
return result;
}
short ReadPacketBuffer::readShort() {
int length = sizeof(short);
if(position + length > size) {
printf("Unable to readShort, buffer size not big enough position: %d , bufferSize: %d \n", (position + length), size);
return 0;
}
short value;
for (int i = 0; i < length; i++) {
reinterpret_cast<unsigned char*>(&value)[i] =
static_cast<unsigned char>(buffer[position + (1 - i)]);
}
position+=length;
return value;
}
std::string ReadPacketBuffer::readUUID() {
if(position + UUID_SIZE > size) {
printf("UUID Length bigger than buffer size , Position: %d Buffer Size: %d", position, size);
return "";
}
std::string result;
result.reserve(UUID_SIZE); // Pre-allocate to avoid reallocations
result.assign(buffer + position, buffer + position + UUID_SIZE);
return result;
}
// Pre-allocate a buffer of at least 'n' bytes
void WritePacketBuffer::reserve(size_t n) {
if (capacity < n) {
char* newBuffer = new char[n];
if (buffer != nullptr) {
memcpy(newBuffer, buffer, currPos); // preserve existing data
delete[] buffer;
}
buffer = newBuffer;
capacity = n;
}
}
size_t WritePacketBuffer::getSize() {
return currPos;
}
void WritePacketBuffer::writeByte(const char& byte) {
if (currPos >= capacity) {
// grow buffer exponentially if needed
reserve(capacity > 0 ? capacity * 2 : 512);
}
buffer[currPos++] = byte;
}
void WritePacketBuffer::writeVarIntAtTheFront(int value) {
// Determine the number of bytes needed for the VarInt
size_t varIntSize = 0;
int temp = value;
do {
temp >>= 7;
varIntSize++;
} while (temp != 0);
// Make sure there’s enough space
if (currPos + varIntSize > capacity) {
reserve(currPos + varIntSize);
}
// Shift existing data forward to make room for the VarInt
memmove(buffer + varIntSize, buffer, currPos);
// Write VarInt bytes at the beginning
size_t pos = 0;
do {
int transfer_bits = value & SEGMENT_BITS;
value >>= 7;
if (value != 0)
transfer_bits |= COMPLETION_BIT_MASK;
buffer[pos++] = static_cast<char>(transfer_bits);
} while (value != 0);
// Update current position
currPos += varIntSize;
}
void WritePacketBuffer::writeString(const std::string &value) {
// Write Size
writeVarInt(static_cast<int>(value.length()));
// Write UTF-8 Characters
std::for_each(value.begin(), value.end(), [this](const char& c) {
this->writeByte(c);
});
}
void WritePacketBuffer::writeVarInt(int value) {
do {
int transfer_bits = (value & SEGMENT_BITS);
value >>= 7;
if(value != 0)
transfer_bits |= COMPLETION_BIT_MASK;
writeByte(static_cast<char>(transfer_bits));
} while(value != 0);
}
void WritePacketBuffer::writeLong(const long long int value) {
reserve(currPos + sizeof(value));
memcpy(buffer + currPos, &value, sizeof(value));
currPos += sizeof(value);
}