]> git.proxmox.com Git - mirror_iproute2.git/commitdiff
utils: provide get_hex to read a hex digit from a char
authorSabrina Dubroca <sd@queasysnail.net>
Fri, 3 Jun 2016 14:45:47 +0000 (16:45 +0200)
committerStephen Hemminger <shemming@brocade.com>
Wed, 8 Jun 2016 16:30:41 +0000 (09:30 -0700)
Signed-off-by: Sabrina Dubroca <sd@queasysnail.net>
Acked-by: Phil Sutter <phil@nwl.cc>
include/utils.h
ip/ipl2tp.c
lib/ipx_pton.c
lib/utils.c

index a9aa8916295008f1bace2f645ab96872a78ac6bc..27562a1c949cad0802d6488632c7981b760a5472 100644 (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);
index 3c8ee9355439f4552e6aa40818663d1e225eb920..1f84c6149f3912f5b68a22f067f5adab921a34d6 100644 (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;
 
index 3dca2713719ad2eaeb19698339693d0b4afaaa8b..071a775e7437b84c9988742d71abec3571b92ec5 100644 (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 == ':')
index e0cee5e9dc56d1cdddd997fd49da05ed5b872c23..70e85b75b8daa38745c68b7087dada17a8bd8a4a 100644 (file)
 
 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;