Merge branch 'master' into jgrpp
# Conflicts: # .github/workflows/ci-build.yml # .github/workflows/codeql.yml # .github/workflows/commit-checker.yml # .github/workflows/release-linux-legacy.yml # .github/workflows/release-linux.yml # .github/workflows/release-macos.yml # .github/workflows/release-windows-store.yml # .github/workflows/release-windows.yml # .github/workflows/upload-cdn.yml # .github/workflows/upload-gog.yml # .github/workflows/upload-steam.yml # src/console_cmds.cpp # src/core/math_func.hpp # src/fios.cpp # src/fios.h # src/intro_gui.cpp # src/network/network_server.cpp # src/openttd.cpp # src/settings.cpp # src/settings_gui.cpp # src/settings_internal.h # src/settings_table.cpp # src/settings_type.h # src/table/settings.h.preamble # src/table/settings/company_settings.ini # src/table/settings/currency_settings.ini # src/table/settings/difficulty_settings.ini # src/table/settings/economy_settings.ini # src/table/settings/game_settings.ini # src/table/settings/gui_settings.ini # src/table/settings/linkgraph_settings.ini # src/table/settings/locale_settings.ini # src/table/settings/misc_settings.ini # src/table/settings/multimedia_settings.ini # src/table/settings/network_private_settings.ini # src/table/settings/network_settings.ini # src/table/settings/news_display_settings.ini # src/table/settings/old_gameopt_settings.ini # src/table/settings/pathfinding_settings.ini # src/table/settings/script_settings.ini # src/table/settings/win32_settings.ini # src/table/settings/window_settings.ini # src/table/settings/world_settings.ini # src/viewport.cpp # src/viewport_func.h # src/window.cpp
This commit is contained in:
@@ -1069,6 +1069,55 @@ static int ICUStringContains(const std::string_view str, const std::string_view
|
||||
return ci_str.find(ci_value) != CaseInsensitiveStringView::npos;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert a single hex-nibble to a byte.
|
||||
*
|
||||
* @param c The hex-nibble to convert.
|
||||
* @return The byte the hex-nibble represents, or -1 if it is not a valid hex-nibble.
|
||||
*/
|
||||
static int ConvertHexNibbleToByte(char c)
|
||||
{
|
||||
if (c >= '0' && c <= '9') return c - '0';
|
||||
if (c >= 'A' && c <= 'F') return c + 10 - 'A';
|
||||
if (c >= 'a' && c <= 'f') return c + 10 - 'a';
|
||||
return -1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert a hex-string to a byte-array, while validating it was actually hex.
|
||||
*
|
||||
* @param hex The hex-string to convert.
|
||||
* @param bytes The byte-array to write the result to.
|
||||
*
|
||||
* @note The length of the hex-string has to be exactly twice that of the length
|
||||
* of the byte-array, otherwise conversion will fail.
|
||||
*
|
||||
* @return True iff the hex-string was valid and the conversion succeeded.
|
||||
*/
|
||||
bool ConvertHexToBytes(std::string_view hex, std::span<uint8_t> bytes)
|
||||
{
|
||||
if (bytes.size() != hex.size() / 2) {
|
||||
return false;
|
||||
}
|
||||
|
||||
/* Hex-string lengths are always divisible by 2. */
|
||||
if (hex.size() % 2 != 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < hex.size() / 2; i++) {
|
||||
auto hi = ConvertHexNibbleToByte(hex[i * 2]);
|
||||
auto lo = ConvertHexNibbleToByte(hex[i * 2 + 1]);
|
||||
|
||||
if (hi < 0 || lo < 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
bytes[i] = (hi << 4) | lo;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
#ifdef WITH_UNISCRIBE
|
||||
|
||||
|
Reference in New Issue
Block a user