From 4c53fcdf39004ed67b8ff0d3c660ec7b191e55e6 Mon Sep 17 00:00:00 2001 From: Vitaliy Filippov Date: Wed, 11 Feb 2026 01:31:58 +0300 Subject: [PATCH] Move fromhexstr() to str_util --- src/disk_tool/disk_tool.h | 1 - src/disk_tool/disk_tool_journal.cpp | 1 + src/disk_tool/disk_tool_meta.cpp | 1 + src/disk_tool/disk_tool_utils.cpp | 22 ---------------------- src/util/str_util.cpp | 21 +++++++++++++++++++++ src/util/str_util.h | 2 ++ 6 files changed, 25 insertions(+), 23 deletions(-) diff --git a/src/disk_tool/disk_tool.h b/src/disk_tool/disk_tool.h index 1853cdf6..0aceab06 100644 --- a/src/disk_tool/disk_tool.h +++ b/src/disk_tool/disk_tool.h @@ -175,7 +175,6 @@ struct disk_tool_t void disk_tool_simple_offsets(json11::Json cfg, bool json_output); uint64_t sscanf_json(const char *fmt, const json11::Json & str); -void fromhexstr(const std::string & from, int bytes, uint8_t *to); int disable_cache(const std::string & dev); uint64_t get_atomic_write_size(const std::string & dev); uint64_t get_device_size(const std::string & dev, bool should_exist = false); diff --git a/src/disk_tool/disk_tool_journal.cpp b/src/disk_tool/disk_tool_journal.cpp index 1d10ef37..3e6199b4 100644 --- a/src/disk_tool/disk_tool_journal.cpp +++ b/src/disk_tool/disk_tool_journal.cpp @@ -4,6 +4,7 @@ #include #include "disk_tool.h" +#include "str_util.h" #include "malloc_or_die.h" int disk_tool_t::dump_journal() diff --git a/src/disk_tool/disk_tool_meta.cpp b/src/disk_tool/disk_tool_meta.cpp index 68b59664..c05af985 100644 --- a/src/disk_tool/disk_tool_meta.cpp +++ b/src/disk_tool/disk_tool_meta.cpp @@ -5,6 +5,7 @@ #include "rw_blocking.h" #include "osd_id.h" #include "json_util.h" +#include "str_util.h" #include "malloc_or_die.h" int disk_tool_t::process_meta(std::function hdr_fn, diff --git a/src/disk_tool/disk_tool_utils.cpp b/src/disk_tool/disk_tool_utils.cpp index 142c3048..a6c5e431 100644 --- a/src/disk_tool/disk_tool_utils.cpp +++ b/src/disk_tool/disk_tool_utils.cpp @@ -22,28 +22,6 @@ uint64_t sscanf_json(const char *fmt, const json11::Json & str) return value; } -static int fromhex(char c) -{ - if (c >= '0' && c <= '9') - return (c-'0'); - else if (c >= 'a' && c <= 'f') - return (c-'a'+10); - else if (c >= 'A' && c <= 'F') - return (c-'A'+10); - return -1; -} - -void fromhexstr(const std::string & from, int bytes, uint8_t *to) -{ - for (int i = 0; i < from.size() && i < bytes; i++) - { - int x = fromhex(from[2*i]), y = fromhex(from[2*i+1]); - if (x < 0 || y < 0) - break; - to[i] = x*16 + y; - } -} - // returns 1 = check error, 0 = write through, -1 = write back // (similar to 1 = warning, -1 = error, 0 = success in disable_cache) static int check_queue_cache(std::string dev, std::string parent_dev) diff --git a/src/util/str_util.cpp b/src/util/str_util.cpp index a475f907..dee51474 100644 --- a/src/util/str_util.cpp +++ b/src/util/str_util.cpp @@ -534,3 +534,24 @@ std::string urldecode(const std::string & orig) } return res; } + +void fromhexstr(const std::string & from, size_t bytes, uint8_t *to) +{ + for (size_t i = 0; i < from.size() && i < bytes; i++) + { + uint8_t x = hextochar(from[2*i]), y = hextochar(from[2*i+1]); + if (x < 0 || y < 0) + break; + to[i] = x*16 + y; + } +} + +std::vector hexdecode(const std::string & str) +{ + std::vector res; + if (str.empty()) + return res; + res.resize(str.size()/2); + fromhexstr(str, str.size()/2, res.data()); + return res; +} diff --git a/src/util/str_util.h b/src/util/str_util.h index 463cc628..dd216098 100644 --- a/src/util/str_util.h +++ b/src/util/str_util.h @@ -35,5 +35,7 @@ std::string realpath_str(std::string path, bool nofail = true); std::string format_datetime(uint64_t unixtime); bool is_zero(void *buf, size_t size); std::string urldecode(const std::string & orig); +void fromhexstr(const std::string & from, size_t bytes, uint8_t *to); +std::vector hexdecode(const std::string & str); #pragma GCC visibility pop