Harald Welte | d284cd9 | 2010-03-01 21:58:31 +0100 | [diff] [blame] | 1 | |
| 2 | #include <string.h> |
| 3 | #include <stdint.h> |
| 4 | #include <errno.h> |
Harald Welte | b59f935 | 2010-03-25 11:37:04 +0800 | [diff] [blame] | 5 | #include <stdio.h> |
Harald Welte | d284cd9 | 2010-03-01 21:58:31 +0100 | [diff] [blame] | 6 | |
Pablo Neira Ayuso | 8341934 | 2011-03-22 16:36:13 +0100 | [diff] [blame] | 7 | #include <osmocom/core/utils.h> |
Harald Welte | d284cd9 | 2010-03-01 21:58:31 +0100 | [diff] [blame] | 8 | |
Harald Welte | 8598f18 | 2011-08-17 14:19:27 +0200 | [diff] [blame] | 9 | /*! \addtogroup utils |
| 10 | * @{ |
| 11 | */ |
| 12 | |
| 13 | /*! \file utils.c */ |
| 14 | |
Harald Welte | b59f935 | 2010-03-25 11:37:04 +0800 | [diff] [blame] | 15 | static char namebuf[255]; |
Harald Welte | 8598f18 | 2011-08-17 14:19:27 +0200 | [diff] [blame] | 16 | |
| 17 | /*! \brief get human-readable string for given value |
| 18 | * \param[in] vs Array of value_string tuples |
| 19 | * \param[in] val Value to be converted |
| 20 | * \returns pointer to human-readable string |
| 21 | */ |
Harald Welte | d284cd9 | 2010-03-01 21:58:31 +0100 | [diff] [blame] | 22 | const char *get_value_string(const struct value_string *vs, uint32_t val) |
| 23 | { |
| 24 | int i; |
| 25 | |
| 26 | for (i = 0;; i++) { |
| 27 | if (vs[i].value == 0 && vs[i].str == NULL) |
| 28 | break; |
| 29 | if (vs[i].value == val) |
| 30 | return vs[i].str; |
| 31 | } |
Harald Welte | b59f935 | 2010-03-25 11:37:04 +0800 | [diff] [blame] | 32 | |
| 33 | snprintf(namebuf, sizeof(namebuf), "unknown 0x%x", val); |
| 34 | return namebuf; |
Harald Welte | d284cd9 | 2010-03-01 21:58:31 +0100 | [diff] [blame] | 35 | } |
| 36 | |
Harald Welte | 8598f18 | 2011-08-17 14:19:27 +0200 | [diff] [blame] | 37 | /*! \brief get numeric value for given human-readable string |
| 38 | * \param[in] vs Array of value_string tuples |
| 39 | * \param[in] str human-readable string |
| 40 | * \returns numeric value (>0) or negative numer in case of error |
| 41 | */ |
Harald Welte | d284cd9 | 2010-03-01 21:58:31 +0100 | [diff] [blame] | 42 | int get_string_value(const struct value_string *vs, const char *str) |
| 43 | { |
| 44 | int i; |
| 45 | |
| 46 | for (i = 0;; i++) { |
| 47 | if (vs[i].value == 0 && vs[i].str == NULL) |
| 48 | break; |
| 49 | if (!strcasecmp(vs[i].str, str)) |
| 50 | return vs[i].value; |
| 51 | } |
| 52 | return -EINVAL; |
| 53 | } |
Harald Welte | a73e2f9 | 2010-03-04 10:50:32 +0100 | [diff] [blame] | 54 | |
Harald Welte | 8598f18 | 2011-08-17 14:19:27 +0200 | [diff] [blame] | 55 | /*! \brief Convert BCD-encoded digit into printable character |
| 56 | * \param[in] bcd A single BCD-encoded digit |
| 57 | * \returns single printable character |
| 58 | */ |
Pablo Neira Ayuso | 87f7b25 | 2011-05-07 12:43:08 +0200 | [diff] [blame] | 59 | char osmo_bcd2char(uint8_t bcd) |
Harald Welte | a73e2f9 | 2010-03-04 10:50:32 +0100 | [diff] [blame] | 60 | { |
| 61 | if (bcd < 0xa) |
| 62 | return '0' + bcd; |
| 63 | else |
| 64 | return 'A' + (bcd - 0xa); |
| 65 | } |
| 66 | |
Holger Hans Peter Freyther | 121f358 | 2010-11-19 19:20:46 +0100 | [diff] [blame] | 67 | /* only works for numbers in ascii */ |
Pablo Neira Ayuso | 87f7b25 | 2011-05-07 12:43:08 +0200 | [diff] [blame] | 68 | uint8_t osmo_char2bcd(char c) |
Harald Welte | a73e2f9 | 2010-03-04 10:50:32 +0100 | [diff] [blame] | 69 | { |
| 70 | return c - 0x30; |
| 71 | } |
Harald Welte | 3eba991 | 2010-07-30 10:37:29 +0200 | [diff] [blame] | 72 | |
Pablo Neira Ayuso | 87f7b25 | 2011-05-07 12:43:08 +0200 | [diff] [blame] | 73 | int osmo_hexparse(const char *str, uint8_t *b, int max_len) |
Harald Welte | 3eba991 | 2010-07-30 10:37:29 +0200 | [diff] [blame] | 74 | |
| 75 | { |
| 76 | int i, l, v; |
| 77 | |
| 78 | l = strlen(str); |
| 79 | if ((l&1) || ((l>>1) > max_len)) |
| 80 | return -1; |
| 81 | |
| 82 | memset(b, 0x00, max_len); |
| 83 | |
| 84 | for (i=0; i<l; i++) { |
| 85 | char c = str[i]; |
| 86 | if (c >= '0' && c <= '9') |
| 87 | v = c - '0'; |
| 88 | else if (c >= 'a' && c <= 'f') |
| 89 | v = 10 + (c - 'a'); |
| 90 | else if (c >= 'A' && c <= 'F') |
| 91 | v = 10 + (c - 'A'); |
| 92 | else |
| 93 | return -1; |
| 94 | b[i>>1] |= v << (i&1 ? 0 : 4); |
| 95 | } |
| 96 | |
| 97 | return i>>1; |
| 98 | } |
Harald Welte | 40481e8 | 2010-07-30 11:40:32 +0200 | [diff] [blame] | 99 | |
| 100 | static char hexd_buff[4096]; |
| 101 | |
Pablo Neira Ayuso | 87f7b25 | 2011-05-07 12:43:08 +0200 | [diff] [blame] | 102 | static char *_osmo_hexdump(const unsigned char *buf, int len, char *delim) |
Harald Welte | 40481e8 | 2010-07-30 11:40:32 +0200 | [diff] [blame] | 103 | { |
| 104 | int i; |
| 105 | char *cur = hexd_buff; |
| 106 | |
| 107 | hexd_buff[0] = 0; |
| 108 | for (i = 0; i < len; i++) { |
| 109 | int len_remain = sizeof(hexd_buff) - (cur - hexd_buff); |
Holger Hans Peter Freyther | 128d9e2 | 2011-07-15 16:07:23 +0200 | [diff] [blame] | 110 | if (len_remain <= 0) |
| 111 | break; |
Harald Welte | dee47cd | 2010-07-30 11:43:30 +0200 | [diff] [blame] | 112 | int rc = snprintf(cur, len_remain, "%02x%s", buf[i], delim); |
Harald Welte | 40481e8 | 2010-07-30 11:40:32 +0200 | [diff] [blame] | 113 | if (rc <= 0) |
| 114 | break; |
| 115 | cur += rc; |
| 116 | } |
| 117 | hexd_buff[sizeof(hexd_buff)-1] = 0; |
| 118 | return hexd_buff; |
| 119 | } |
Harald Welte | dee47cd | 2010-07-30 11:43:30 +0200 | [diff] [blame] | 120 | |
Harald Welte | 8598f18 | 2011-08-17 14:19:27 +0200 | [diff] [blame] | 121 | /*! \brief Convert a sequence of unpacked bits to ASCII string |
| 122 | * \param[in] bits A sequence of unpacked bits |
| 123 | * \param[in] len Length of bits |
| 124 | */ |
Pablo Neira Ayuso | 87f7b25 | 2011-05-07 12:43:08 +0200 | [diff] [blame] | 125 | char *osmo_ubit_dump(const uint8_t *bits, unsigned int len) |
Harald Welte | 3d0ac5e | 2011-02-08 16:55:03 +0100 | [diff] [blame] | 126 | { |
| 127 | int i; |
| 128 | |
| 129 | if (len > sizeof(hexd_buff)-1) |
| 130 | len = sizeof(hexd_buff)-1; |
| 131 | memset(hexd_buff, 0, sizeof(hexd_buff)); |
| 132 | |
| 133 | for (i = 0; i < len; i++) { |
| 134 | char outch; |
| 135 | switch (bits[i]) { |
| 136 | case 0: |
| 137 | outch = '0'; |
| 138 | break; |
| 139 | case 0xff: |
| 140 | outch = '?'; |
| 141 | break; |
| 142 | case 1: |
| 143 | outch = '1'; |
| 144 | break; |
| 145 | default: |
| 146 | outch = 'E'; |
| 147 | break; |
| 148 | } |
| 149 | hexd_buff[i] = outch; |
| 150 | } |
| 151 | hexd_buff[sizeof(hexd_buff)-1] = 0; |
| 152 | return hexd_buff; |
| 153 | } |
| 154 | |
Harald Welte | 8598f18 | 2011-08-17 14:19:27 +0200 | [diff] [blame] | 155 | /*! \brief Convert binary sequence to hexadecimal ASCII string |
| 156 | * \param[in] buf pointer to sequence of bytes |
| 157 | * \param[in] len length of buf in number of bytes |
| 158 | * \returns pointer to zero-terminated string |
| 159 | * |
| 160 | * This function will print a sequence of bytes as hexadecimal numbers, |
| 161 | * adding one space character between each byte (e.g. "1a ef d9") |
| 162 | */ |
Pablo Neira Ayuso | 87f7b25 | 2011-05-07 12:43:08 +0200 | [diff] [blame] | 163 | char *osmo_hexdump(const unsigned char *buf, int len) |
Harald Welte | dee47cd | 2010-07-30 11:43:30 +0200 | [diff] [blame] | 164 | { |
Pablo Neira Ayuso | 87f7b25 | 2011-05-07 12:43:08 +0200 | [diff] [blame] | 165 | return _osmo_hexdump(buf, len, " "); |
Harald Welte | dee47cd | 2010-07-30 11:43:30 +0200 | [diff] [blame] | 166 | } |
| 167 | |
Harald Welte | 8598f18 | 2011-08-17 14:19:27 +0200 | [diff] [blame] | 168 | /*! \brief Convert binary sequence to hexadecimal ASCII string |
| 169 | * \param[in] buf pointer to sequence of bytes |
| 170 | * \param[in] len length of buf in number of bytes |
| 171 | * \returns pointer to zero-terminated string |
| 172 | * |
| 173 | * This function will print a sequence of bytes as hexadecimal numbers, |
| 174 | * without any space character between each byte (e.g. "1aefd9") |
| 175 | */ |
Sylvain Munaut | ff23d24 | 2011-11-10 23:03:18 +0100 | [diff] [blame] | 176 | char *osmo_hexdump_nospc(const unsigned char *buf, int len) |
Harald Welte | dee47cd | 2010-07-30 11:43:30 +0200 | [diff] [blame] | 177 | { |
Pablo Neira Ayuso | 87f7b25 | 2011-05-07 12:43:08 +0200 | [diff] [blame] | 178 | return _osmo_hexdump(buf, len, ""); |
Harald Welte | dee47cd | 2010-07-30 11:43:30 +0200 | [diff] [blame] | 179 | } |
Harald Welte | 2822296 | 2011-02-18 20:37:04 +0100 | [diff] [blame] | 180 | |
Sylvain Munaut | e55ae3a | 2011-11-11 23:06:55 +0100 | [diff] [blame] | 181 | /* Compat with previous typo to preserve abi */ |
| 182 | char *osmo_osmo_hexdump_nospc(const unsigned char *buf, int len) |
| 183 | __attribute__((alias("osmo_hexdump_nospc"))); |
| 184 | |
Harald Welte | 2822296 | 2011-02-18 20:37:04 +0100 | [diff] [blame] | 185 | #include "../config.h" |
| 186 | #ifdef HAVE_CTYPE_H |
| 187 | #include <ctype.h> |
Harald Welte | 8598f18 | 2011-08-17 14:19:27 +0200 | [diff] [blame] | 188 | /*! \brief Convert an entire string to lower case |
| 189 | * \param[out] out output string, caller-allocated |
| 190 | * \param[in] in input string |
| 191 | */ |
Harald Welte | 2822296 | 2011-02-18 20:37:04 +0100 | [diff] [blame] | 192 | void osmo_str2lower(char *out, const char *in) |
| 193 | { |
| 194 | unsigned int i; |
| 195 | |
| 196 | for (i = 0; i < strlen(in); i++) |
| 197 | out[i] = tolower(in[i]); |
| 198 | out[strlen(in)] = '\0'; |
| 199 | } |
| 200 | |
Harald Welte | 8598f18 | 2011-08-17 14:19:27 +0200 | [diff] [blame] | 201 | /*! \brief Convert an entire string to upper case |
| 202 | * \param[out] out output string, caller-allocated |
| 203 | * \param[in] in input string |
| 204 | */ |
Harald Welte | 2822296 | 2011-02-18 20:37:04 +0100 | [diff] [blame] | 205 | void osmo_str2upper(char *out, const char *in) |
| 206 | { |
| 207 | unsigned int i; |
| 208 | |
| 209 | for (i = 0; i < strlen(in); i++) |
| 210 | out[i] = toupper(in[i]); |
| 211 | out[strlen(in)] = '\0'; |
| 212 | } |
| 213 | #endif /* HAVE_CTYPE_H */ |
Harald Welte | 8598f18 | 2011-08-17 14:19:27 +0200 | [diff] [blame] | 214 | |
| 215 | /*! }@ */ |