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 | 9709b2e | 2016-04-25 18:47:53 +0200 | [diff] [blame] | 31 | #include <osmocom/core/bit64gen.h> |
| 32 | |
Harald Welte | d284cd9 | 2010-03-01 21:58:31 +0100 | [diff] [blame] | 33 | |
Harald Welte | 8598f18 | 2011-08-17 14:19:27 +0200 | [diff] [blame] | 34 | /*! \addtogroup utils |
| 35 | * @{ |
| 36 | */ |
| 37 | |
| 38 | /*! \file utils.c */ |
| 39 | |
Harald Welte | b59f935 | 2010-03-25 11:37:04 +0800 | [diff] [blame] | 40 | static char namebuf[255]; |
Harald Welte | 8598f18 | 2011-08-17 14:19:27 +0200 | [diff] [blame] | 41 | |
| 42 | /*! \brief get human-readable string for given value |
| 43 | * \param[in] vs Array of value_string tuples |
| 44 | * \param[in] val Value to be converted |
| 45 | * \returns pointer to human-readable string |
| 46 | */ |
Harald Welte | d284cd9 | 2010-03-01 21:58:31 +0100 | [diff] [blame] | 47 | const char *get_value_string(const struct value_string *vs, uint32_t val) |
| 48 | { |
| 49 | int i; |
| 50 | |
| 51 | for (i = 0;; i++) { |
| 52 | if (vs[i].value == 0 && vs[i].str == NULL) |
| 53 | break; |
| 54 | if (vs[i].value == val) |
| 55 | return vs[i].str; |
| 56 | } |
Harald Welte | b59f935 | 2010-03-25 11:37:04 +0800 | [diff] [blame] | 57 | |
| 58 | snprintf(namebuf, sizeof(namebuf), "unknown 0x%x", val); |
Holger Hans Peter Freyther | 8d50600 | 2013-07-04 20:14:10 +0200 | [diff] [blame] | 59 | namebuf[sizeof(namebuf) - 1] = '\0'; |
Harald Welte | b59f935 | 2010-03-25 11:37:04 +0800 | [diff] [blame] | 60 | return namebuf; |
Harald Welte | d284cd9 | 2010-03-01 21:58:31 +0100 | [diff] [blame] | 61 | } |
| 62 | |
Harald Welte | 8598f18 | 2011-08-17 14:19:27 +0200 | [diff] [blame] | 63 | /*! \brief get numeric value for given human-readable string |
| 64 | * \param[in] vs Array of value_string tuples |
| 65 | * \param[in] str human-readable string |
| 66 | * \returns numeric value (>0) or negative numer in case of error |
| 67 | */ |
Harald Welte | d284cd9 | 2010-03-01 21:58:31 +0100 | [diff] [blame] | 68 | int get_string_value(const struct value_string *vs, const char *str) |
| 69 | { |
| 70 | int i; |
| 71 | |
| 72 | for (i = 0;; i++) { |
| 73 | if (vs[i].value == 0 && vs[i].str == NULL) |
| 74 | break; |
| 75 | if (!strcasecmp(vs[i].str, str)) |
| 76 | return vs[i].value; |
| 77 | } |
| 78 | return -EINVAL; |
| 79 | } |
Harald Welte | a73e2f9 | 2010-03-04 10:50:32 +0100 | [diff] [blame] | 80 | |
Harald Welte | 8598f18 | 2011-08-17 14:19:27 +0200 | [diff] [blame] | 81 | /*! \brief Convert BCD-encoded digit into printable character |
| 82 | * \param[in] bcd A single BCD-encoded digit |
| 83 | * \returns single printable character |
| 84 | */ |
Pablo Neira Ayuso | 87f7b25 | 2011-05-07 12:43:08 +0200 | [diff] [blame] | 85 | char osmo_bcd2char(uint8_t bcd) |
Harald Welte | a73e2f9 | 2010-03-04 10:50:32 +0100 | [diff] [blame] | 86 | { |
| 87 | if (bcd < 0xa) |
| 88 | return '0' + bcd; |
| 89 | else |
| 90 | return 'A' + (bcd - 0xa); |
| 91 | } |
| 92 | |
Harald Welte | de6e498 | 2012-12-06 21:25:27 +0100 | [diff] [blame] | 93 | /*! \brief Convert number in ASCII to BCD value |
| 94 | * \param[in] c ASCII character |
| 95 | * \returns BCD encoded value of character |
| 96 | */ |
Pablo Neira Ayuso | 87f7b25 | 2011-05-07 12:43:08 +0200 | [diff] [blame] | 97 | uint8_t osmo_char2bcd(char c) |
Harald Welte | a73e2f9 | 2010-03-04 10:50:32 +0100 | [diff] [blame] | 98 | { |
| 99 | return c - 0x30; |
| 100 | } |
Harald Welte | 3eba991 | 2010-07-30 10:37:29 +0200 | [diff] [blame] | 101 | |
Harald Welte | de6e498 | 2012-12-06 21:25:27 +0100 | [diff] [blame] | 102 | /*! \brief Parse a string ocntaining hexadecimal digits |
| 103 | * \param[in] str string containing ASCII encoded hexadecimal digits |
| 104 | * \param[out] b output buffer |
| 105 | * \param[in] max_len maximum space in output buffer |
Neels Hofmeyr | 3de7b05 | 2015-09-23 23:16:53 +0200 | [diff] [blame] | 106 | * \returns number of parsed octets, or -1 on error |
Harald Welte | de6e498 | 2012-12-06 21:25:27 +0100 | [diff] [blame] | 107 | */ |
Pablo Neira Ayuso | 87f7b25 | 2011-05-07 12:43:08 +0200 | [diff] [blame] | 108 | int osmo_hexparse(const char *str, uint8_t *b, int max_len) |
Harald Welte | 3eba991 | 2010-07-30 10:37:29 +0200 | [diff] [blame] | 109 | |
| 110 | { |
| 111 | int i, l, v; |
| 112 | |
| 113 | l = strlen(str); |
| 114 | if ((l&1) || ((l>>1) > max_len)) |
| 115 | return -1; |
| 116 | |
| 117 | memset(b, 0x00, max_len); |
| 118 | |
| 119 | for (i=0; i<l; i++) { |
| 120 | char c = str[i]; |
| 121 | if (c >= '0' && c <= '9') |
| 122 | v = c - '0'; |
| 123 | else if (c >= 'a' && c <= 'f') |
| 124 | v = 10 + (c - 'a'); |
| 125 | else if (c >= 'A' && c <= 'F') |
| 126 | v = 10 + (c - 'A'); |
| 127 | else |
| 128 | return -1; |
| 129 | b[i>>1] |= v << (i&1 ? 0 : 4); |
| 130 | } |
| 131 | |
| 132 | return i>>1; |
| 133 | } |
Harald Welte | 40481e8 | 2010-07-30 11:40:32 +0200 | [diff] [blame] | 134 | |
| 135 | static char hexd_buff[4096]; |
Nils O. Selåsdal | 3244702 | 2014-01-02 14:04:43 +0100 | [diff] [blame] | 136 | static const char hex_chars[] = "0123456789abcdef"; |
Harald Welte | 40481e8 | 2010-07-30 11:40:32 +0200 | [diff] [blame] | 137 | |
Pablo Neira Ayuso | 87f7b25 | 2011-05-07 12:43:08 +0200 | [diff] [blame] | 138 | static char *_osmo_hexdump(const unsigned char *buf, int len, char *delim) |
Harald Welte | 40481e8 | 2010-07-30 11:40:32 +0200 | [diff] [blame] | 139 | { |
| 140 | int i; |
| 141 | char *cur = hexd_buff; |
| 142 | |
| 143 | hexd_buff[0] = 0; |
| 144 | for (i = 0; i < len; i++) { |
Nils O. Selåsdal | 3244702 | 2014-01-02 14:04:43 +0100 | [diff] [blame] | 145 | const char *delimp = delim; |
Harald Welte | 40481e8 | 2010-07-30 11:40:32 +0200 | [diff] [blame] | 146 | int len_remain = sizeof(hexd_buff) - (cur - hexd_buff); |
Nils O. Selåsdal | 3244702 | 2014-01-02 14:04:43 +0100 | [diff] [blame] | 147 | if (len_remain < 3) |
Holger Hans Peter Freyther | 128d9e2 | 2011-07-15 16:07:23 +0200 | [diff] [blame] | 148 | break; |
Nils O. Selåsdal | 3244702 | 2014-01-02 14:04:43 +0100 | [diff] [blame] | 149 | |
| 150 | *cur++ = hex_chars[buf[i] >> 4]; |
| 151 | *cur++ = hex_chars[buf[i] & 0xf]; |
| 152 | |
| 153 | while (len_remain > 1 && *delimp) { |
| 154 | *cur++ = *delimp++; |
| 155 | len_remain--; |
| 156 | } |
| 157 | |
| 158 | *cur = 0; |
Harald Welte | 40481e8 | 2010-07-30 11:40:32 +0200 | [diff] [blame] | 159 | } |
| 160 | hexd_buff[sizeof(hexd_buff)-1] = 0; |
| 161 | return hexd_buff; |
| 162 | } |
Harald Welte | dee47cd | 2010-07-30 11:43:30 +0200 | [diff] [blame] | 163 | |
Harald Welte | 8598f18 | 2011-08-17 14:19:27 +0200 | [diff] [blame] | 164 | /*! \brief Convert a sequence of unpacked bits to ASCII string |
| 165 | * \param[in] bits A sequence of unpacked bits |
| 166 | * \param[in] len Length of bits |
| 167 | */ |
Pablo Neira Ayuso | 87f7b25 | 2011-05-07 12:43:08 +0200 | [diff] [blame] | 168 | char *osmo_ubit_dump(const uint8_t *bits, unsigned int len) |
Harald Welte | 3d0ac5e | 2011-02-08 16:55:03 +0100 | [diff] [blame] | 169 | { |
| 170 | int i; |
| 171 | |
| 172 | if (len > sizeof(hexd_buff)-1) |
| 173 | len = sizeof(hexd_buff)-1; |
| 174 | memset(hexd_buff, 0, sizeof(hexd_buff)); |
| 175 | |
| 176 | for (i = 0; i < len; i++) { |
| 177 | char outch; |
| 178 | switch (bits[i]) { |
| 179 | case 0: |
| 180 | outch = '0'; |
| 181 | break; |
| 182 | case 0xff: |
| 183 | outch = '?'; |
| 184 | break; |
| 185 | case 1: |
| 186 | outch = '1'; |
| 187 | break; |
| 188 | default: |
| 189 | outch = 'E'; |
| 190 | break; |
| 191 | } |
| 192 | hexd_buff[i] = outch; |
| 193 | } |
| 194 | hexd_buff[sizeof(hexd_buff)-1] = 0; |
| 195 | return hexd_buff; |
| 196 | } |
| 197 | |
Harald Welte | 8598f18 | 2011-08-17 14:19:27 +0200 | [diff] [blame] | 198 | /*! \brief Convert binary sequence to hexadecimal ASCII string |
| 199 | * \param[in] buf pointer to sequence of bytes |
| 200 | * \param[in] len length of buf in number of bytes |
| 201 | * \returns pointer to zero-terminated string |
| 202 | * |
| 203 | * This function will print a sequence of bytes as hexadecimal numbers, |
| 204 | * adding one space character between each byte (e.g. "1a ef d9") |
| 205 | */ |
Pablo Neira Ayuso | 87f7b25 | 2011-05-07 12:43:08 +0200 | [diff] [blame] | 206 | char *osmo_hexdump(const unsigned char *buf, int len) |
Harald Welte | dee47cd | 2010-07-30 11:43:30 +0200 | [diff] [blame] | 207 | { |
Pablo Neira Ayuso | 87f7b25 | 2011-05-07 12:43:08 +0200 | [diff] [blame] | 208 | return _osmo_hexdump(buf, len, " "); |
Harald Welte | dee47cd | 2010-07-30 11:43:30 +0200 | [diff] [blame] | 209 | } |
| 210 | |
Harald Welte | 8598f18 | 2011-08-17 14:19:27 +0200 | [diff] [blame] | 211 | /*! \brief Convert binary sequence to hexadecimal ASCII string |
| 212 | * \param[in] buf pointer to sequence of bytes |
| 213 | * \param[in] len length of buf in number of bytes |
| 214 | * \returns pointer to zero-terminated string |
| 215 | * |
| 216 | * This function will print a sequence of bytes as hexadecimal numbers, |
| 217 | * without any space character between each byte (e.g. "1aefd9") |
| 218 | */ |
Sylvain Munaut | ff23d24 | 2011-11-10 23:03:18 +0100 | [diff] [blame] | 219 | char *osmo_hexdump_nospc(const unsigned char *buf, int len) |
Harald Welte | dee47cd | 2010-07-30 11:43:30 +0200 | [diff] [blame] | 220 | { |
Pablo Neira Ayuso | 87f7b25 | 2011-05-07 12:43:08 +0200 | [diff] [blame] | 221 | return _osmo_hexdump(buf, len, ""); |
Harald Welte | dee47cd | 2010-07-30 11:43:30 +0200 | [diff] [blame] | 222 | } |
Harald Welte | 2822296 | 2011-02-18 20:37:04 +0100 | [diff] [blame] | 223 | |
Holger Hans Peter Freyther | 9a1a5a1 | 2015-04-11 19:26:55 +0200 | [diff] [blame] | 224 | /* Compat with previous typo to preserve abi */ |
Sylvain Munaut | e55ae3a | 2011-11-11 23:06:55 +0100 | [diff] [blame] | 225 | 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] | 226 | #if defined(__MACH__) && defined(__APPLE__) |
| 227 | ; |
| 228 | #else |
Sylvain Munaut | 17af41d | 2011-11-19 22:30:39 +0100 | [diff] [blame] | 229 | __attribute__((weak, alias("osmo_hexdump_nospc"))); |
Holger Hans Peter Freyther | 9a1a5a1 | 2015-04-11 19:26:55 +0200 | [diff] [blame] | 230 | #endif |
Sylvain Munaut | e55ae3a | 2011-11-11 23:06:55 +0100 | [diff] [blame] | 231 | |
Harald Welte | 2822296 | 2011-02-18 20:37:04 +0100 | [diff] [blame] | 232 | #include "../config.h" |
| 233 | #ifdef HAVE_CTYPE_H |
| 234 | #include <ctype.h> |
Harald Welte | 8598f18 | 2011-08-17 14:19:27 +0200 | [diff] [blame] | 235 | /*! \brief Convert an entire string to lower case |
| 236 | * \param[out] out output string, caller-allocated |
| 237 | * \param[in] in input string |
| 238 | */ |
Harald Welte | 2822296 | 2011-02-18 20:37:04 +0100 | [diff] [blame] | 239 | void osmo_str2lower(char *out, const char *in) |
| 240 | { |
| 241 | unsigned int i; |
| 242 | |
| 243 | for (i = 0; i < strlen(in); i++) |
| 244 | out[i] = tolower(in[i]); |
| 245 | out[strlen(in)] = '\0'; |
| 246 | } |
| 247 | |
Harald Welte | 8598f18 | 2011-08-17 14:19:27 +0200 | [diff] [blame] | 248 | /*! \brief Convert an entire string to upper case |
| 249 | * \param[out] out output string, caller-allocated |
| 250 | * \param[in] in input string |
| 251 | */ |
Harald Welte | 2822296 | 2011-02-18 20:37:04 +0100 | [diff] [blame] | 252 | void osmo_str2upper(char *out, const char *in) |
| 253 | { |
| 254 | unsigned int i; |
| 255 | |
| 256 | for (i = 0; i < strlen(in); i++) |
| 257 | out[i] = toupper(in[i]); |
| 258 | out[strlen(in)] = '\0'; |
| 259 | } |
| 260 | #endif /* HAVE_CTYPE_H */ |
Harald Welte | 8598f18 | 2011-08-17 14:19:27 +0200 | [diff] [blame] | 261 | |
Harald Welte | 9709b2e | 2016-04-25 18:47:53 +0200 | [diff] [blame] | 262 | /*! \brief Wishful thinking to generate a constant time compare |
| 263 | * \param[in] exp Expected data |
| 264 | * \param[in] rel Comparison value |
| 265 | * \param[in] count Number of bytes to compare |
| 266 | * \returns 1 in case \a exp equals \a rel; zero otherwise |
| 267 | * |
| 268 | * Compare count bytes of exp to rel. Return 0 if they are identical, 1 |
| 269 | * otherwise. Do not return a mismatch on the first mismatching byte, |
| 270 | * but always compare all bytes, regardless. The idea is that the amount of |
| 271 | * matching bytes cannot be inferred from the time the comparison took. */ |
| 272 | int osmo_constant_time_cmp(const uint8_t *exp, const uint8_t *rel, const int count) |
| 273 | { |
| 274 | int x = 0, i; |
| 275 | |
| 276 | for (i = 0; i < count; ++i) |
| 277 | x |= exp[i] ^ rel[i]; |
| 278 | |
| 279 | /* if x is zero, all data was identical */ |
| 280 | return x? 1 : 0; |
| 281 | } |
| 282 | |
| 283 | /*! \brief Generic retrieval of 1..8 bytes as big-endian uint64_t |
| 284 | * \param[in] data Input data as byte-array |
| 285 | * \param[in] data_len Length of \a data in octets |
| 286 | * \returns uint64_t of \a data interpreted as big-endian |
| 287 | * |
| 288 | * This is like osmo_load64be_ext, except that if data_len is less than |
| 289 | * sizeof(uint64_t), the data is interpreted as the least significant bytes |
| 290 | * (osmo_load64be_ext loads them as the most significant bytes into the |
| 291 | * returned uint64_t). In this way, any integer size up to 64 bits can be |
| 292 | * decoded conveniently by using sizeof(), without the need to call specific |
| 293 | * numbered functions (osmo_load16, 32, ...). */ |
| 294 | uint64_t osmo_decode_big_endian(const uint8_t *data, size_t data_len) |
| 295 | { |
| 296 | uint64_t value = 0; |
| 297 | |
| 298 | while (data_len > 0) { |
| 299 | value = (value << 8) + *data; |
| 300 | data += 1; |
| 301 | data_len -= 1; |
| 302 | } |
| 303 | |
| 304 | return value; |
| 305 | } |
| 306 | |
| 307 | /*! \brief Generic big-endian encoding of big endian number up to 64bit |
| 308 | * \param[in] value unsigned integer value to be stored |
| 309 | * \param[in] data_len number of octets |
| 310 | * \returns static buffer containing big-endian stored value |
| 311 | * |
| 312 | * This is like osmo_store64be_ext, except that this returns a static buffer of |
| 313 | * the result (for convenience, but not threadsafe). If data_len is less than |
| 314 | * sizeof(uint64_t), only the least significant bytes of value are encoded. */ |
| 315 | uint8_t *osmo_encode_big_endian(uint64_t value, size_t data_len) |
| 316 | { |
| 317 | static uint8_t buf[sizeof(uint64_t)]; |
| 318 | OSMO_ASSERT(data_len <= ARRAY_SIZE(buf)); |
| 319 | osmo_store64be_ext(value, buf, data_len); |
| 320 | return buf; |
| 321 | } |
Sylvain Munaut | dca7d2c | 2012-04-18 21:53:23 +0200 | [diff] [blame] | 322 | /*! @} */ |