utils: provide get_hex to read a hex digit from a char

Signed-off-by: Sabrina Dubroca <sd@queasysnail.net>
Acked-by: Phil Sutter <phil@nwl.cc>
This commit is contained in:
Sabrina Dubroca 2016-06-03 16:45:47 +02:00 committed by Stephen Hemminger
parent 9f7401fa49
commit 609640f5f0
4 changed files with 18 additions and 28 deletions

View File

@ -99,6 +99,7 @@ int get_prefix(inet_prefix *dst, char *arg, int family);
int mask2bits(__u32 netmask);
int get_addr_ila(__u64 *val, const char *arg);
int get_hex(char c);
int get_integer(int *val, const char *arg, int base);
int get_unsigned(unsigned *val, const char *arg, int base);
int get_time_rtt(unsigned *val, const char *arg, int *raw);

View File

@ -425,30 +425,19 @@ static int get_tunnel(struct l2tp_data *p)
* Command parser
*****************************************************************************/
static int hex(char ch)
{
if ((ch >= 'a') && (ch <= 'f'))
return ch - 'a' + 10;
if ((ch >= '0') && (ch <= '9'))
return ch - '0';
if ((ch >= 'A') && (ch <= 'F'))
return ch - 'A' + 10;
return -1;
}
static int hex2mem(const char *buf, uint8_t *mem, int count)
{
int i, j;
int c;
for (i = 0, j = 0; i < count; i++, j += 2) {
c = hex(buf[j]);
c = get_hex(buf[j]);
if (c < 0)
goto err;
mem[i] = c << 4;
c = hex(buf[j + 1]);
c = get_hex(buf[j + 1]);
if (c < 0)
goto err;

View File

@ -6,18 +6,6 @@
#include "utils.h"
static u_int32_t hexget(char c)
{
if (c >= 'A' && c <= 'F')
return c - 'A' + 10;
if (c >= 'a' && c <= 'f')
return c - 'a' + 10;
if (c >= '0' && c <= '9')
return c - '0';
return 0xf0;
}
static int ipx_getnet(u_int32_t *net, const char *str)
{
int i;
@ -25,7 +13,7 @@ static int ipx_getnet(u_int32_t *net, const char *str)
for(i = 0; *str && (i < 8); i++) {
if ((tmp = hexget(*str)) & 0xf0) {
if ((tmp = get_hex(*str)) == -1) {
if (*str == '.')
return 0;
else
@ -49,11 +37,11 @@ static int ipx_getnode(u_int8_t *node, const char *str)
u_int32_t tmp;
for(i = 0; i < 6; i++) {
if ((tmp = hexget(*str++)) & 0xf0)
if ((tmp = get_hex(*str++)) == -1)
return -1;
node[i] = (u_int8_t)tmp;
node[i] <<= 4;
if ((tmp = hexget(*str++)) & 0xf0)
if ((tmp = get_hex(*str++)) == -1)
return -1;
node[i] |= (u_int8_t)tmp;
if (*str == ':')

View File

@ -37,6 +37,18 @@
int timestamp_short = 0;
int get_hex(char c)
{
if (c >= 'A' && c <= 'F')
return c - 'A' + 10;
if (c >= 'a' && c <= 'f')
return c - 'a' + 10;
if (c >= '0' && c <= '9')
return c - '0';
return -1;
}
int get_integer(int *val, const char *arg, int base)
{
long res;