diff --git a/src/console_cmds.cpp b/src/console_cmds.cpp index bc3a4434fa..267fa1b5d7 100644 --- a/src/console_cmds.cpp +++ b/src/console_cmds.cpp @@ -1899,9 +1899,7 @@ static void OutputContentState(const ContentInfo *const ci) static const char * const states[] = { "Not selected", "Selected", "Dep Selected", "Installed", "Unknown" }; static const TextColour state_to_colour[] = { CC_COMMAND, CC_INFO, CC_INFO, CC_WHITE, CC_ERROR }; - char buf[sizeof(ci->md5sum) * 2 + 1]; - md5sumToString(buf, lastof(buf), ci->md5sum); - IConsolePrint(state_to_colour[ci->state], "{}, {}, {}, {}, {:08X}, {}", ci->id, types[ci->type - 1], states[ci->state], ci->name, ci->unique_id, buf); + IConsolePrint(state_to_colour[ci->state], "{}, {}, {}, {}, {:08X}, {}", ci->id, types[ci->type - 1], states[ci->state], ci->name, ci->unique_id, MD5SumToString(ci->md5sum)); } DEF_CONSOLE_CMD(ConContent) diff --git a/src/gamelog.cpp b/src/gamelog.cpp index 8477ab90cd..6a4632fcb7 100644 --- a/src/gamelog.cpp +++ b/src/gamelog.cpp @@ -125,9 +125,7 @@ template static void AddGrfInfo(T &outputIterator, uint grfid, const uint8 *md5sum, const GRFConfig *gc) { if (md5sum != nullptr) { - char txt[40]; - md5sumToString(txt, lastof(txt), md5sum); - fmt::format_to(outputIterator, "GRF ID {:08X}, checksum {}", BSWAP32(grfid), txt); + fmt::format_to(outputIterator, "GRF ID {:08X}, checksum {}", BSWAP32(grfid), MD5SumToString(md5sum)); } else { fmt::format_to(outputIterator, "GRF ID {:08X}", BSWAP32(grfid)); } diff --git a/src/network/network_client.cpp b/src/network/network_client.cpp index 3bfdaa118d..6ffecebe1e 100644 --- a/src/network/network_client.cpp +++ b/src/network/network_client.cpp @@ -666,9 +666,7 @@ NetworkRecvStatus ClientNetworkGameSocketHandler::Receive_SERVER_CHECK_NEWGRFS(P const GRFConfig *f = FindGRFConfig(c.grfid, FGCM_EXACT, c.md5sum); if (f == nullptr) { /* We do not know this GRF, bail out of initialization */ - char buf[sizeof(c.md5sum) * 2 + 1]; - md5sumToString(buf, lastof(buf), c.md5sum); - Debug(grf, 0, "NewGRF {:08X} not found; checksum {}", BSWAP32(c.grfid), buf); + Debug(grf, 0, "NewGRF {:08X} not found; checksum {}", BSWAP32(c.grfid), MD5SumToString(c.md5sum)); ret = NETWORK_RECV_STATUS_NEWGRF_MISMATCH; } } diff --git a/src/network/network_content_gui.cpp b/src/network/network_content_gui.cpp index dcafc80112..3f8c97e348 100644 --- a/src/network/network_content_gui.cpp +++ b/src/network/network_content_gui.cpp @@ -365,9 +365,7 @@ class NetworkContentListWindow : public Window, ContentCallback { if (!first) url.push_back(','); first = false; - char buf[33]; - md5sumToString(buf, lastof(buf), ci->md5sum); - fmt::format_to(std::back_inserter(url), "{:08X}:{}", ci->unique_id, buf); + fmt::format_to(std::back_inserter(url), "{:08X}:{}", ci->unique_id, MD5SumToString(ci->md5sum)); } } else { url += "do=searchtext&q="; diff --git a/src/newgrf_config.cpp b/src/newgrf_config.cpp index 0391e2c000..d51bb48a5b 100644 --- a/src/newgrf_config.cpp +++ b/src/newgrf_config.cpp @@ -517,14 +517,11 @@ GRFListCompatibility IsGoodGRFConfigList(GRFConfig *grfconfig) for (GRFConfig *c = grfconfig; c != nullptr; c = c->next) { const GRFConfig *f = FindGRFConfig(c->ident.grfid, FGCM_EXACT, c->ident.md5sum); if (f == nullptr || HasBit(f->flags, GCF_INVALID)) { - char buf[256]; - /* If we have not found the exactly matching GRF try to find one with the * same grfid, as it most likely is compatible */ f = FindGRFConfig(c->ident.grfid, FGCM_COMPATIBLE, nullptr, c->version); if (f != nullptr) { - md5sumToString(buf, lastof(buf), c->ident.md5sum); - Debug(grf, 1, "NewGRF {:08X} ({}) not found; checksum {}. Compatibility mode on", BSWAP32(c->ident.grfid), c->filename, buf); + Debug(grf, 1, "NewGRF {:08X} ({}) not found; checksum {}. Compatibility mode on", BSWAP32(c->ident.grfid), c->filename, MD5SumToString(c->ident.md5sum)); if (!HasBit(c->flags, GCF_COMPATIBLE)) { /* Preserve original_md5sum after it has been assigned */ SetBit(c->flags, GCF_COMPATIBLE); @@ -537,8 +534,7 @@ GRFListCompatibility IsGoodGRFConfigList(GRFConfig *grfconfig) } /* No compatible grf was found, mark it as disabled */ - md5sumToString(buf, lastof(buf), c->ident.md5sum); - Debug(grf, 0, "NewGRF {:08X} ({}) not found; checksum {}", BSWAP32(c->ident.grfid), c->filename, buf); + Debug(grf, 0, "NewGRF {:08X} ({}) not found; checksum {}", BSWAP32(c->ident.grfid), c->filename, MD5SumToString(c->ident.md5sum)); c->status = GCS_NOT_FOUND; res = GLC_NOT_FOUND; diff --git a/src/newgrf_gui.cpp b/src/newgrf_gui.cpp index 0181556a86..0cd608de4a 100644 --- a/src/newgrf_gui.cpp +++ b/src/newgrf_gui.cpp @@ -92,9 +92,8 @@ static void ShowNewGRFInfo(const GRFConfig *c, const Rect &r, bool show_params) } /* Prepare and draw GRF ID */ - char buff[256]; - seprintf(buff, lastof(buff), "%08X", BSWAP32(c->ident.grfid)); - SetDParamStr(0, buff); + std::string tmp = fmt::format("{:08X}", BSWAP32(c->ident.grfid)); + SetDParamStr(0, tmp); tr.top = DrawStringMultiLine(tr, STR_NEWGRF_SETTINGS_GRF_ID); if ((_settings_client.gui.newgrf_developer_tools || _settings_client.gui.newgrf_show_old_versions) && c->version != 0) { @@ -107,13 +106,14 @@ static void ShowNewGRFInfo(const GRFConfig *c, const Rect &r, bool show_params) } /* Prepare and draw MD5 sum */ - md5sumToString(buff, lastof(buff), c->ident.md5sum); - SetDParamStr(0, buff); + tmp = MD5SumToString(c->ident.md5sum); + SetDParamStr(0, tmp); tr.top = DrawStringMultiLine(tr, STR_NEWGRF_SETTINGS_MD5SUM); /* Show GRF parameter list */ if (show_params) { if (c->num_params > 0) { + char buff[256]; GRFBuildParamList(buff, c, lastof(buff)); SetDParam(0, STR_JUST_RAW_STRING); SetDParamStr(1, buff); diff --git a/src/openttd.cpp b/src/openttd.cpp index 52a466f78d..97face4e6c 100644 --- a/src/openttd.cpp +++ b/src/openttd.cpp @@ -257,9 +257,8 @@ static void WriteSavegameInfo(const char *name) message += "NewGRFs:\n"; if (_load_check_data.HasNewGrfs()) { for (GRFConfig *c = _load_check_data.grfconfig; c != nullptr; c = c->next) { - char md5sum[33]; - md5sumToString(md5sum, lastof(md5sum), HasBit(c->flags, GCF_COMPATIBLE) ? c->original_md5sum : c->ident.md5sum); - fmt::format_to(std::back_inserter(message), "{:08X} {} {}\n", c->ident.grfid, md5sum, c->filename); + fmt::format_to(std::back_inserter(message), "{:08X} {} {}\n", c->ident.grfid, + MD5SumToString(HasBit(c->flags, GCF_COMPATIBLE) ? c->original_md5sum : c->ident.md5sum), c->filename); } } diff --git a/src/saveload/afterload.cpp b/src/saveload/afterload.cpp index 0b8ae1cff7..822de66802 100644 --- a/src/saveload/afterload.cpp +++ b/src/saveload/afterload.cpp @@ -388,16 +388,12 @@ static void CDECL HandleSavegameLoadCrash(int signum) for (const GRFConfig *c = _grfconfig; c != nullptr; c = c->next) { if (HasBit(c->flags, GCF_COMPATIBLE)) { const GRFIdentifier *replaced = _gamelog.GetOverriddenIdentifier(c); - char original_md5[40]; - char replaced_md5[40]; - md5sumToString(original_md5, lastof(original_md5), c->original_md5sum); - md5sumToString(replaced_md5, lastof(replaced_md5), replaced->md5sum); - fmt::format_to(std::back_inserter(message), "NewGRF {:08X} (checksum {}) not found.\n Loaded NewGRF \"{}\" (checksum {}) with same GRF ID instead.\n", BSWAP32(c->ident.grfid), original_md5, c->filename, replaced_md5); + fmt::format_to(std::back_inserter(message), "NewGRF {:08X} (checksum {}) not found.\n Loaded NewGRF \"{}\" (checksum {}) with same GRF ID instead.\n", + BSWAP32(c->ident.grfid), MD5SumToString(c->original_md5sum), c->filename, MD5SumToString(replaced->md5sum)); } if (c->status == GCS_NOT_FOUND) { - char buf[40]; - md5sumToString(buf, lastof(buf), c->ident.md5sum); - fmt::format_to(std::back_inserter(message), "NewGRF {:08X} ({}) not found; checksum {}.\n", BSWAP32(c->ident.grfid), c->filename, buf); + fmt::format_to(std::back_inserter(message), "NewGRF {:08X} ({}) not found; checksum {}.\n", + BSWAP32(c->ident.grfid), c->filename, MD5SumToString(c->ident.md5sum)); } } } else { diff --git a/src/screenshot.cpp b/src/screenshot.cpp index f0176b31d5..2f3a8257cb 100644 --- a/src/screenshot.cpp +++ b/src/screenshot.cpp @@ -320,9 +320,7 @@ static bool MakePNGImage(const char *name, ScreenshotCallback *callb, void *user fmt::format_to(std::back_inserter(message), "Graphics set: {} ({})\n", BaseGraphics::GetUsedSet()->name, BaseGraphics::GetUsedSet()->version); message += "NewGRFs:\n"; for (const GRFConfig *c = _game_mode == GM_MENU ? nullptr : _grfconfig; c != nullptr; c = c->next) { - char buf[33]; - md5sumToString(buf, lastof(buf), c->ident.md5sum); - fmt::format_to(std::back_inserter(message), "{:08X} {} {}\n", BSWAP32(c->ident.grfid), buf, c->filename); + fmt::format_to(std::back_inserter(message), "{:08X} {} {}\n", BSWAP32(c->ident.grfid), MD5SumToString(c->ident.md5sum), c->filename); } message += "\nCompanies:\n"; for (const Company *c : Company::Iterate()) { diff --git a/src/settings.cpp b/src/settings.cpp index 6dfd8e240d..d650d5594d 100644 --- a/src/settings.cpp +++ b/src/settings.cpp @@ -1135,14 +1135,11 @@ static void GRFSaveConfig(IniFile &ini, const char *grpname, const GRFConfig *li const GRFConfig *c; for (c = list; c != nullptr; c = c->next) { - /* Hex grfid (4 bytes in nibbles), "|", hex md5sum (16 bytes in nibbles), "|", file system path. */ - char key[4 * 2 + 1 + 16 * 2 + 1 + MAX_PATH]; char params[512]; GRFBuildParamList(params, c, lastof(params)); - char *pos = key + seprintf(key, lastof(key), "%08X|", BSWAP32(c->ident.grfid)); - pos = md5sumToString(pos, lastof(key), c->ident.md5sum); - seprintf(pos, lastof(key), "|%s", c->filename); + std::string key = fmt::format("{:08X}|{}|{}", BSWAP32(c->ident.grfid), + MD5SumToString(c->ident.md5sum), c->filename); group->GetItem(key, true)->SetValue(params); } } diff --git a/src/string.cpp b/src/string.cpp index 55a7ad4ced..35b3dc5b3d 100644 --- a/src/string.cpp +++ b/src/string.cpp @@ -612,20 +612,12 @@ int CDECL seprintf(char *str, const char *last, const char *format, ...) /** * Convert the md5sum to a hexadecimal string representation - * @param buf buffer to put the md5sum into - * @param last last character of buffer (usually lastof(buf)) * @param md5sum the md5sum itself - * @return a pointer to the next character after the md5sum + * @return the string representation of the md5sum. */ -char *md5sumToString(char *buf, const char *last, const uint8 md5sum[16]) +std::string MD5SumToString(const uint8 md5sum[16]) { - char *p = buf; - - for (uint i = 0; i < 16; i++) { - p += seprintf(p, last, "%02X", md5sum[i]); - } - - return p; + return FormatArrayAsHex({md5sum, 16}); } diff --git a/src/string_func.h b/src/string_func.h index 1abbd470f1..c03101c213 100644 --- a/src/string_func.h +++ b/src/string_func.h @@ -86,7 +86,7 @@ static inline size_t ttd_strnlen(const char *str, size_t maxlen) return t - str; } -char *md5sumToString(char *buf, const char *last, const uint8 md5sum[16]); +std::string MD5SumToString(const uint8 md5sum[16]); bool IsValidChar(WChar key, CharSetFilter afilter);