mcpinger/protocol_utils.cpp

22 lines
603 B
C++
Raw Normal View History

2024-05-08 10:24:32 +02:00
#include "protocol_utils.h"
std::vector<unsigned char> encodeVarint(int value) {
std::vector<unsigned char> varint;
auto uValue = static_cast<unsigned int>(value);
do {
unsigned char temp = uValue & 0x7FU;
uValue >>= 7;
if (uValue != 0) {
temp |= 0x80;
}
varint.push_back(temp);
} while (uValue != 0);
return varint;
}
std::vector<unsigned char> encodeString(const std::string& str) {
std::vector<unsigned char> data = encodeVarint(str.length());
(void)data.insert(data.end(), str.begin(), str.end());
return data;
}