2010-03-01 20:58:31 +00:00
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <errno.h>
|
2010-03-25 03:37:04 +00:00
|
|
|
#include <stdio.h>
|
2010-03-01 20:58:31 +00:00
|
|
|
|
|
|
|
#include <osmocore/utils.h>
|
|
|
|
|
2010-03-25 03:37:04 +00:00
|
|
|
static char namebuf[255];
|
2010-03-01 20:58:31 +00:00
|
|
|
const char *get_value_string(const struct value_string *vs, uint32_t val)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0;; i++) {
|
|
|
|
if (vs[i].value == 0 && vs[i].str == NULL)
|
|
|
|
break;
|
|
|
|
if (vs[i].value == val)
|
|
|
|
return vs[i].str;
|
|
|
|
}
|
2010-03-25 03:37:04 +00:00
|
|
|
|
|
|
|
snprintf(namebuf, sizeof(namebuf), "unknown 0x%x", val);
|
|
|
|
return namebuf;
|
2010-03-01 20:58:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int get_string_value(const struct value_string *vs, const char *str)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0;; i++) {
|
|
|
|
if (vs[i].value == 0 && vs[i].str == NULL)
|
|
|
|
break;
|
|
|
|
if (!strcasecmp(vs[i].str, str))
|
|
|
|
return vs[i].value;
|
|
|
|
}
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
2010-03-04 09:50:32 +00:00
|
|
|
|
|
|
|
char bcd2char(uint8_t bcd)
|
|
|
|
{
|
|
|
|
if (bcd < 0xa)
|
|
|
|
return '0' + bcd;
|
|
|
|
else
|
|
|
|
return 'A' + (bcd - 0xa);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* only works for numbers in ascci */
|
|
|
|
uint8_t char2bcd(char c)
|
|
|
|
{
|
|
|
|
return c - 0x30;
|
|
|
|
}
|
2010-07-30 08:37:29 +00:00
|
|
|
|
|
|
|
int hexparse(const char *str, uint8_t *b, int max_len)
|
|
|
|
|
|
|
|
{
|
|
|
|
int i, l, v;
|
|
|
|
|
|
|
|
l = strlen(str);
|
|
|
|
if ((l&1) || ((l>>1) > max_len))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
memset(b, 0x00, max_len);
|
|
|
|
|
|
|
|
for (i=0; i<l; i++) {
|
|
|
|
char c = str[i];
|
|
|
|
if (c >= '0' && c <= '9')
|
|
|
|
v = c - '0';
|
|
|
|
else if (c >= 'a' && c <= 'f')
|
|
|
|
v = 10 + (c - 'a');
|
|
|
|
else if (c >= 'A' && c <= 'F')
|
|
|
|
v = 10 + (c - 'A');
|
|
|
|
else
|
|
|
|
return -1;
|
|
|
|
b[i>>1] |= v << (i&1 ? 0 : 4);
|
|
|
|
}
|
|
|
|
|
|
|
|
return i>>1;
|
|
|
|
}
|
2010-07-30 09:40:32 +00:00
|
|
|
|
|
|
|
static char hexd_buff[4096];
|
|
|
|
|
2010-07-30 09:43:30 +00:00
|
|
|
static char *_hexdump(const unsigned char *buf, int len, char *delim)
|
2010-07-30 09:40:32 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
char *cur = hexd_buff;
|
|
|
|
|
|
|
|
hexd_buff[0] = 0;
|
|
|
|
for (i = 0; i < len; i++) {
|
|
|
|
int len_remain = sizeof(hexd_buff) - (cur - hexd_buff);
|
2010-07-30 09:43:30 +00:00
|
|
|
int rc = snprintf(cur, len_remain, "%02x%s", buf[i], delim);
|
2010-07-30 09:40:32 +00:00
|
|
|
if (rc <= 0)
|
|
|
|
break;
|
|
|
|
cur += rc;
|
|
|
|
}
|
|
|
|
hexd_buff[sizeof(hexd_buff)-1] = 0;
|
|
|
|
return hexd_buff;
|
|
|
|
}
|
2010-07-30 09:43:30 +00:00
|
|
|
|
|
|
|
char *hexdump(const unsigned char *buf, int len)
|
|
|
|
{
|
|
|
|
return _hexdump(buf, len, " ");
|
|
|
|
}
|
|
|
|
|
|
|
|
char *hexdump_nospc(const unsigned char *buf, int len)
|
|
|
|
{
|
|
|
|
return _hexdump(buf, len, "");
|
|
|
|
}
|