stcp: ed25519 pubkey exchange in handshake, fix instance destroy segfault, fix chat_tcp_test
- u_async.c: process immediate_queue BEFORE epoll_wait in uasync_poll - stcp_link.c: defensive NULL etcp_conn/etcp_link in stcp_link_close - utun_instance.c: move stcp_server_list_destroy_all to Phase J (before ETCP), add deferred drain in both Phase J and L - stcp.h/c: STCP_HS_ENC_CLIENT 6→38, STCP_HS_ENC_SERVER 7→39, exchange ed25519_pubkey in both handshake directions - etcp_connections.c: copy peer_ed25519 from STCP to ETCP_CONN in etcp_link_enter_ready_tcp; guard link->etcp in tcp_link_close_cb - chat_sync.c: fix lk->conn NULL dereference for TCP links - test_stcp.c: update stcp_server_create/stcp_client_connect callsites - tools/chat_tcp_test: change transport udp→tcp
This commit is contained in:
1 parent
a3783b5f55
commit
38c9f229bc
1272 files changed
+968388
-1
No files matched your search
@@ -0,0 +1,154 @@
|
||||
/*
|
||||
* Copyright 2025 Axel Waggershauser
|
||||
*/
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
#include "JSON.h"
|
||||
|
||||
#include "ZXAlgorithms.h"
|
||||
|
||||
#include <string_view>
|
||||
|
||||
// This code is trying to find the value of a key-value pair in a string of those.
|
||||
// The input could be valid JSON, like '{"key": "val"}' or a stripped down version like
|
||||
// 'key:val'. This is also compatible with the string serialization of a python dictionary.
|
||||
// For convenience, also 'key=val' is supported, key is checked case insensitive and '_' is ignored.
|
||||
// This could easily be done with the following regex (see below).
|
||||
// But using std::regex adds 140k+ to the binary size. ctre is _very_ nice to use and has
|
||||
// the same binary footprint as the hand-roled code below! But I don't want to add
|
||||
// 5k lines of c++ code for that.
|
||||
|
||||
// #define ZXING_USE_CTRE
|
||||
#ifdef ZXING_USE_CTRE
|
||||
#pragma GCC diagnostic ignored "-Wundef" // see https://github.com/hanickadot/compile-time-regular-expressions/issues/340
|
||||
#include "ctre.hpp"
|
||||
#pragma GCC diagnostic error "-Wundef"
|
||||
|
||||
static constexpr auto PATTERN =
|
||||
ctll::fixed_string{R"(["']?([[:alpha:]][[:alnum:]]*)["']?\s*(?:[:]\s*["']?([[:alnum:]]+)["']?)?(?:,|\}|$))"};
|
||||
#endif
|
||||
|
||||
namespace ZXing {
|
||||
|
||||
// Trim whitespace and quotes/braces from both ends
|
||||
inline std::string_view Trim(std::string_view sv)
|
||||
{
|
||||
return TrimWS(sv, " \t\n\r\"'{}");
|
||||
}
|
||||
|
||||
inline bool IsEqualIgnoreCaseAndUnderscore(std::string_view a, std::string_view b)
|
||||
{
|
||||
return IsEqualIgnoreCaseAnd(a, b, "_");
|
||||
}
|
||||
|
||||
std::string_view JsonFind(std::string_view json, std::string_view key)
|
||||
{
|
||||
#ifdef ZXING_USE_CTRE
|
||||
for (auto [ma, mk, mv] : ctre::search_all<PATTERN>(json))
|
||||
if (IsEqualCaseInsensitive(key, mk))
|
||||
return mv.size() ? mv.to_view() : std::string_view(mk.data(), 0);
|
||||
|
||||
return {};
|
||||
#else
|
||||
json = Trim(json);
|
||||
|
||||
// TODO: use something like ForEachToken here?
|
||||
while (!json.empty()) {
|
||||
auto posComma = json.find(',');
|
||||
auto pair = Trim(json.substr(0, posComma));
|
||||
|
||||
if (IsEqualIgnoreCaseAndUnderscore(pair, key))
|
||||
return {pair.data(), 0}; // return non-null data to signal that we found the key
|
||||
|
||||
auto posSep = pair.find_first_of(":=");
|
||||
|
||||
if (posSep != std::string_view::npos && IsEqualIgnoreCaseAndUnderscore(Trim(pair.substr(0, posSep)), key))
|
||||
return Trim(pair.substr(posSep + 1));
|
||||
|
||||
json = (posComma == std::string_view::npos) ? std::string_view{} : json.substr(posComma + 1);
|
||||
}
|
||||
|
||||
return {};
|
||||
#endif
|
||||
}
|
||||
|
||||
std::string JsonEscapeStr(std::string_view str)
|
||||
{
|
||||
std::string res;
|
||||
res.reserve(str.size() + 10);
|
||||
|
||||
for (unsigned char c : str) {
|
||||
switch (c) {
|
||||
case '\"': res += "\\\""; break;
|
||||
case '\\': res += "\\\\"; break;
|
||||
case '\b': res += "\\b"; break;
|
||||
case '\f': res += "\\f"; break;
|
||||
case '\n': res += "\\n"; break;
|
||||
case '\r': res += "\\r"; break;
|
||||
case '\t': res += "\\t"; break;
|
||||
default:
|
||||
if (c <= 0x1F) {
|
||||
char buf[7];
|
||||
std::snprintf(buf, sizeof(buf), "\\u%04X", c);
|
||||
res += buf;
|
||||
} else {
|
||||
res += c;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
std::string JsonUnEscapeStr(std::string_view str)
|
||||
{
|
||||
std::string res;
|
||||
res.reserve(str.size());
|
||||
|
||||
for (size_t i = 0; i < str.size(); ++i) {
|
||||
char c = str[i];
|
||||
if (c == '\\') {
|
||||
if (++i >= str.size())
|
||||
throw std::runtime_error("Invalid escape sequence");
|
||||
|
||||
char esc = str[i];
|
||||
switch (esc) {
|
||||
case '"': res += '\"'; break;
|
||||
case '\\': res += '\\'; break;
|
||||
case '/': res += '/'; break;
|
||||
case 'b': res += '\b'; break;
|
||||
case 'f': res += '\f'; break;
|
||||
case 'n': res += '\n'; break;
|
||||
case 'r': res += '\r'; break;
|
||||
case 't': res += '\t'; break;
|
||||
case 'u': {
|
||||
if (i + 4 >= str.size())
|
||||
throw std::runtime_error("Incomplete \\u escape");
|
||||
|
||||
uint32_t code = 0;
|
||||
auto first = str.data() + i + 1;
|
||||
auto last = first + 4;
|
||||
|
||||
auto [ptr, ec] = std::from_chars(first, last, code, 16);
|
||||
if (ec != std::errc() || ptr != last)
|
||||
throw std::runtime_error("Failed to parse hex code");
|
||||
|
||||
if (code > 0x1F)
|
||||
throw std::runtime_error("Unexpected code point in \\u escape");
|
||||
|
||||
res += static_cast<char>(code);
|
||||
i += 4;
|
||||
break;
|
||||
}
|
||||
default: throw std::runtime_error("Unknown escape sequence");
|
||||
}
|
||||
} else {
|
||||
res += c;
|
||||
}
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
} // ZXing
|
||||
Reference in new issue
Block a user