Skip to content

Commit

Permalink
Replace BytesToHexString with FormatArrayAsHex
Browse files Browse the repository at this point in the history
  • Loading branch information
JGRennison committed Nov 13, 2023
1 parent 6a35661 commit beee3cc
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 21 deletions.
4 changes: 2 additions & 2 deletions src/misc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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));
Expand All @@ -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)});
}

/**
Expand Down
20 changes: 3 additions & 17 deletions src/network/network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -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 {
Expand Down
1 change: 0 additions & 1 deletion src/network/network_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<uint8> 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);
Expand Down
3 changes: 2 additions & 1 deletion src/network/network_survey.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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";
Expand Down
19 changes: 19 additions & 0 deletions src/string.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<const byte> 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.
Expand Down
3 changes: 3 additions & 0 deletions src/string_func.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include <iosfwd>

#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);
Expand All @@ -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<const byte> 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);
Expand Down

0 comments on commit beee3cc

Please sign in to comment.