lokinet/llarp/encode.cpp

30 lines
559 B
C++
Raw Normal View History

2018-12-12 01:02:36 +00:00
#include <encode.hpp>
2018-06-21 11:13:40 +00:00
#include <stdexcept>
2018-06-21 11:11:55 +00:00
2018-06-21 11:13:40 +00:00
namespace llarp
2018-06-21 11:11:55 +00:00
{
int
char2int(char input)
2018-06-21 11:13:40 +00:00
{
if(input >= '0' && input <= '9')
return input - '0';
if(input >= 'A' && input <= 'F')
return input - 'A' + 10;
if(input >= 'a' && input <= 'f')
return input - 'a' + 10;
2018-09-04 19:15:06 +00:00
return 0;
2018-06-21 11:13:40 +00:00
}
2018-11-14 19:34:17 +00:00
bool
HexDecode(const char* src, uint8_t* target, size_t sz)
2018-06-21 11:11:55 +00:00
{
2018-11-14 19:34:17 +00:00
while(*src && src[1] && sz)
2018-06-21 11:13:40 +00:00
{
*(target++) = char2int(*src) * 16 + char2int(src[1]);
2018-06-21 11:13:40 +00:00
src += 2;
2018-11-14 19:34:17 +00:00
--sz;
2018-06-21 11:13:40 +00:00
}
2018-11-14 19:34:17 +00:00
return sz == 0;
2018-06-21 11:11:55 +00:00
}
2018-07-09 03:34:29 +00:00
} // namespace llarp