Signed-off-by: faumaray <faumaray@gmail.com>
This commit is contained in:
faumaray committed 2026-07-20 01:19:06 +03:00
1 parent 89911ff107
commit cb43116da6
3 files changed
+185 -275

No files matched your search

+1 -1
View File
@@ -38,7 +38,7 @@ target_include_directories(VoidTeamspeak PUBLIC
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>)
target_link_libraries(VoidTeamspeak
PUBLIC Void::Functional Void::Log Void::System Void::Net
PRIVATE OpenSSL::Crypto PkgConfig::SODIUM)
PRIVATE Void::Text OpenSSL::Crypto PkgConfig::SODIUM)
if(TARGET PkgConfig::OPUS)
target_link_libraries(VoidTeamspeak PRIVATE PkgConfig::OPUS)
target_compile_definitions(VoidTeamspeak PUBLIC VOID_TEAMSPEAK_HAVE_OPUS=1)
+151 -169
View File
@@ -1,184 +1,166 @@
#include "Void/TeamSpeak/Protocol/Command.hpp"
#include <Void/Text/Str.hpp>
#include <sstream>
#include <stdexcept>
namespace Void::TeamSpeak::Protocol
{
namespace Void::TeamSpeak::Protocol {
std::string tsEscape(const std::string& str)
{
std::string out;
out.reserve(str.size());
for (char c : str) {
switch (c) {
case '\\':
out += "\\\\";
break;
case '/':
out += "\\/";
break;
case ' ':
out += "\\s";
break;
case '|':
out += "\\p";
break;
case '\f':
out += "\\f";
break;
case '\n':
out += "\\n";
break;
case '\r':
out += "\\r";
break;
case '\t':
out += "\\t";
break;
case '\v':
out += "\\v";
break;
default:
out.push_back(c);
}
}
return out;
}
std::string tsEscape(const std::string &str) {
std::string out;
out.reserve(str.size());
for (char c : str) {
switch (c) {
case '\\':
out += "\\\\";
break;
case '/':
out += "\\/";
break;
case ' ':
out += "\\s";
break;
case '|':
out += "\\p";
break;
case '\f':
out += "\\f";
break;
case '\n':
out += "\\n";
break;
case '\r':
out += "\\r";
break;
case '\t':
out += "\\t";
break;
case '\v':
out += "\\v";
break;
default:
out.push_back(c);
}
}
return out;
}
std::string tsUnescape(const std::string& str)
{
std::string out;
out.reserve(str.size());
for (size_t i = 0; i < str.size(); ++i) {
if (str[i] == '\\') {
if (++i >= str.size()) {
throw std::runtime_error("invalid escape sequence");
}
switch (str[i]) {
case '\\':
out.push_back('\\');
break;
case '/':
out.push_back('/');
break;
case 's':
out.push_back(' ');
break;
case 'p':
out.push_back('|');
break;
case 'f':
out.push_back('\f');
break;
case 'n':
out.push_back('\n');
break;
case 'r':
out.push_back('\r');
break;
case 't':
out.push_back('\t');
break;
case 'v':
out.push_back('\v');
break;
default:
throw std::runtime_error(std::string("unknown escape: \\") +
str[i]);
}
} else {
out.push_back(str[i]);
}
}
return out;
}
std::string tsUnescape(const std::string &str) {
std::string out;
out.reserve(str.size());
for (size_t i = 0; i < str.size(); ++i) {
if (str[i] == '\\') {
if (++i >= str.size()) {
throw std::runtime_error("invalid escape sequence");
}
switch (str[i]) {
case '\\':
out.push_back('\\');
break;
case '/':
out.push_back('/');
break;
case 's':
out.push_back(' ');
break;
case 'p':
out.push_back('|');
break;
case 'f':
out.push_back('\f');
break;
case 'n':
out.push_back('\n');
break;
case 'r':
out.push_back('\r');
break;
case 't':
out.push_back('\t');
break;
case 'v':
out.push_back('\v');
break;
default:
throw std::runtime_error(std::string("unknown escape: \\") + str[i]);
}
} else {
out.push_back(str[i]);
}
}
return out;
}
std::string buildCommand(const std::string& name,
const CommandParams& params)
{
std::string cmd = tsEscape(name);
for (const auto& [key, value] : params) {
cmd += ' ';
cmd += key;
cmd += '=';
cmd += tsEscape(value);
}
return cmd;
}
std::string buildCommand(const std::string &name, const CommandParams &params) {
std::string cmd = tsEscape(name);
for (const auto &[key, value] : params) {
cmd += ' ';
cmd += key;
cmd += '=';
cmd += tsEscape(value);
}
return cmd;
}
namespace
{
CommandParams parseGroup(const std::string& part)
{
CommandParams params;
std::istringstream iss(part);
std::string token;
while (std::getline(iss, token, ' ')) {
if (token.empty()) {
continue;
}
auto eq = token.find('=');
if (eq != std::string::npos) {
params[token.substr(0, eq)] =
tsUnescape(token.substr(eq + 1));
} else {
params[token] = "";
}
}
return params;
}
namespace {
CommandParams parseGroup(const std::string &part) {
CommandParams params;
std::istringstream iss(part);
std::string token;
while (std::getline(iss, token, ' ')) {
if (token.empty()) {
continue;
}
auto eq = token.find('=');
if (eq != std::string::npos) {
params[token.substr(0, eq)] = tsUnescape(token.substr(eq + 1));
} else {
params[token] = "";
}
}
return params;
}
} // namespace
std::string trim(const std::string& s)
{
size_t a = s.find_first_not_of(" \t\r\n");
if (a == std::string::npos) {
return "";
}
size_t b = s.find_last_not_of(" \t\r\n");
return s.substr(a, b - a + 1);
}
} // namespace
ParsedCommand parseCommand(const std::string &raw) {
// Split on '|' into groups.
std::vector<std::string> parts;
{
std::string cur;
for (char c : raw) {
if (c == '|') {
parts.push_back(cur);
cur.clear();
} else {
cur.push_back(c);
}
}
parts.push_back(cur);
}
ParsedCommand parseCommand(const std::string& raw)
{
// Split on '|' into groups.
std::vector<std::string> parts;
{
std::string cur;
for (char c : raw) {
if (c == '|') {
parts.push_back(cur);
cur.clear();
} else {
cur.push_back(c);
}
}
parts.push_back(cur);
}
ParsedCommand out;
std::string first{Void::Str::trim(parts[0])};
ParsedCommand out;
std::string first = trim(parts[0]);
// The command name is the first whitespace-delimited token.
std::string body;
auto sp = first.find(' ');
if (sp == std::string::npos) {
out.name = tsUnescape(first);
body = "";
} else {
out.name = tsUnescape(first.substr(0, sp));
body = first.substr(sp + 1);
}
out.params = parseGroup(body);
// The command name is the first whitespace-delimited token.
std::string body;
auto sp = first.find(' ');
if (sp == std::string::npos) {
out.name = tsUnescape(first);
body = "";
} else {
out.name = tsUnescape(first.substr(0, sp));
body = first.substr(sp + 1);
}
out.params = parseGroup(body);
if (parts.size() > 1) {
out.groups.push_back(out.params);
for (size_t g = 1; g < parts.size(); ++g) {
out.groups.push_back(parseGroup(trim(parts[g])));
}
}
return out;
}
if (parts.size() > 1) {
out.groups.push_back(out.params);
for (size_t g = 1; g < parts.size(); ++g) {
out.groups.push_back(parseGroup(std::string{Void::Str::trim(parts[g])}));
}
}
return out;
}
} // namespace Void::TeamSpeak::Protocol
+33 -105
View File
@@ -1,116 +1,44 @@
#include "Void/TeamSpeak/Protocol/Util.hpp"
#include <array>
#include <Void/Text/Base64.hpp>
#include <Void/Text/Hex.hpp>
#include <cstddef>
#include <stdexcept>
namespace Void::TeamSpeak::Protocol::Util
{
namespace
{
constexpr char kEnc[] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
namespace Void::TeamSpeak::Protocol::Util {
std::string base64Encode(ByteView data) {
return Void::Encoding::Base64::encode(data);
}
std::array<int8_t, 256> makeDecodeTable()
{
std::array<int8_t, 256> t{};
t.fill(-1);
for (int i = 0; i < 64; ++i) {
t[static_cast<uint8_t>(kEnc[i])] = static_cast<int8_t>(i);
}
return t;
}
const std::array<int8_t, 256> kDec = makeDecodeTable();
} // namespace
Bytes base64Decode(const std::string &text) {
const auto decoded = Void::Encoding::Base64::decode_bytes(text);
if (!decoded) {
throw std::runtime_error("invalid base64 character");
}
std::string base64Encode(ByteView data)
{
std::string out;
out.reserve((data.size() + 2) / 3 * 4);
size_t i = 0;
for (; i + 3 <= data.size(); i += 3) {
uint32_t n = (static_cast<uint32_t>(data[i]) << 16) |
(static_cast<uint32_t>(data[i + 1]) << 8) |
static_cast<uint32_t>(data[i + 2]);
out.push_back(kEnc[(n >> 18) & 0x3f]);
out.push_back(kEnc[(n >> 12) & 0x3f]);
out.push_back(kEnc[(n >> 6) & 0x3f]);
out.push_back(kEnc[n & 0x3f]);
}
const size_t rem = data.size() - i;
if (rem == 1) {
uint32_t n = static_cast<uint32_t>(data[i]) << 16;
out.push_back(kEnc[(n >> 18) & 0x3f]);
out.push_back(kEnc[(n >> 12) & 0x3f]);
out.push_back('=');
out.push_back('=');
} else if (rem == 2) {
uint32_t n = (static_cast<uint32_t>(data[i]) << 16) |
(static_cast<uint32_t>(data[i + 1]) << 8);
out.push_back(kEnc[(n >> 18) & 0x3f]);
out.push_back(kEnc[(n >> 12) & 0x3f]);
out.push_back(kEnc[(n >> 6) & 0x3f]);
out.push_back('=');
}
return out;
}
Bytes out;
out.reserve(decoded->size());
for (const std::byte value : *decoded) {
out.push_back(std::to_integer<uint8_t>(value));
}
return out;
}
Bytes base64Decode(const std::string& text)
{
Bytes out;
out.reserve(text.size() / 4 * 3);
uint32_t buf = 0;
int bits = 0;
for (char c : text) {
if (c == '=' || c == '\n' || c == '\r' || c == ' ') {
continue;
}
int8_t v = kDec[static_cast<uint8_t>(c)];
if (v < 0) {
throw std::runtime_error("invalid base64 character");
}
buf = (buf << 6) | static_cast<uint32_t>(v);
bits += 6;
if (bits >= 8) {
bits -= 8;
out.push_back(static_cast<uint8_t>((buf >> bits) & 0xff));
}
}
return out;
}
std::string toHex(ByteView data) { return Void::Encoding::Hex::encode(data); }
std::string toHex(ByteView data)
{
static const char* h = "0123456789abcdef";
std::string out;
out.reserve(data.size() * 2);
for (uint8_t b : data) {
out.push_back(h[b >> 4]);
out.push_back(h[b & 0x0f]);
}
return out;
}
Bytes fromHex(const std::string &hex) {
const auto decoded = Void::Encoding::Hex::decode_bytes(hex);
if (!decoded) {
throw std::runtime_error("invalid hex string");
}
Bytes fromHex(const std::string& hex)
{
auto nib = [](char c) -> int {
if (c >= '0' && c <= '9')
return c - '0';
if (c >= 'a' && c <= 'f')
return c - 'a' + 10;
if (c >= 'A' && c <= 'F')
return c - 'A' + 10;
throw std::runtime_error("invalid hex character");
};
if (hex.size() % 2 != 0) {
throw std::runtime_error("odd-length hex string");
}
Bytes out;
out.reserve(hex.size() / 2);
for (size_t i = 0; i < hex.size(); i += 2) {
out.push_back(static_cast<uint8_t>((nib(hex[i]) << 4) |
nib(hex[i + 1])));
}
return out;
}
Bytes out;
out.reserve(decoded->size());
for (const std::byte value : *decoded) {
out.push_back(std::to_integer<uint8_t>(value));
}
return out;
}
} // namespace Void::TeamSpeak::Protocol::Util