utils: make hex2mem available to all users

hex2mem() api is useful for parsing hexstrings which are then packed in
a stream of chars.

Signed-off-by: Jamal Hadi Salim <jhs@mojatatu.com>
This commit is contained in:
Jamal Hadi Salim 2017-01-14 17:04:43 -05:00 committed by Stephen Hemminger
parent 530903dd90
commit 1c570c50a3
3 changed files with 26 additions and 25 deletions

View File

@ -118,6 +118,7 @@ int get_be32(__be32 *val, const char *arg, int base);
int get_be16(__be16 *val, const char *arg, int base); int get_be16(__be16 *val, const char *arg, int base);
int get_addr64(__u64 *ap, const char *cp); int get_addr64(__u64 *ap, const char *cp);
int hex2mem(const char *buf, uint8_t *mem, int count);
char *hexstring_n2a(const __u8 *str, int len, char *buf, int blen); char *hexstring_n2a(const __u8 *str, int len, char *buf, int blen);
__u8 *hexstring_a2n(const char *str, __u8 *buf, int blen, unsigned int *len); __u8 *hexstring_a2n(const char *str, __u8 *buf, int blen, unsigned int *len);
#define ADDR64_BUF_SIZE sizeof("xxxx:xxxx:xxxx:xxxx") #define ADDR64_BUF_SIZE sizeof("xxxx:xxxx:xxxx:xxxx")

View File

@ -485,31 +485,6 @@ static int get_tunnel(struct l2tp_data *p)
* Command parser * Command parser
*****************************************************************************/ *****************************************************************************/
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 = get_hex(buf[j]);
if (c < 0)
goto err;
mem[i] = c << 4;
c = get_hex(buf[j + 1]);
if (c < 0)
goto err;
mem[i] |= c;
}
return 0;
err:
return -1;
}
static void usage(void) __attribute__((noreturn)); static void usage(void) __attribute__((noreturn));
static void usage(void) static void usage(void)

View File

@ -962,6 +962,31 @@ __u8 *hexstring_a2n(const char *str, __u8 *buf, int blen, unsigned int *len)
return buf; return buf;
} }
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 = get_hex(buf[j]);
if (c < 0)
goto err;
mem[i] = c << 4;
c = get_hex(buf[j + 1]);
if (c < 0)
goto err;
mem[i] |= c;
}
return 0;
err:
return -1;
}
int addr64_n2a(__u64 addr, char *buff, size_t len) int addr64_n2a(__u64 addr, char *buff, size_t len)
{ {
__u16 *words = (__u16 *)&addr; __u16 *words = (__u16 *)&addr;