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]; |
| 110 | |
Pablo Neira Ayuso | 87f7b25 | 2011-05-07 12:43:08 +0200 | [diff] [blame] | 111 | static char *_osmo_hexdump(const unsigned char *buf, int len, char *delim) |
Harald Welte | 40481e8 | 2010-07-30 11:40:32 +0200 | [diff] [blame] | 112 | { |
| 113 | int i; |
| 114 | char *cur = hexd_buff; |
| 115 | |
| 116 | hexd_buff[0] = 0; |
| 117 | for (i = 0; i < len; i++) { |
| 118 | int len_remain = sizeof(hexd_buff) - (cur - hexd_buff); |
Holger Hans Peter Freyther | 128d9e2 | 2011-07-15 16:07:23 +0200 | [diff] [blame] | 119 | if (len_remain <= 0) |
| 120 | break; |
Harald Welte | dee47cd | 2010-07-30 11:43:30 +0200 | [diff] [blame] | 121 | int rc = snprintf(cur, len_remain, "%02x%s", buf[i], delim); |
Harald Welte | 40481e8 | 2010-07-30 11:40:32 +0200 | [diff] [blame] | 122 | if (rc <= 0) |
| 123 | break; |
| 124 | cur += rc; |
| 125 | } |
| 126 | hexd_buff[sizeof(hexd_buff)-1] = 0; |
| 127 | return hexd_buff; |
| 128 | } |
Harald Welte | dee47cd | 2010-07-30 11:43:30 +0200 | [diff] [blame] | 129 | |
Harald Welte | 8598f18 | 2011-08-17 14:19:27 +0200 | [diff] [blame] | 130 | /*! \brief Convert a sequence of unpacked bits to ASCII string |
| 131 | * \param[in] bits A sequence of unpacked bits |
| 132 | * \param[in] len Length of bits |
| 133 | */ |
Pablo Neira Ayuso | 87f7b25 | 2011-05-07 12:43:08 +0200 | [diff] [blame] | 134 | char *osmo_ubit_dump(const uint8_t *bits, unsigned int len) |
Harald Welte | 3d0ac5e | 2011-02-08 16:55:03 +0100 | [diff] [blame] | 135 | { |
| 136 | int i; |
| 137 | |
| 138 | if (len > sizeof(hexd_buff)-1) |
| 139 | len = sizeof(hexd_buff)-1; |
| 140 | memset(hexd_buff, 0, sizeof(hexd_buff)); |
| 141 | |
| 142 | for (i = 0; i < len; i++) { |
| 143 | char outch; |
| 144 | switch (bits[i]) { |
| 145 | case 0: |
| 146 | outch = '0'; |
| 147 | break; |
| 148 | case 0xff: |
| 149 | outch = '?'; |
| 150 | break; |
| 151 | case 1: |
| 152 | outch = '1'; |
| 153 | break; |
| 154 | default: |
| 155 | outch = 'E'; |
| 156 | break; |
| 157 | } |
| 158 | hexd_buff[i] = outch; |
| 159 | } |
| 160 | hexd_buff[sizeof(hexd_buff)-1] = 0; |
| 161 | return hexd_buff; |
| 162 | } |
| 163 | |
Harald Welte | 8598f18 | 2011-08-17 14:19:27 +0200 | [diff] [blame] | 164 | /*! \brief Convert binary sequence to hexadecimal ASCII string |
| 165 | * \param[in] buf pointer to sequence of bytes |
| 166 | * \param[in] len length of buf in number of bytes |
| 167 | * \returns pointer to zero-terminated string |
| 168 | * |
| 169 | * This function will print a sequence of bytes as hexadecimal numbers, |
| 170 | * adding one space character between each byte (e.g. "1a ef d9") |
| 171 | */ |
Pablo Neira Ayuso | 87f7b25 | 2011-05-07 12:43:08 +0200 | [diff] [blame] | 172 | char *osmo_hexdump(const unsigned char *buf, int len) |
Harald Welte | dee47cd | 2010-07-30 11:43:30 +0200 | [diff] [blame] | 173 | { |
Pablo Neira Ayuso | 87f7b25 | 2011-05-07 12:43:08 +0200 | [diff] [blame] | 174 | return _osmo_hexdump(buf, len, " "); |
Harald Welte | dee47cd | 2010-07-30 11:43:30 +0200 | [diff] [blame] | 175 | } |
| 176 | |
Harald Welte | 8598f18 | 2011-08-17 14:19:27 +0200 | [diff] [blame] | 177 | /*! \brief Convert binary sequence to hexadecimal ASCII string |
| 178 | * \param[in] buf pointer to sequence of bytes |
| 179 | * \param[in] len length of buf in number of bytes |
| 180 | * \returns pointer to zero-terminated string |
| 181 | * |
| 182 | * This function will print a sequence of bytes as hexadecimal numbers, |
| 183 | * without any space character between each byte (e.g. "1aefd9") |
| 184 | */ |
Sylvain Munaut | ff23d24 | 2011-11-10 23:03:18 +0100 | [diff] [blame] | 185 | char *osmo_hexdump_nospc(const unsigned char *buf, int len) |
Harald Welte | dee47cd | 2010-07-30 11:43:30 +0200 | [diff] [blame] | 186 | { |
Pablo Neira Ayuso | 87f7b25 | 2011-05-07 12:43:08 +0200 | [diff] [blame] | 187 | return _osmo_hexdump(buf, len, ""); |
Harald Welte | dee47cd | 2010-07-30 11:43:30 +0200 | [diff] [blame] | 188 | } |
Harald Welte | 2822296 | 2011-02-18 20:37:04 +0100 | [diff] [blame] | 189 | |
Sylvain Munaut | e55ae3a | 2011-11-11 23:06:55 +0100 | [diff] [blame] | 190 | /* Compat with previous typo to preserve abi */ |
| 191 | char *osmo_osmo_hexdump_nospc(const unsigned char *buf, int len) |
Sylvain Munaut | 17af41d | 2011-11-19 22:30:39 +0100 | [diff] [blame] | 192 | __attribute__((weak, alias("osmo_hexdump_nospc"))); |
Sylvain Munaut | e55ae3a | 2011-11-11 23:06:55 +0100 | [diff] [blame] | 193 | |
Harald Welte | 2822296 | 2011-02-18 20:37:04 +0100 | [diff] [blame] | 194 | #include "../config.h" |
| 195 | #ifdef HAVE_CTYPE_H |
| 196 | #include <ctype.h> |
Harald Welte | 8598f18 | 2011-08-17 14:19:27 +0200 | [diff] [blame] | 197 | /*! \brief Convert an entire string to lower case |
| 198 | * \param[out] out output string, caller-allocated |
| 199 | * \param[in] in input string |
| 200 | */ |
Harald Welte | 2822296 | 2011-02-18 20:37:04 +0100 | [diff] [blame] | 201 | void osmo_str2lower(char *out, const char *in) |
| 202 | { |
| 203 | unsigned int i; |
| 204 | |
| 205 | for (i = 0; i < strlen(in); i++) |
| 206 | out[i] = tolower(in[i]); |
| 207 | out[strlen(in)] = '\0'; |
| 208 | } |
| 209 | |
Harald Welte | 8598f18 | 2011-08-17 14:19:27 +0200 | [diff] [blame] | 210 | /*! \brief Convert an entire string to upper case |
| 211 | * \param[out] out output string, caller-allocated |
| 212 | * \param[in] in input string |
| 213 | */ |
Harald Welte | 2822296 | 2011-02-18 20:37:04 +0100 | [diff] [blame] | 214 | void osmo_str2upper(char *out, const char *in) |
| 215 | { |
| 216 | unsigned int i; |
| 217 | |
| 218 | for (i = 0; i < strlen(in); i++) |
| 219 | out[i] = toupper(in[i]); |
| 220 | out[strlen(in)] = '\0'; |
| 221 | } |
| 222 | #endif /* HAVE_CTYPE_H */ |
Harald Welte | 8598f18 | 2011-08-17 14:19:27 +0200 | [diff] [blame] | 223 | |
Sylvain Munaut | dca7d2c | 2012-04-18 21:53:23 +0200 | [diff] [blame] | 224 | /*! @} */ |