X-Git-Url: http://git.rot13.org/?a=blobdiff_plain;f=src%2Futils.c;h=e1d4c893edcedfff76fd87c1c26d1d018600dba1;hb=291e61346bb55d0462c07bf078cab3bb7eec2241;hp=05381c8bdcc8ac2a0fcaafcd4af8c3261cc84a76;hpb=3eba991b3d005c3ae4aafe2235558a60e06b49f4;p=osmocom-bb.git diff --git a/src/utils.c b/src/utils.c index 05381c8..e1d4c89 100644 --- a/src/utils.c +++ b/src/utils.c @@ -4,7 +4,7 @@ #include #include -#include +#include static char namebuf[255]; const char *get_value_string(const struct value_string *vs, uint32_t val) @@ -35,7 +35,7 @@ int get_string_value(const struct value_string *vs, const char *str) return -EINVAL; } -char bcd2char(uint8_t bcd) +char osmo_bcd2char(uint8_t bcd) { if (bcd < 0xa) return '0' + bcd; @@ -43,13 +43,13 @@ char bcd2char(uint8_t bcd) return 'A' + (bcd - 0xa); } -/* only works for numbers in ascci */ -uint8_t char2bcd(char c) +/* only works for numbers in ascii */ +uint8_t osmo_char2bcd(char c) { return c - 0x30; } -int hexparse(const char *str, uint8_t *b, int max_len) +int osmo_hexparse(const char *str, uint8_t *b, int max_len) { int i, l, v; @@ -75,3 +75,86 @@ int hexparse(const char *str, uint8_t *b, int max_len) return i>>1; } + +static char hexd_buff[4096]; + +static char *_osmo_hexdump(const unsigned char *buf, int len, char *delim) +{ + 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); + if (len_remain <= 0) + break; + int rc = snprintf(cur, len_remain, "%02x%s", buf[i], delim); + if (rc <= 0) + break; + cur += rc; + } + hexd_buff[sizeof(hexd_buff)-1] = 0; + return hexd_buff; +} + +char *osmo_ubit_dump(const uint8_t *bits, unsigned int len) +{ + int i; + + if (len > sizeof(hexd_buff)-1) + len = sizeof(hexd_buff)-1; + memset(hexd_buff, 0, sizeof(hexd_buff)); + + for (i = 0; i < len; i++) { + char outch; + switch (bits[i]) { + case 0: + outch = '0'; + break; + case 0xff: + outch = '?'; + break; + case 1: + outch = '1'; + break; + default: + outch = 'E'; + break; + } + hexd_buff[i] = outch; + } + hexd_buff[sizeof(hexd_buff)-1] = 0; + return hexd_buff; +} + +char *osmo_hexdump(const unsigned char *buf, int len) +{ + return _osmo_hexdump(buf, len, " "); +} + +char *osmo_osmo_hexdump_nospc(const unsigned char *buf, int len) +{ + return _osmo_hexdump(buf, len, ""); +} + +#include "../config.h" +#ifdef HAVE_CTYPE_H +#include +void osmo_str2lower(char *out, const char *in) +{ + unsigned int i; + + for (i = 0; i < strlen(in); i++) + out[i] = tolower(in[i]); + out[strlen(in)] = '\0'; +} + +void osmo_str2upper(char *out, const char *in) +{ + unsigned int i; + + for (i = 0; i < strlen(in); i++) + out[i] = toupper(in[i]); + out[strlen(in)] = '\0'; +} +#endif /* HAVE_CTYPE_H */