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 | * |
Harald Welte | e08da97 | 2017-11-13 01:00:26 +0900 | [diff] [blame] | 8 | * SPDX-License-Identifier: GPL-2.0+ |
| 9 | * |
Harald Welte | 468b643 | 2014-09-11 13:05:51 +0800 | [diff] [blame] | 10 | * This program is free software; you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU General Public License as published by |
| 12 | * the Free Software Foundation; either version 2 of the License, or |
| 13 | * (at your option) any later version. |
| 14 | * |
| 15 | * This program is distributed in the hope that it will be useful, |
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | * GNU General Public License for more details. |
| 19 | * |
| 20 | * You should have received a copy of the GNU General Public License along |
| 21 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 22 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 23 | * |
| 24 | */ |
| 25 | |
Harald Welte | d284cd9 | 2010-03-01 21:58:31 +0100 | [diff] [blame] | 26 | |
Harald Welte | febe83c | 2017-10-03 17:41:59 +0800 | [diff] [blame] | 27 | #include <stdbool.h> |
Harald Welte | d284cd9 | 2010-03-01 21:58:31 +0100 | [diff] [blame] | 28 | #include <string.h> |
| 29 | #include <stdint.h> |
| 30 | #include <errno.h> |
Harald Welte | b59f935 | 2010-03-25 11:37:04 +0800 | [diff] [blame] | 31 | #include <stdio.h> |
Pau Espin Pedrol | 4573502 | 2017-06-18 14:05:24 +0200 | [diff] [blame] | 32 | #include <inttypes.h> |
Harald Welte | d284cd9 | 2010-03-01 21:58:31 +0100 | [diff] [blame] | 33 | |
Pablo Neira Ayuso | 8341934 | 2011-03-22 16:36:13 +0100 | [diff] [blame] | 34 | #include <osmocom/core/utils.h> |
Harald Welte | 9709b2e | 2016-04-25 18:47:53 +0200 | [diff] [blame] | 35 | #include <osmocom/core/bit64gen.h> |
| 36 | |
Harald Welte | d284cd9 | 2010-03-01 21:58:31 +0100 | [diff] [blame] | 37 | |
Harald Welte | 8598f18 | 2011-08-17 14:19:27 +0200 | [diff] [blame] | 38 | /*! \addtogroup utils |
| 39 | * @{ |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 40 | * various utility routines |
Neels Hofmeyr | 17518fe | 2017-06-20 04:35:06 +0200 | [diff] [blame] | 41 | * |
| 42 | * \file utils.c */ |
Harald Welte | 8598f18 | 2011-08-17 14:19:27 +0200 | [diff] [blame] | 43 | |
Harald Welte | b59f935 | 2010-03-25 11:37:04 +0800 | [diff] [blame] | 44 | static char namebuf[255]; |
Harald Welte | 8598f18 | 2011-08-17 14:19:27 +0200 | [diff] [blame] | 45 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 46 | /*! get human-readable string for given value |
Harald Welte | 8598f18 | 2011-08-17 14:19:27 +0200 | [diff] [blame] | 47 | * \param[in] vs Array of value_string tuples |
| 48 | * \param[in] val Value to be converted |
| 49 | * \returns pointer to human-readable string |
Neels Hofmeyr | 8a3c83e | 2016-06-13 13:16:58 +0200 | [diff] [blame] | 50 | * |
| 51 | * If val is found in vs, the array's string entry is returned. Otherwise, an |
| 52 | * "unknown" string containing the actual value is composed in a static buffer |
| 53 | * that is reused across invocations. |
Harald Welte | 8598f18 | 2011-08-17 14:19:27 +0200 | [diff] [blame] | 54 | */ |
Harald Welte | d284cd9 | 2010-03-01 21:58:31 +0100 | [diff] [blame] | 55 | const char *get_value_string(const struct value_string *vs, uint32_t val) |
| 56 | { |
Neels Hofmeyr | 8d6dcd9 | 2016-06-06 18:05:23 +0200 | [diff] [blame] | 57 | const char *str = get_value_string_or_null(vs, val); |
| 58 | if (str) |
| 59 | return str; |
| 60 | |
Pau Espin Pedrol | 4573502 | 2017-06-18 14:05:24 +0200 | [diff] [blame] | 61 | snprintf(namebuf, sizeof(namebuf), "unknown 0x%"PRIx32, val); |
Neels Hofmeyr | 8d6dcd9 | 2016-06-06 18:05:23 +0200 | [diff] [blame] | 62 | namebuf[sizeof(namebuf) - 1] = '\0'; |
| 63 | return namebuf; |
| 64 | } |
| 65 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 66 | /*! get human-readable string or NULL for given value |
Neels Hofmeyr | 8d6dcd9 | 2016-06-06 18:05:23 +0200 | [diff] [blame] | 67 | * \param[in] vs Array of value_string tuples |
| 68 | * \param[in] val Value to be converted |
| 69 | * \returns pointer to human-readable string or NULL if val is not found |
| 70 | */ |
| 71 | const char *get_value_string_or_null(const struct value_string *vs, |
| 72 | uint32_t val) |
| 73 | { |
Harald Welte | d284cd9 | 2010-03-01 21:58:31 +0100 | [diff] [blame] | 74 | int i; |
| 75 | |
| 76 | for (i = 0;; i++) { |
| 77 | if (vs[i].value == 0 && vs[i].str == NULL) |
| 78 | break; |
| 79 | if (vs[i].value == val) |
| 80 | return vs[i].str; |
| 81 | } |
Harald Welte | b59f935 | 2010-03-25 11:37:04 +0800 | [diff] [blame] | 82 | |
Neels Hofmeyr | 8d6dcd9 | 2016-06-06 18:05:23 +0200 | [diff] [blame] | 83 | return NULL; |
Harald Welte | d284cd9 | 2010-03-01 21:58:31 +0100 | [diff] [blame] | 84 | } |
| 85 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 86 | /*! get numeric value for given human-readable string |
Harald Welte | 8598f18 | 2011-08-17 14:19:27 +0200 | [diff] [blame] | 87 | * \param[in] vs Array of value_string tuples |
| 88 | * \param[in] str human-readable string |
| 89 | * \returns numeric value (>0) or negative numer in case of error |
| 90 | */ |
Harald Welte | d284cd9 | 2010-03-01 21:58:31 +0100 | [diff] [blame] | 91 | int get_string_value(const struct value_string *vs, const char *str) |
| 92 | { |
| 93 | int i; |
| 94 | |
| 95 | for (i = 0;; i++) { |
| 96 | if (vs[i].value == 0 && vs[i].str == NULL) |
| 97 | break; |
| 98 | if (!strcasecmp(vs[i].str, str)) |
| 99 | return vs[i].value; |
| 100 | } |
| 101 | return -EINVAL; |
| 102 | } |
Harald Welte | a73e2f9 | 2010-03-04 10:50:32 +0100 | [diff] [blame] | 103 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 104 | /*! Convert BCD-encoded digit into printable character |
Harald Welte | 8598f18 | 2011-08-17 14:19:27 +0200 | [diff] [blame] | 105 | * \param[in] bcd A single BCD-encoded digit |
| 106 | * \returns single printable character |
| 107 | */ |
Pablo Neira Ayuso | 87f7b25 | 2011-05-07 12:43:08 +0200 | [diff] [blame] | 108 | char osmo_bcd2char(uint8_t bcd) |
Harald Welte | a73e2f9 | 2010-03-04 10:50:32 +0100 | [diff] [blame] | 109 | { |
| 110 | if (bcd < 0xa) |
| 111 | return '0' + bcd; |
| 112 | else |
| 113 | return 'A' + (bcd - 0xa); |
| 114 | } |
| 115 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 116 | /*! Convert number in ASCII to BCD value |
Harald Welte | de6e498 | 2012-12-06 21:25:27 +0100 | [diff] [blame] | 117 | * \param[in] c ASCII character |
| 118 | * \returns BCD encoded value of character |
| 119 | */ |
Pablo Neira Ayuso | 87f7b25 | 2011-05-07 12:43:08 +0200 | [diff] [blame] | 120 | uint8_t osmo_char2bcd(char c) |
Harald Welte | a73e2f9 | 2010-03-04 10:50:32 +0100 | [diff] [blame] | 121 | { |
Harald Welte | fa8983d | 2017-10-27 16:52:59 +0200 | [diff] [blame] | 122 | if (c >= '0' && c <= '9') |
| 123 | return c - 0x30; |
| 124 | else if (c >= 'A' && c <= 'F') |
| 125 | return 0xa + (c - 'A'); |
| 126 | else if (c >= 'a' && c <= 'f') |
| 127 | return 0xa + (c - 'a'); |
| 128 | else |
| 129 | return 0; |
Harald Welte | a73e2f9 | 2010-03-04 10:50:32 +0100 | [diff] [blame] | 130 | } |
Harald Welte | 3eba991 | 2010-07-30 10:37:29 +0200 | [diff] [blame] | 131 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 132 | /*! Parse a string containing hexadecimal digits |
Harald Welte | de6e498 | 2012-12-06 21:25:27 +0100 | [diff] [blame] | 133 | * \param[in] str string containing ASCII encoded hexadecimal digits |
| 134 | * \param[out] b output buffer |
| 135 | * \param[in] max_len maximum space in output buffer |
Neels Hofmeyr | 3de7b05 | 2015-09-23 23:16:53 +0200 | [diff] [blame] | 136 | * \returns number of parsed octets, or -1 on error |
Harald Welte | de6e498 | 2012-12-06 21:25:27 +0100 | [diff] [blame] | 137 | */ |
Pablo Neira Ayuso | 87f7b25 | 2011-05-07 12:43:08 +0200 | [diff] [blame] | 138 | int osmo_hexparse(const char *str, uint8_t *b, int max_len) |
Harald Welte | 3eba991 | 2010-07-30 10:37:29 +0200 | [diff] [blame] | 139 | |
| 140 | { |
Neels Hofmeyr | 437ed4a | 2017-02-14 15:54:31 +0100 | [diff] [blame] | 141 | char c; |
| 142 | uint8_t v; |
| 143 | const char *strpos; |
| 144 | unsigned int nibblepos = 0; |
Harald Welte | 3eba991 | 2010-07-30 10:37:29 +0200 | [diff] [blame] | 145 | |
| 146 | memset(b, 0x00, max_len); |
| 147 | |
Neels Hofmeyr | 437ed4a | 2017-02-14 15:54:31 +0100 | [diff] [blame] | 148 | for (strpos = str; (c = *strpos); strpos++) { |
| 149 | /* skip whitespace */ |
| 150 | if (c == ' ' || c == '\t' || c == '\n' || c == '\r') |
| 151 | continue; |
| 152 | |
| 153 | /* If the buffer is too small, error out */ |
| 154 | if (nibblepos >= (max_len << 1)) |
| 155 | return -1; |
| 156 | |
Harald Welte | 3eba991 | 2010-07-30 10:37:29 +0200 | [diff] [blame] | 157 | if (c >= '0' && c <= '9') |
| 158 | v = c - '0'; |
| 159 | else if (c >= 'a' && c <= 'f') |
| 160 | v = 10 + (c - 'a'); |
| 161 | else if (c >= 'A' && c <= 'F') |
| 162 | v = 10 + (c - 'A'); |
| 163 | else |
| 164 | return -1; |
Neels Hofmeyr | 437ed4a | 2017-02-14 15:54:31 +0100 | [diff] [blame] | 165 | |
| 166 | b[nibblepos >> 1] |= v << (nibblepos & 1 ? 0 : 4); |
| 167 | nibblepos ++; |
Harald Welte | 3eba991 | 2010-07-30 10:37:29 +0200 | [diff] [blame] | 168 | } |
| 169 | |
Neels Hofmeyr | 437ed4a | 2017-02-14 15:54:31 +0100 | [diff] [blame] | 170 | /* In case of uneven amount of digits, the last byte is not complete |
| 171 | * and that's an error. */ |
| 172 | if (nibblepos & 1) |
| 173 | return -1; |
| 174 | |
| 175 | return nibblepos >> 1; |
Harald Welte | 3eba991 | 2010-07-30 10:37:29 +0200 | [diff] [blame] | 176 | } |
Harald Welte | 40481e8 | 2010-07-30 11:40:32 +0200 | [diff] [blame] | 177 | |
| 178 | static char hexd_buff[4096]; |
Nils O. SelÄsdal | 3244702 | 2014-01-02 14:04:43 +0100 | [diff] [blame] | 179 | static const char hex_chars[] = "0123456789abcdef"; |
Harald Welte | 40481e8 | 2010-07-30 11:40:32 +0200 | [diff] [blame] | 180 | |
Pablo Neira Ayuso | 87f7b25 | 2011-05-07 12:43:08 +0200 | [diff] [blame] | 181 | static char *_osmo_hexdump(const unsigned char *buf, int len, char *delim) |
Harald Welte | 40481e8 | 2010-07-30 11:40:32 +0200 | [diff] [blame] | 182 | { |
| 183 | int i; |
| 184 | char *cur = hexd_buff; |
| 185 | |
| 186 | hexd_buff[0] = 0; |
| 187 | for (i = 0; i < len; i++) { |
Nils O. SelÄsdal | 3244702 | 2014-01-02 14:04:43 +0100 | [diff] [blame] | 188 | const char *delimp = delim; |
Harald Welte | 40481e8 | 2010-07-30 11:40:32 +0200 | [diff] [blame] | 189 | int len_remain = sizeof(hexd_buff) - (cur - hexd_buff); |
Nils O. SelÄsdal | 3244702 | 2014-01-02 14:04:43 +0100 | [diff] [blame] | 190 | if (len_remain < 3) |
Holger Hans Peter Freyther | 128d9e2 | 2011-07-15 16:07:23 +0200 | [diff] [blame] | 191 | break; |
Nils O. SelÄsdal | 3244702 | 2014-01-02 14:04:43 +0100 | [diff] [blame] | 192 | |
| 193 | *cur++ = hex_chars[buf[i] >> 4]; |
| 194 | *cur++ = hex_chars[buf[i] & 0xf]; |
| 195 | |
| 196 | while (len_remain > 1 && *delimp) { |
| 197 | *cur++ = *delimp++; |
| 198 | len_remain--; |
| 199 | } |
| 200 | |
| 201 | *cur = 0; |
Harald Welte | 40481e8 | 2010-07-30 11:40:32 +0200 | [diff] [blame] | 202 | } |
| 203 | hexd_buff[sizeof(hexd_buff)-1] = 0; |
| 204 | return hexd_buff; |
| 205 | } |
Harald Welte | dee47cd | 2010-07-30 11:43:30 +0200 | [diff] [blame] | 206 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 207 | /*! Convert a sequence of unpacked bits to ASCII string |
Harald Welte | 8598f18 | 2011-08-17 14:19:27 +0200 | [diff] [blame] | 208 | * \param[in] bits A sequence of unpacked bits |
| 209 | * \param[in] len Length of bits |
| 210 | */ |
Pablo Neira Ayuso | 87f7b25 | 2011-05-07 12:43:08 +0200 | [diff] [blame] | 211 | char *osmo_ubit_dump(const uint8_t *bits, unsigned int len) |
Harald Welte | 3d0ac5e | 2011-02-08 16:55:03 +0100 | [diff] [blame] | 212 | { |
| 213 | int i; |
| 214 | |
| 215 | if (len > sizeof(hexd_buff)-1) |
| 216 | len = sizeof(hexd_buff)-1; |
| 217 | memset(hexd_buff, 0, sizeof(hexd_buff)); |
| 218 | |
| 219 | for (i = 0; i < len; i++) { |
| 220 | char outch; |
| 221 | switch (bits[i]) { |
| 222 | case 0: |
| 223 | outch = '0'; |
| 224 | break; |
| 225 | case 0xff: |
| 226 | outch = '?'; |
| 227 | break; |
| 228 | case 1: |
| 229 | outch = '1'; |
| 230 | break; |
| 231 | default: |
| 232 | outch = 'E'; |
| 233 | break; |
| 234 | } |
| 235 | hexd_buff[i] = outch; |
| 236 | } |
| 237 | hexd_buff[sizeof(hexd_buff)-1] = 0; |
| 238 | return hexd_buff; |
| 239 | } |
| 240 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 241 | /*! Convert binary sequence to hexadecimal ASCII string |
Harald Welte | 8598f18 | 2011-08-17 14:19:27 +0200 | [diff] [blame] | 242 | * \param[in] buf pointer to sequence of bytes |
| 243 | * \param[in] len length of buf in number of bytes |
| 244 | * \returns pointer to zero-terminated string |
| 245 | * |
| 246 | * This function will print a sequence of bytes as hexadecimal numbers, |
| 247 | * adding one space character between each byte (e.g. "1a ef d9") |
Harald Welte | 096a666 | 2017-10-16 14:33:11 +0200 | [diff] [blame] | 248 | * |
| 249 | * The maximum size of the output buffer is 4096 bytes, i.e. the maximum |
| 250 | * number of input bytes that can be printed in one call is 1365! |
Harald Welte | 8598f18 | 2011-08-17 14:19:27 +0200 | [diff] [blame] | 251 | */ |
Pablo Neira Ayuso | 87f7b25 | 2011-05-07 12:43:08 +0200 | [diff] [blame] | 252 | char *osmo_hexdump(const unsigned char *buf, int len) |
Harald Welte | dee47cd | 2010-07-30 11:43:30 +0200 | [diff] [blame] | 253 | { |
Pablo Neira Ayuso | 87f7b25 | 2011-05-07 12:43:08 +0200 | [diff] [blame] | 254 | return _osmo_hexdump(buf, len, " "); |
Harald Welte | dee47cd | 2010-07-30 11:43:30 +0200 | [diff] [blame] | 255 | } |
| 256 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 257 | /*! Convert binary sequence to hexadecimal ASCII string |
Harald Welte | 8598f18 | 2011-08-17 14:19:27 +0200 | [diff] [blame] | 258 | * \param[in] buf pointer to sequence of bytes |
| 259 | * \param[in] len length of buf in number of bytes |
| 260 | * \returns pointer to zero-terminated string |
| 261 | * |
| 262 | * This function will print a sequence of bytes as hexadecimal numbers, |
| 263 | * without any space character between each byte (e.g. "1aefd9") |
Harald Welte | 096a666 | 2017-10-16 14:33:11 +0200 | [diff] [blame] | 264 | * |
| 265 | * The maximum size of the output buffer is 4096 bytes, i.e. the maximum |
| 266 | * number of input bytes that can be printed in one call is 2048! |
Harald Welte | 8598f18 | 2011-08-17 14:19:27 +0200 | [diff] [blame] | 267 | */ |
Sylvain Munaut | ff23d24 | 2011-11-10 23:03:18 +0100 | [diff] [blame] | 268 | char *osmo_hexdump_nospc(const unsigned char *buf, int len) |
Harald Welte | dee47cd | 2010-07-30 11:43:30 +0200 | [diff] [blame] | 269 | { |
Pablo Neira Ayuso | 87f7b25 | 2011-05-07 12:43:08 +0200 | [diff] [blame] | 270 | return _osmo_hexdump(buf, len, ""); |
Harald Welte | dee47cd | 2010-07-30 11:43:30 +0200 | [diff] [blame] | 271 | } |
Harald Welte | 2822296 | 2011-02-18 20:37:04 +0100 | [diff] [blame] | 272 | |
Holger Hans Peter Freyther | 9a1a5a1 | 2015-04-11 19:26:55 +0200 | [diff] [blame] | 273 | /* Compat with previous typo to preserve abi */ |
Sylvain Munaut | e55ae3a | 2011-11-11 23:06:55 +0100 | [diff] [blame] | 274 | 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] | 275 | #if defined(__MACH__) && defined(__APPLE__) |
| 276 | ; |
| 277 | #else |
Sylvain Munaut | 17af41d | 2011-11-19 22:30:39 +0100 | [diff] [blame] | 278 | __attribute__((weak, alias("osmo_hexdump_nospc"))); |
Holger Hans Peter Freyther | 9a1a5a1 | 2015-04-11 19:26:55 +0200 | [diff] [blame] | 279 | #endif |
Sylvain Munaut | e55ae3a | 2011-11-11 23:06:55 +0100 | [diff] [blame] | 280 | |
Harald Welte | 2822296 | 2011-02-18 20:37:04 +0100 | [diff] [blame] | 281 | #include "../config.h" |
| 282 | #ifdef HAVE_CTYPE_H |
| 283 | #include <ctype.h> |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 284 | /*! Convert an entire string to lower case |
Harald Welte | 8598f18 | 2011-08-17 14:19:27 +0200 | [diff] [blame] | 285 | * \param[out] out output string, caller-allocated |
| 286 | * \param[in] in input string |
| 287 | */ |
Harald Welte | 2822296 | 2011-02-18 20:37:04 +0100 | [diff] [blame] | 288 | void osmo_str2lower(char *out, const char *in) |
| 289 | { |
| 290 | unsigned int i; |
| 291 | |
| 292 | for (i = 0; i < strlen(in); i++) |
Pau Espin Pedrol | 399a6f0 | 2017-06-18 14:07:37 +0200 | [diff] [blame] | 293 | out[i] = tolower((const unsigned char)in[i]); |
Harald Welte | 2822296 | 2011-02-18 20:37:04 +0100 | [diff] [blame] | 294 | out[strlen(in)] = '\0'; |
| 295 | } |
| 296 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 297 | /*! Convert an entire string to upper case |
Harald Welte | 8598f18 | 2011-08-17 14:19:27 +0200 | [diff] [blame] | 298 | * \param[out] out output string, caller-allocated |
| 299 | * \param[in] in input string |
| 300 | */ |
Harald Welte | 2822296 | 2011-02-18 20:37:04 +0100 | [diff] [blame] | 301 | void osmo_str2upper(char *out, const char *in) |
| 302 | { |
| 303 | unsigned int i; |
| 304 | |
| 305 | for (i = 0; i < strlen(in); i++) |
Pau Espin Pedrol | 399a6f0 | 2017-06-18 14:07:37 +0200 | [diff] [blame] | 306 | out[i] = toupper((const unsigned char)in[i]); |
Harald Welte | 2822296 | 2011-02-18 20:37:04 +0100 | [diff] [blame] | 307 | out[strlen(in)] = '\0'; |
| 308 | } |
| 309 | #endif /* HAVE_CTYPE_H */ |
Harald Welte | 8598f18 | 2011-08-17 14:19:27 +0200 | [diff] [blame] | 310 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 311 | /*! Wishful thinking to generate a constant time compare |
Harald Welte | 9709b2e | 2016-04-25 18:47:53 +0200 | [diff] [blame] | 312 | * \param[in] exp Expected data |
| 313 | * \param[in] rel Comparison value |
| 314 | * \param[in] count Number of bytes to compare |
| 315 | * \returns 1 in case \a exp equals \a rel; zero otherwise |
| 316 | * |
| 317 | * Compare count bytes of exp to rel. Return 0 if they are identical, 1 |
| 318 | * otherwise. Do not return a mismatch on the first mismatching byte, |
| 319 | * but always compare all bytes, regardless. The idea is that the amount of |
| 320 | * matching bytes cannot be inferred from the time the comparison took. */ |
| 321 | int osmo_constant_time_cmp(const uint8_t *exp, const uint8_t *rel, const int count) |
| 322 | { |
| 323 | int x = 0, i; |
| 324 | |
| 325 | for (i = 0; i < count; ++i) |
| 326 | x |= exp[i] ^ rel[i]; |
| 327 | |
| 328 | /* if x is zero, all data was identical */ |
| 329 | return x? 1 : 0; |
| 330 | } |
| 331 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 332 | /*! Generic retrieval of 1..8 bytes as big-endian uint64_t |
Harald Welte | 9709b2e | 2016-04-25 18:47:53 +0200 | [diff] [blame] | 333 | * \param[in] data Input data as byte-array |
| 334 | * \param[in] data_len Length of \a data in octets |
| 335 | * \returns uint64_t of \a data interpreted as big-endian |
| 336 | * |
| 337 | * This is like osmo_load64be_ext, except that if data_len is less than |
| 338 | * sizeof(uint64_t), the data is interpreted as the least significant bytes |
| 339 | * (osmo_load64be_ext loads them as the most significant bytes into the |
| 340 | * returned uint64_t). In this way, any integer size up to 64 bits can be |
| 341 | * decoded conveniently by using sizeof(), without the need to call specific |
| 342 | * numbered functions (osmo_load16, 32, ...). */ |
| 343 | uint64_t osmo_decode_big_endian(const uint8_t *data, size_t data_len) |
| 344 | { |
| 345 | uint64_t value = 0; |
| 346 | |
| 347 | while (data_len > 0) { |
| 348 | value = (value << 8) + *data; |
| 349 | data += 1; |
| 350 | data_len -= 1; |
| 351 | } |
| 352 | |
| 353 | return value; |
| 354 | } |
| 355 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 356 | /*! Generic big-endian encoding of big endian number up to 64bit |
Harald Welte | 9709b2e | 2016-04-25 18:47:53 +0200 | [diff] [blame] | 357 | * \param[in] value unsigned integer value to be stored |
| 358 | * \param[in] data_len number of octets |
| 359 | * \returns static buffer containing big-endian stored value |
| 360 | * |
| 361 | * This is like osmo_store64be_ext, except that this returns a static buffer of |
| 362 | * the result (for convenience, but not threadsafe). If data_len is less than |
| 363 | * sizeof(uint64_t), only the least significant bytes of value are encoded. */ |
| 364 | uint8_t *osmo_encode_big_endian(uint64_t value, size_t data_len) |
| 365 | { |
| 366 | static uint8_t buf[sizeof(uint64_t)]; |
| 367 | OSMO_ASSERT(data_len <= ARRAY_SIZE(buf)); |
| 368 | osmo_store64be_ext(value, buf, data_len); |
| 369 | return buf; |
| 370 | } |
Harald Welte | aeecc48 | 2016-11-26 10:41:40 +0100 | [diff] [blame] | 371 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 372 | /*! Copy a C-string into a sized buffer |
Harald Welte | aeecc48 | 2016-11-26 10:41:40 +0100 | [diff] [blame] | 373 | * \param[in] src source string |
| 374 | * \param[out] dst destination string |
Neels Hofmeyr | df83ece | 2017-01-13 13:55:43 +0100 | [diff] [blame] | 375 | * \param[in] siz size of the \a dst buffer |
| 376 | * \returns length of \a src |
Harald Welte | aeecc48 | 2016-11-26 10:41:40 +0100 | [diff] [blame] | 377 | * |
Neels Hofmeyr | df83ece | 2017-01-13 13:55:43 +0100 | [diff] [blame] | 378 | * Copy at most \a siz bytes from \a src to \a dst, ensuring that the result is |
| 379 | * NUL terminated. The NUL character is included in \a siz, i.e. passing the |
| 380 | * actual sizeof(*dst) is correct. |
Harald Welte | aeecc48 | 2016-11-26 10:41:40 +0100 | [diff] [blame] | 381 | */ |
| 382 | size_t osmo_strlcpy(char *dst, const char *src, size_t siz) |
| 383 | { |
Neels Hofmeyr | bcf9f23 | 2017-10-25 04:16:45 +0200 | [diff] [blame] | 384 | size_t ret = src ? strlen(src) : 0; |
Harald Welte | aeecc48 | 2016-11-26 10:41:40 +0100 | [diff] [blame] | 385 | |
| 386 | if (siz) { |
| 387 | size_t len = (ret >= siz) ? siz - 1 : ret; |
Neels Hofmeyr | ebd3cdd | 2017-11-18 23:07:38 +0100 | [diff] [blame] | 388 | if (src) |
| 389 | memcpy(dst, src, len); |
Harald Welte | aeecc48 | 2016-11-26 10:41:40 +0100 | [diff] [blame] | 390 | dst[len] = '\0'; |
| 391 | } |
| 392 | return ret; |
| 393 | } |
Neels Hofmeyr | 0aeda1b | 2017-01-13 14:16:02 +0100 | [diff] [blame] | 394 | |
Neels Hofmeyr | 4335bad | 2017-10-07 04:39:14 +0200 | [diff] [blame] | 395 | /*! Validate that a given string is a hex string within given size limits. |
| 396 | * Note that each hex digit amounts to a nibble, so if checking for a hex |
| 397 | * string to result in N bytes, pass amount of digits as 2*N. |
| 398 | * \param str A nul-terminated string to validate, or NULL. |
| 399 | * \param min_digits least permitted amount of digits. |
| 400 | * \param max_digits most permitted amount of digits. |
| 401 | * \param require_even if true, require an even amount of digits. |
| 402 | * \returns true when the hex_str contains only hexadecimal digits (no |
| 403 | * whitespace) and matches the requested length; also true |
| 404 | * when min_digits <= 0 and str is NULL. |
| 405 | */ |
| 406 | bool osmo_is_hexstr(const char *str, int min_digits, int max_digits, |
| 407 | bool require_even) |
| 408 | { |
| 409 | int len; |
| 410 | /* Use unsigned char * to avoid a compiler warning of |
| 411 | * "error: array subscript has type 'char' [-Werror=char-subscripts]" */ |
| 412 | const unsigned char *pos = (const unsigned char*)str; |
| 413 | if (!pos) |
| 414 | return min_digits < 1; |
| 415 | for (len = 0; *pos && len < max_digits; len++, pos++) |
| 416 | if (!isxdigit(*pos)) |
| 417 | return false; |
| 418 | if (len < min_digits) |
| 419 | return false; |
| 420 | /* With not too many digits, we should have reached *str == nul */ |
| 421 | if (*pos) |
| 422 | return false; |
| 423 | if (require_even && (len & 1)) |
| 424 | return false; |
Harald Welte | febe83c | 2017-10-03 17:41:59 +0800 | [diff] [blame] | 425 | |
| 426 | return true; |
| 427 | } |
| 428 | |
| 429 | /*! Determine if a given identifier is valid, i.e. doesn't contain illegal chars |
| 430 | * \param[in] str String to validate |
Neels Hofmeyr | 937ddea | 2017-12-16 00:46:50 +0100 | [diff] [blame] | 431 | * \param[in] sep_chars Permitted separation characters between identifiers. |
| 432 | * \returns true in case \a str contains only valid identifiers and sep_chars, false otherwise |
Harald Welte | febe83c | 2017-10-03 17:41:59 +0800 | [diff] [blame] | 433 | */ |
Neels Hofmeyr | 937ddea | 2017-12-16 00:46:50 +0100 | [diff] [blame] | 434 | bool osmo_separated_identifiers_valid(const char *str, const char *sep_chars) |
Harald Welte | febe83c | 2017-10-03 17:41:59 +0800 | [diff] [blame] | 435 | { |
| 436 | /* characters that are illegal in names */ |
| 437 | static const char illegal_chars[] = "., {}[]()<>|~\\^`'\"?=;/+*&%$#!"; |
| 438 | unsigned int i; |
Neels Hofmeyr | 937ddea | 2017-12-16 00:46:50 +0100 | [diff] [blame] | 439 | size_t len; |
Harald Welte | febe83c | 2017-10-03 17:41:59 +0800 | [diff] [blame] | 440 | |
| 441 | /* an empty string is not a valid identifier */ |
Neels Hofmeyr | 937ddea | 2017-12-16 00:46:50 +0100 | [diff] [blame] | 442 | if (!str || (len = strlen(str)) == 0) |
Harald Welte | febe83c | 2017-10-03 17:41:59 +0800 | [diff] [blame] | 443 | return false; |
| 444 | |
Neels Hofmeyr | 937ddea | 2017-12-16 00:46:50 +0100 | [diff] [blame] | 445 | for (i = 0; i < len; i++) { |
| 446 | if (sep_chars && strchr(sep_chars, str[i])) |
| 447 | continue; |
Harald Welte | febe83c | 2017-10-03 17:41:59 +0800 | [diff] [blame] | 448 | /* check for 7-bit ASCII */ |
| 449 | if (str[i] & 0x80) |
| 450 | return false; |
Neels Hofmeyr | e5a2bdb | 2017-12-16 04:54:37 +0100 | [diff] [blame] | 451 | if (!isprint((int)str[i])) |
| 452 | return false; |
Harald Welte | febe83c | 2017-10-03 17:41:59 +0800 | [diff] [blame] | 453 | /* check for some explicit reserved control characters */ |
| 454 | if (strchr(illegal_chars, str[i])) |
| 455 | return false; |
| 456 | } |
| 457 | |
Neels Hofmeyr | 4335bad | 2017-10-07 04:39:14 +0200 | [diff] [blame] | 458 | return true; |
| 459 | } |
| 460 | |
Neels Hofmeyr | 937ddea | 2017-12-16 00:46:50 +0100 | [diff] [blame] | 461 | /*! Determine if a given identifier is valid, i.e. doesn't contain illegal chars |
| 462 | * \param[in] str String to validate |
| 463 | * \returns true in case \a str contains valid identifier, false otherwise |
| 464 | */ |
| 465 | bool osmo_identifier_valid(const char *str) |
| 466 | { |
| 467 | return osmo_separated_identifiers_valid(str, NULL); |
| 468 | } |
| 469 | |
Neels Hofmeyr | 9910bbc | 2017-12-16 00:54:52 +0100 | [diff] [blame] | 470 | /*! Return the string with all non-printable characters escaped. |
| 471 | * \param[in] str A string that may contain any characters. |
| 472 | * \param[in] len Pass -1 to print until nul char, or >= 0 to force a length. |
| 473 | * \param[inout] buf string buffer to write escaped characters to. |
| 474 | * \param[in] bufsize size of \a buf. |
| 475 | * \returns buf containing an escaped representation, possibly truncated, or str itself. |
| 476 | */ |
| 477 | const char *osmo_escape_str_buf(const char *str, int in_len, char *buf, size_t bufsize) |
| 478 | { |
| 479 | int in_pos = 0; |
| 480 | int next_unprintable = 0; |
| 481 | int out_pos = 0; |
| 482 | char *out = buf; |
| 483 | /* -1 to leave space for a final \0 */ |
| 484 | int out_len = bufsize-1; |
| 485 | |
| 486 | if (!str) |
| 487 | return "(null)"; |
| 488 | |
| 489 | if (in_len < 0) |
| 490 | in_len = strlen(str); |
| 491 | |
| 492 | while (in_pos < in_len) { |
| 493 | for (next_unprintable = in_pos; |
| 494 | next_unprintable < in_len && isprint((int)str[next_unprintable]) |
| 495 | && str[next_unprintable] != '"' |
| 496 | && str[next_unprintable] != '\\'; |
| 497 | next_unprintable++); |
| 498 | |
| 499 | if (next_unprintable == in_len |
| 500 | && in_pos == 0) |
| 501 | return str; |
| 502 | |
| 503 | while (in_pos < next_unprintable && out_pos < out_len) |
| 504 | out[out_pos++] = str[in_pos++]; |
| 505 | |
| 506 | if (out_pos == out_len || in_pos == in_len) |
| 507 | goto done; |
| 508 | |
| 509 | switch (str[next_unprintable]) { |
| 510 | #define BACKSLASH_CASE(c, repr) \ |
| 511 | case c: \ |
| 512 | if (out_pos > out_len-2) \ |
| 513 | goto done; \ |
| 514 | out[out_pos++] = '\\'; \ |
| 515 | out[out_pos++] = repr; \ |
| 516 | break |
| 517 | |
| 518 | BACKSLASH_CASE('\n', 'n'); |
| 519 | BACKSLASH_CASE('\r', 'r'); |
| 520 | BACKSLASH_CASE('\t', 't'); |
| 521 | BACKSLASH_CASE('\0', '0'); |
| 522 | BACKSLASH_CASE('\a', 'a'); |
| 523 | BACKSLASH_CASE('\b', 'b'); |
| 524 | BACKSLASH_CASE('\v', 'v'); |
| 525 | BACKSLASH_CASE('\f', 'f'); |
| 526 | BACKSLASH_CASE('\\', '\\'); |
| 527 | BACKSLASH_CASE('"', '"'); |
| 528 | #undef BACKSLASH_CASE |
| 529 | |
| 530 | default: |
| 531 | out_pos += snprintf(&out[out_pos], out_len - out_pos, "\\%u", (unsigned char)str[in_pos]); |
| 532 | if (out_pos > out_len) { |
| 533 | out_pos = out_len; |
| 534 | goto done; |
| 535 | } |
| 536 | break; |
| 537 | } |
| 538 | in_pos ++; |
| 539 | } |
| 540 | |
| 541 | done: |
| 542 | out[out_pos] = '\0'; |
| 543 | return out; |
| 544 | } |
| 545 | |
| 546 | /*! Return the string with all non-printable characters escaped. |
| 547 | * Call osmo_escape_str_buf() with a static buffer. |
| 548 | * \param[in] str A string that may contain any characters. |
| 549 | * \param[in] len Pass -1 to print until nul char, or >= 0 to force a length. |
| 550 | * \returns buf containing an escaped representation, possibly truncated, or str itself. |
| 551 | */ |
| 552 | const char *osmo_escape_str(const char *str, int in_len) |
| 553 | { |
| 554 | return osmo_escape_str_buf(str, in_len, namebuf, sizeof(namebuf)); |
| 555 | } |
| 556 | |
Neels Hofmeyr | 0aeda1b | 2017-01-13 14:16:02 +0100 | [diff] [blame] | 557 | /*! @} */ |