Move fromhexstr() to str_util

This commit is contained in:
Vitaliy Filippov
2026-04-17 13:53:40 +03:00
parent 0d97fba466
commit 803c870493
6 changed files with 23 additions and 26 deletions
+20 -3
View File
@@ -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;
}