22 lines
603 B
C++
22 lines
603 B
C++
|
#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;
|
||
|
}
|