Move fromhexstr() to str_util
This commit is contained in:
@@ -172,7 +172,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);
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
#include <unistd.h>
|
||||
|
||||
#include "disk_tool.h"
|
||||
#include "str_util.h"
|
||||
#include "malloc_or_die.h"
|
||||
|
||||
int disk_tool_t::dump_journal()
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
#include "disk_tool.h"
|
||||
#include "rw_blocking.h"
|
||||
#include "json_util.h"
|
||||
#include "str_util.h"
|
||||
#include "malloc_or_die.h"
|
||||
|
||||
int disk_tool_t::process_meta(std::function<void(blockstore_meta_header_v3_t *)> hdr_fn,
|
||||
|
||||
@@ -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)
|
||||
|
||||
+20
-3
@@ -504,7 +504,7 @@ bool is_zero(void *buf, size_t size)
|
||||
return true;
|
||||
}
|
||||
|
||||
char hextochar(char h)
|
||||
static uint8_t fromhexchar(char h, uint8_t def)
|
||||
{
|
||||
if (h >= 'a' && h <= 'f')
|
||||
return h-'a'+10;
|
||||
@@ -512,7 +512,7 @@ char hextochar(char h)
|
||||
return h-'A'+10;
|
||||
else if (h >= '0' && h <= '9')
|
||||
return h-'0';
|
||||
return 0;
|
||||
return def;
|
||||
}
|
||||
|
||||
std::string urldecode(const std::string & orig)
|
||||
@@ -526,7 +526,7 @@ std::string urldecode(const std::string & orig)
|
||||
res.push_back(' ');
|
||||
else if (orig[i] == '%' && i < len-2)
|
||||
{
|
||||
res.push_back(hextochar(orig[i+1])*16 + hextochar(orig[i+2]));
|
||||
res.push_back(fromhexchar(orig[i+1], 0)*16 + fromhexchar(orig[i+2], 0));
|
||||
i += 2;
|
||||
}
|
||||
else
|
||||
@@ -534,3 +534,20 @@ std::string urldecode(const std::string & orig)
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
size_t fromhexstr(const std::string & from, size_t bytes, uint8_t *to)
|
||||
{
|
||||
if (bytes > from.size()/2)
|
||||
bytes = from.size()/2;
|
||||
size_t i = 0;
|
||||
while (i < bytes)
|
||||
{
|
||||
uint8_t x = fromhexchar(from[2*i], 16);
|
||||
uint8_t y = fromhexchar(from[2*i+1], 16);
|
||||
if (x == 16 || y == 16)
|
||||
break;
|
||||
to[i] = x*16 + y;
|
||||
i++;
|
||||
}
|
||||
return i;
|
||||
}
|
||||
|
||||
@@ -35,5 +35,6 @@ 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);
|
||||
size_t fromhexstr(const std::string & from, size_t bytes, uint8_t *to);
|
||||
|
||||
#pragma GCC visibility pop
|
||||
|
||||
Reference in New Issue
Block a user