From beee3cc369e712124c4b2bb41898569eb270fd48 Mon Sep 17 00:00:00 2001 From: Jonathan G Rennison Date: Mon, 13 Nov 2023 23:26:05 +0000 Subject: [PATCH] Replace BytesToHexString with FormatArrayAsHex --- src/misc.cpp | 4 ++-- src/network/network.cpp | 20 +++----------------- src/network/network_internal.h | 1 - src/network/network_survey.cpp | 3 ++- src/string.cpp | 19 +++++++++++++++++++ src/string_func.h | 3 +++ 6 files changed, 29 insertions(+), 21 deletions(-) diff --git a/src/misc.cpp b/src/misc.cpp index 04d615a7c2a..1c12bc35ce1 100644 --- a/src/misc.cpp +++ b/src/misc.cpp @@ -40,6 +40,7 @@ #include "cargopacket.h" #include "tbtr_template_vehicle_func.h" #include "event_logs.h" +#include "string_func.h" #include "3rdparty/monocypher/monocypher.h" #include "safeguards.h" @@ -77,7 +78,6 @@ void InitializeOldNames(); std::string GenerateUid(std::string_view subject) { extern void NetworkRandomBytesWithFallback(void *buf, size_t n); - extern std::string BytesToHexString(const byte *data, size_t length); uint8 random_bytes[32]; NetworkRandomBytesWithFallback(random_bytes, lengthof(random_bytes)); @@ -90,7 +90,7 @@ std::string GenerateUid(std::string_view subject) crypto_blake2b_update(&ctx, (const byte *)subject.data(), subject.size()); crypto_blake2b_final (&ctx, digest); - return BytesToHexString(digest, lengthof(digest)); + return FormatArrayAsHex({digest, lengthof(digest)}); } /** diff --git a/src/network/network.cpp b/src/network/network.cpp index 5e308fef7d9..afcbf8df906 100644 --- a/src/network/network.cpp +++ b/src/network/network.cpp @@ -36,6 +36,7 @@ #include "../gfx_func.h" #include "../error.h" #include "../core/checksum_func.hpp" +#include "../string_func.h" #include "../string_func_extra.h" #include "../core/serialisation.hpp" #include "../3rdparty/randombytes/randombytes.h" @@ -215,7 +216,7 @@ std::string GenerateCompanyPasswordHash(const std::string &password, const std:: checksum.Append(salted_password_string.data(), salted_password_string.size()); checksum.Finish(digest); - return BytesToHexString(digest.data(), digest.size()); + return FormatArrayAsHex(digest); } /** @@ -1340,27 +1341,12 @@ static void NetworkGenerateServerId() _settings_client.network.network_id = GenerateUid("OpenTTD Server ID"); } -std::string BytesToHexString(const byte *data, size_t length) -{ - std::string hex_output; - hex_output.resize(length * 2); - - char txt[3]; - for (uint i = 0; i < length; ++i) { - seprintf(txt, lastof(txt), "%02x", data[i]); - hex_output[i * 2] = txt[0]; - hex_output[(i * 2) + 1] = txt[1]; - } - - return hex_output; -} - std::string NetworkGenerateRandomKeyString(uint bytes) { uint8 *key = AllocaM(uint8, bytes); NetworkRandomBytesWithFallback(key, bytes); - return BytesToHexString(key, bytes); + return FormatArrayAsHex({key, bytes}); } class TCPNetworkDebugConnecter : TCPConnecter { diff --git a/src/network/network_internal.h b/src/network/network_internal.h index dfe8ba8ccbd..abdbf224cd8 100644 --- a/src/network/network_internal.h +++ b/src/network/network_internal.h @@ -142,7 +142,6 @@ StringID GetNetworkErrorMsg(NetworkErrorCode err); bool NetworkMakeClientNameUnique(std::string &new_name); std::string GenerateCompanyPasswordHash(const std::string &password, const std::string &password_server_id, uint32 password_game_seed); std::vector GenerateGeneralPasswordHash(const std::string &password, const std::string &password_server_id, uint64 password_game_seed); -std::string BytesToHexString(const byte *data, size_t length); std::string NetworkGenerateRandomKeyString(uint bytes); std::string_view ParseCompanyFromConnectionString(const std::string &connection_string, CompanyID *company_id); diff --git a/src/network/network_survey.cpp b/src/network/network_survey.cpp index 59f361ba12c..1b173931a32 100644 --- a/src/network/network_survey.cpp +++ b/src/network/network_survey.cpp @@ -20,6 +20,7 @@ #include "../timer/timer_game_tick.h" #include "../sl/saveload.h" #include "../date_func.h" +#include "../string_func.h" #include "../currency.h" #include "../fontcache.h" @@ -237,7 +238,7 @@ static void SurveyGrfs(nlohmann::json &survey) auto grfid = fmt::format("{:08x}", BSWAP32(c->ident.grfid)); auto &grf = survey[grfid]; - grf["md5sum"] = BytesToHexString(c->ident.md5sum.data(), c->ident.md5sum.size()); + grf["md5sum"] = FormatArrayAsHex(c->ident.md5sum); grf["status"] = c->status; if ((c->palette & GRFP_GRF_MASK) == GRFP_GRF_UNSET) grf["palette"] = "unset"; diff --git a/src/string.cpp b/src/string.cpp index 48d1e1ce152..229819c9e69 100644 --- a/src/string.cpp +++ b/src/string.cpp @@ -191,6 +191,25 @@ const char *str_fix_scc_encoded(char *str, const char *last) return str; } +/** + * Format a byte array into a continuous hex string. + * @param data Array to format + * @return Converted string. + */ +std::string FormatArrayAsHex(span data) +{ + std::string hex_output; + hex_output.resize(data.size() * 2); + + char txt[3]; + for (uint i = 0; i < data.size(); ++i) { + seprintf(txt, lastof(txt), "%02x", data[i]); + hex_output[i * 2] = txt[0]; + hex_output[(i * 2) + 1] = txt[1]; + } + + return hex_output; +} /** * Copies the valid (UTF-8) characters from \c str up to \c last to the \c dst. diff --git a/src/string_func.h b/src/string_func.h index 64753b358e0..e106aad5a3c 100644 --- a/src/string_func.h +++ b/src/string_func.h @@ -28,6 +28,7 @@ #include #include "core/bitmath_func.hpp" +#include "core/span_type.hpp" #include "string_type.h" char *strecat(char *dst, const char *src, const char *last) NOACCESS(3); @@ -40,6 +41,8 @@ int CDECL vseprintf(char *str, const char *last, const char *format, va_list ap) std::string CDECL stdstr_fmt(const char *str, ...) WARN_FORMAT(1, 2); std::string stdstr_vfmt(const char *str, va_list va) WARN_FORMAT(1, 0); +std::string FormatArrayAsHex(span data); + char *StrMakeValidInPlace(char *str, const char *last, StringValidationSettings settings = SVS_REPLACE_WITH_QUESTION_MARK) NOACCESS(2); [[nodiscard]] std::string StrMakeValid(std::string_view str, StringValidationSettings settings = SVS_REPLACE_WITH_QUESTION_MARK); void StrMakeValidInPlace(char *str, StringValidationSettings settings = SVS_REPLACE_WITH_QUESTION_MARK);