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