Harald Welte | 468b643 | 2014-09-11 13:05:51 +0800 | [diff] [blame] | 1 | /* |
| 2 | * (C) 2011 by Harald Welte <laforge@gnumonks.org> |
| 3 | * (C) 2011 by Sylvain Munaut <tnt@246tNt.com> |
| 4 | * (C) 2014 by Nils O. Selåsdal <noselasd@fiane.dyndns.org> |
| 5 | * |
| 6 | * All Rights Reserved |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License along |
| 19 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 21 | * |
| 22 | */ |
| 23 | |
Harald Welte | d284cd9 | 2010-03-01 21:58:31 +0100 | [diff] [blame] | 24 | |
| 25 | #include <string.h> |
| 26 | #include <stdint.h> |
| 27 | #include <errno.h> |
Harald Welte | b59f935 | 2010-03-25 11:37:04 +0800 | [diff] [blame] | 28 | #include <stdio.h> |
Harald Welte | d284cd9 | 2010-03-01 21:58:31 +0100 | [diff] [blame] | 29 | |
Pablo Neira Ayuso | 8341934 | 2011-03-22 16:36:13 +0100 | [diff] [blame] | 30 | #include <osmocom/core/utils.h> |
Harald Welte | d284cd9 | 2010-03-01 21:58:31 +0100 | [diff] [blame] | 31 | |
Harald Welte | 8598f18 | 2011-08-17 14:19:27 +0200 | [diff] [blame] | 32 | /*! \addtogroup utils |
| 33 | * @{ |
| 34 | */ |
| 35 | |
| 36 | /*! \file utils.c */ |
| 37 | |
Harald Welte | b59f935 | 2010-03-25 11:37:04 +0800 | [diff] [blame] | 38 | static char namebuf[255]; |
Harald Welte | 8598f18 | 2011-08-17 14:19:27 +0200 | [diff] [blame] | 39 | |
| 40 | /*! \brief get human-readable string for given value |
| 41 | * \param[in] vs Array of value_string tuples |
| 42 | * \param[in] val Value to be converted |
| 43 | * \returns pointer to human-readable string |
| 44 | */ |
Harald Welte | d284cd9 | 2010-03-01 21:58:31 +0100 | [diff] [blame] | 45 | const char *get_value_string(const struct value_string *vs, uint32_t val) |
| 46 | { |
| 47 | int i; |
| 48 | |
| 49 | for (i = 0;; i++) { |
| 50 | if (vs[i].value == 0 && vs[i].str == NULL) |
| 51 | break; |
| 52 | if (vs[i].value == val) |
| 53 | return vs[i].str; |
| 54 | } |
Harald Welte | b59f935 | 2010-03-25 11:37:04 +0800 | [diff] [blame] | 55 | |
| 56 | snprintf(namebuf, sizeof(namebuf), "unknown 0x%x", val); |
Holger Hans Peter Freyther | 8d50600 | 2013-07-04 20:14:10 +0200 | [diff] [blame] | 57 | namebuf[sizeof(namebuf) - 1] = '\0'; |
Harald Welte | b59f935 | 2010-03-25 11:37:04 +0800 | [diff] [blame] | 58 | return namebuf; |
Harald Welte | d284cd9 | 2010-03-01 21:58:31 +0100 | [diff] [blame] | 59 | } |
| 60 | |
Harald Welte | 8598f18 | 2011-08-17 14:19:27 +0200 | [diff] [blame] | 61 | /*! \brief get numeric value for given human-readable string |
| 62 | * \param[in] vs Array of value_string tuples |
| 63 | * \param[in] str human-readable string |
| 64 | * \returns numeric value (>0) or negative numer in case of error |
| 65 | */ |
Harald Welte | d284cd9 | 2010-03-01 21:58:31 +0100 | [diff] [blame] | 66 | int get_string_value(const struct value_string *vs, const char *str) |
| 67 | { |
| 68 | int i; |
| 69 | |
| 70 | for (i = 0;; i++) { |
| 71 | if (vs[i].value == 0 && vs[i].str == NULL) |
| 72 | break; |
| 73 | if (!strcasecmp(vs[i].str, str)) |
| 74 | return vs[i].value; |
| 75 | } |
| 76 | return -EINVAL; |
| 77 | } |
Harald Welte | a73e2f9 | 2010-03-04 10:50:32 +0100 | [diff] [blame] | 78 | |
Harald Welte | 8598f18 | 2011-08-17 14:19:27 +0200 | [diff] [blame] | 79 | /*! \brief Convert BCD-encoded digit into printable character |
| 80 | * \param[in] bcd A single BCD-encoded digit |
| 81 | * \returns single printable character |
| 82 | */ |
Pablo Neira Ayuso | 87f7b25 | 2011-05-07 12:43:08 +0200 | [diff] [blame] | 83 | char osmo_bcd2char(uint8_t bcd) |
Harald Welte | a73e2f9 | 2010-03-04 10:50:32 +0100 | [diff] [blame] | 84 | { |
| 85 | if (bcd < 0xa) |
| 86 | return '0' + bcd; |
| 87 | else |
| 88 | return 'A' + (bcd - 0xa); |
| 89 | } |
| 90 | |
Harald Welte | de6e498 | 2012-12-06 21:25:27 +0100 | [diff] [blame] | 91 | /*! \brief Convert number in ASCII to BCD value |
| 92 | * \param[in] c ASCII character |
| 93 | * \returns BCD encoded value of character |
| 94 | */ |
Pablo Neira Ayuso | 87f7b25 | 2011-05-07 12:43:08 +0200 | [diff] [blame] | 95 | uint8_t osmo_char2bcd(char c) |
Harald Welte | a73e2f9 | 2010-03-04 10:50:32 +0100 | [diff] [blame] | 96 | { |
| 97 | return c - 0x30; |
| 98 | } |
Harald Welte | 3eba991 | 2010-07-30 10:37:29 +0200 | [diff] [blame] | 99 | |
Harald Welte | de6e498 | 2012-12-06 21:25:27 +0100 | [diff] [blame] | 100 | /*! \brief Parse a string ocntaining hexadecimal digits |
| 101 | * \param[in] str string containing ASCII encoded hexadecimal digits |
| 102 | * \param[out] b output buffer |
| 103 | * \param[in] max_len maximum space in output buffer |
Neels Hofmeyr | 3de7b05 | 2015-09-23 23:16:53 +0200 | [diff] [blame] | 104 | * \returns number of parsed octets, or -1 on error |
Harald Welte | de6e498 | 2012-12-06 21:25:27 +0100 | [diff] [blame] | 105 | */ |
Pablo Neira Ayuso | 87f7b25 | 2011-05-07 12:43:08 +0200 | [diff] [blame] | 106 | int osmo_hexparse(const char *str, uint8_t *b, int max_len) |
Harald Welte | 3eba991 | 2010-07-30 10:37:29 +0200 | [diff] [blame] | 107 | |
| 108 | { |
| 109 | int i, l, v; |
| 110 | |
| 111 | l = strlen(str); |
| 112 | if ((l&1) || ((l>>1) > max_len)) |
| 113 | return -1; |
| 114 | |
| 115 | memset(b, 0x00, max_len); |
| 116 | |
| 117 | for (i=0; i<l; i++) { |
| 118 | char c = str[i]; |
| 119 | if (c >= '0' && c <= '9') |
| 120 | v = c - '0'; |
| 121 | else if (c >= 'a' && c <= 'f') |
| 122 | v = 10 + (c - 'a'); |
| 123 | else if (c >= 'A' && c <= 'F') |
| 124 | v = 10 + (c - 'A'); |
| 125 | else |
| 126 | return -1; |
| 127 | b[i>>1] |= v << (i&1 ? 0 : 4); |
| 128 | } |
| 129 | |
| 130 | return i>>1; |
| 131 | } |
Harald Welte | 40481e8 | 2010-07-30 11:40:32 +0200 | [diff] [blame] | 132 | |
| 133 | static char hexd_buff[4096]; |
Nils O. Selåsdal | 3244702 | 2014-01-02 14:04:43 +0100 | [diff] [blame] | 134 | static const char hex_chars[] = "0123456789abcdef"; |
Harald Welte | 40481e8 | 2010-07-30 11:40:32 +0200 | [diff] [blame] | 135 | |
Pablo Neira Ayuso | 87f7b25 | 2011-05-07 12:43:08 +0200 | [diff] [blame] | 136 | static char *_osmo_hexdump(const unsigned char *buf, int len, char *delim) |
Harald Welte | 40481e8 | 2010-07-30 11:40:32 +0200 | [diff] [blame] | 137 | { |
| 138 | int i; |
| 139 | char *cur = hexd_buff; |
| 140 | |
| 141 | hexd_buff[0] = 0; |
| 142 | for (i = 0; i < len; i++) { |
Nils O. Selåsdal | 3244702 | 2014-01-02 14:04:43 +0100 | [diff] [blame] | 143 | const char *delimp = delim; |
Harald Welte | 40481e8 | 2010-07-30 11:40:32 +0200 | [diff] [blame] | 144 | int len_remain = sizeof(hexd_buff) - (cur - hexd_buff); |
Nils O. Selåsdal | 3244702 | 2014-01-02 14:04:43 +0100 | [diff] [blame] | 145 | if (len_remain < 3) |
Holger Hans Peter Freyther | 128d9e2 | 2011-07-15 16:07:23 +0200 | [diff] [blame] | 146 | break; |
Nils O. Selåsdal | 3244702 | 2014-01-02 14:04:43 +0100 | [diff] [blame] | 147 | |
| 148 | *cur++ = hex_chars[buf[i] >> 4]; |
| 149 | *cur++ = hex_chars[buf[i] & 0xf]; |
| 150 | |
| 151 | while (len_remain > 1 && *delimp) { |
| 152 | *cur++ = *delimp++; |
| 153 | len_remain--; |
| 154 | } |
| 155 | |
| 156 | *cur = 0; |
Harald Welte | 40481e8 | 2010-07-30 11:40:32 +0200 | [diff] [blame] | 157 | } |
| 158 | hexd_buff[sizeof(hexd_buff)-1] = 0; |
| 159 | return hexd_buff; |
| 160 | } |
Harald Welte | dee47cd | 2010-07-30 11:43:30 +0200 | [diff] [blame] | 161 | |
Harald Welte | 8598f18 | 2011-08-17 14:19:27 +0200 | [diff] [blame] | 162 | /*! \brief Convert a sequence of unpacked bits to ASCII string |
| 163 | * \param[in] bits A sequence of unpacked bits |
| 164 | * \param[in] len Length of bits |
| 165 | */ |
Pablo Neira Ayuso | 87f7b25 | 2011-05-07 12:43:08 +0200 | [diff] [blame] | 166 | char *osmo_ubit_dump(const uint8_t *bits, unsigned int len) |
Harald Welte | 3d0ac5e | 2011-02-08 16:55:03 +0100 | [diff] [blame] | 167 | { |
| 168 | int i; |
| 169 | |
| 170 | if (len > sizeof(hexd_buff)-1) |
| 171 | len = sizeof(hexd_buff)-1; |
| 172 | memset(hexd_buff, 0, sizeof(hexd_buff)); |
| 173 | |
| 174 | for (i = 0; i < len; i++) { |
| 175 | char outch; |
| 176 | switch (bits[i]) { |
| 177 | case 0: |
| 178 | outch = '0'; |
| 179 | break; |
| 180 | case 0xff: |
| 181 | outch = '?'; |
| 182 | break; |
| 183 | case 1: |
| 184 | outch = '1'; |
| 185 | break; |
| 186 | default: |
| 187 | outch = 'E'; |
| 188 | break; |
| 189 | } |
| 190 | hexd_buff[i] = outch; |
| 191 | } |
| 192 | hexd_buff[sizeof(hexd_buff)-1] = 0; |
| 193 | return hexd_buff; |
| 194 | } |
| 195 | |
Harald Welte | 8598f18 | 2011-08-17 14:19:27 +0200 | [diff] [blame] | 196 | /*! \brief Convert binary sequence to hexadecimal ASCII string |
| 197 | * \param[in] buf pointer to sequence of bytes |
| 198 | * \param[in] len length of buf in number of bytes |
| 199 | * \returns pointer to zero-terminated string |
| 200 | * |
| 201 | * This function will print a sequence of bytes as hexadecimal numbers, |
| 202 | * adding one space character between each byte (e.g. "1a ef d9") |
| 203 | */ |
Pablo Neira Ayuso | 87f7b25 | 2011-05-07 12:43:08 +0200 | [diff] [blame] | 204 | char *osmo_hexdump(const unsigned char *buf, int len) |
Harald Welte | dee47cd | 2010-07-30 11:43:30 +0200 | [diff] [blame] | 205 | { |
Pablo Neira Ayuso | 87f7b25 | 2011-05-07 12:43:08 +0200 | [diff] [blame] | 206 | return _osmo_hexdump(buf, len, " "); |
Harald Welte | dee47cd | 2010-07-30 11:43:30 +0200 | [diff] [blame] | 207 | } |
| 208 | |
Harald Welte | 8598f18 | 2011-08-17 14:19:27 +0200 | [diff] [blame] | 209 | /*! \brief Convert binary sequence to hexadecimal ASCII string |
| 210 | * \param[in] buf pointer to sequence of bytes |
| 211 | * \param[in] len length of buf in number of bytes |
| 212 | * \returns pointer to zero-terminated string |
| 213 | * |
| 214 | * This function will print a sequence of bytes as hexadecimal numbers, |
| 215 | * without any space character between each byte (e.g. "1aefd9") |
| 216 | */ |
Sylvain Munaut | ff23d24 | 2011-11-10 23:03:18 +0100 | [diff] [blame] | 217 | char *osmo_hexdump_nospc(const unsigned char *buf, int len) |
Harald Welte | dee47cd | 2010-07-30 11:43:30 +0200 | [diff] [blame] | 218 | { |
Pablo Neira Ayuso | 87f7b25 | 2011-05-07 12:43:08 +0200 | [diff] [blame] | 219 | return _osmo_hexdump(buf, len, ""); |
Harald Welte | dee47cd | 2010-07-30 11:43:30 +0200 | [diff] [blame] | 220 | } |
Harald Welte | 2822296 | 2011-02-18 20:37:04 +0100 | [diff] [blame] | 221 | |
Holger Hans Peter Freyther | 9a1a5a1 | 2015-04-11 19:26:55 +0200 | [diff] [blame] | 222 | /* Compat with previous typo to preserve abi */ |
Sylvain Munaut | e55ae3a | 2011-11-11 23:06:55 +0100 | [diff] [blame] | 223 | char *osmo_osmo_hexdump_nospc(const unsigned char *buf, int len) |
Holger Hans Peter Freyther | 9a1a5a1 | 2015-04-11 19:26:55 +0200 | [diff] [blame] | 224 | #if defined(__MACH__) && defined(__APPLE__) |
| 225 | ; |
| 226 | #else |
Sylvain Munaut | 17af41d | 2011-11-19 22:30:39 +0100 | [diff] [blame] | 227 | __attribute__((weak, alias("osmo_hexdump_nospc"))); |
Holger Hans Peter Freyther | 9a1a5a1 | 2015-04-11 19:26:55 +0200 | [diff] [blame] | 228 | #endif |
Sylvain Munaut | e55ae3a | 2011-11-11 23:06:55 +0100 | [diff] [blame] | 229 | |
Harald Welte | 2822296 | 2011-02-18 20:37:04 +0100 | [diff] [blame] | 230 | #include "../config.h" |
| 231 | #ifdef HAVE_CTYPE_H |
| 232 | #include <ctype.h> |
Harald Welte | 8598f18 | 2011-08-17 14:19:27 +0200 | [diff] [blame] | 233 | /*! \brief Convert an entire string to lower case |
| 234 | * \param[out] out output string, caller-allocated |
| 235 | * \param[in] in input string |
| 236 | */ |
Harald Welte | 2822296 | 2011-02-18 20:37:04 +0100 | [diff] [blame] | 237 | void osmo_str2lower(char *out, const char *in) |
| 238 | { |
| 239 | unsigned int i; |
| 240 | |
| 241 | for (i = 0; i < strlen(in); i++) |
| 242 | out[i] = tolower(in[i]); |
| 243 | out[strlen(in)] = '\0'; |
| 244 | } |
| 245 | |
Harald Welte | 8598f18 | 2011-08-17 14:19:27 +0200 | [diff] [blame] | 246 | /*! \brief Convert an entire string to upper case |
| 247 | * \param[out] out output string, caller-allocated |
| 248 | * \param[in] in input string |
| 249 | */ |
Harald Welte | 2822296 | 2011-02-18 20:37:04 +0100 | [diff] [blame] | 250 | void osmo_str2upper(char *out, const char *in) |
| 251 | { |
| 252 | unsigned int i; |
| 253 | |
| 254 | for (i = 0; i < strlen(in); i++) |
| 255 | out[i] = toupper(in[i]); |
| 256 | out[strlen(in)] = '\0'; |
| 257 | } |
| 258 | #endif /* HAVE_CTYPE_H */ |
Harald Welte | 8598f18 | 2011-08-17 14:19:27 +0200 | [diff] [blame] | 259 | |
Sylvain Munaut | dca7d2c | 2012-04-18 21:53:23 +0200 | [diff] [blame] | 260 | /*! @} */ |