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 | 171ef82 | 2019-03-28 10:49:05 +0100 | [diff] [blame] | 44 | static __thread char namebuf[255]; |
Pau Espin Pedrol | a37f58e | 2019-08-01 18:11:41 +0200 | [diff] [blame] | 45 | /* shared by osmo_str_tolower() and osmo_str_toupper() */ |
| 46 | static __thread char capsbuf[128]; |
Harald Welte | 8598f18 | 2011-08-17 14:19:27 +0200 | [diff] [blame] | 47 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 48 | /*! get human-readable string for given value |
Harald Welte | 8598f18 | 2011-08-17 14:19:27 +0200 | [diff] [blame] | 49 | * \param[in] vs Array of value_string tuples |
| 50 | * \param[in] val Value to be converted |
| 51 | * \returns pointer to human-readable string |
Neels Hofmeyr | 8a3c83e | 2016-06-13 13:16:58 +0200 | [diff] [blame] | 52 | * |
| 53 | * If val is found in vs, the array's string entry is returned. Otherwise, an |
| 54 | * "unknown" string containing the actual value is composed in a static buffer |
| 55 | * that is reused across invocations. |
Harald Welte | 8598f18 | 2011-08-17 14:19:27 +0200 | [diff] [blame] | 56 | */ |
Harald Welte | d284cd9 | 2010-03-01 21:58:31 +0100 | [diff] [blame] | 57 | const char *get_value_string(const struct value_string *vs, uint32_t val) |
| 58 | { |
Neels Hofmeyr | 8d6dcd9 | 2016-06-06 18:05:23 +0200 | [diff] [blame] | 59 | const char *str = get_value_string_or_null(vs, val); |
| 60 | if (str) |
| 61 | return str; |
| 62 | |
Pau Espin Pedrol | 4573502 | 2017-06-18 14:05:24 +0200 | [diff] [blame] | 63 | snprintf(namebuf, sizeof(namebuf), "unknown 0x%"PRIx32, val); |
Neels Hofmeyr | 8d6dcd9 | 2016-06-06 18:05:23 +0200 | [diff] [blame] | 64 | namebuf[sizeof(namebuf) - 1] = '\0'; |
| 65 | return namebuf; |
| 66 | } |
| 67 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 68 | /*! get human-readable string or NULL for given value |
Neels Hofmeyr | 8d6dcd9 | 2016-06-06 18:05:23 +0200 | [diff] [blame] | 69 | * \param[in] vs Array of value_string tuples |
| 70 | * \param[in] val Value to be converted |
| 71 | * \returns pointer to human-readable string or NULL if val is not found |
| 72 | */ |
| 73 | const char *get_value_string_or_null(const struct value_string *vs, |
| 74 | uint32_t val) |
| 75 | { |
Harald Welte | d284cd9 | 2010-03-01 21:58:31 +0100 | [diff] [blame] | 76 | int i; |
| 77 | |
Neels Hofmeyr | a0331ed | 2019-02-11 21:24:40 +0100 | [diff] [blame] | 78 | if (!vs) |
| 79 | return NULL; |
| 80 | |
Harald Welte | d284cd9 | 2010-03-01 21:58:31 +0100 | [diff] [blame] | 81 | for (i = 0;; i++) { |
| 82 | if (vs[i].value == 0 && vs[i].str == NULL) |
| 83 | break; |
| 84 | if (vs[i].value == val) |
| 85 | return vs[i].str; |
| 86 | } |
Harald Welte | b59f935 | 2010-03-25 11:37:04 +0800 | [diff] [blame] | 87 | |
Neels Hofmeyr | 8d6dcd9 | 2016-06-06 18:05:23 +0200 | [diff] [blame] | 88 | return NULL; |
Harald Welte | d284cd9 | 2010-03-01 21:58:31 +0100 | [diff] [blame] | 89 | } |
| 90 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 91 | /*! get numeric value for given human-readable string |
Harald Welte | 8598f18 | 2011-08-17 14:19:27 +0200 | [diff] [blame] | 92 | * \param[in] vs Array of value_string tuples |
| 93 | * \param[in] str human-readable string |
| 94 | * \returns numeric value (>0) or negative numer in case of error |
| 95 | */ |
Harald Welte | d284cd9 | 2010-03-01 21:58:31 +0100 | [diff] [blame] | 96 | int get_string_value(const struct value_string *vs, const char *str) |
| 97 | { |
| 98 | int i; |
| 99 | |
| 100 | for (i = 0;; i++) { |
| 101 | if (vs[i].value == 0 && vs[i].str == NULL) |
| 102 | break; |
| 103 | if (!strcasecmp(vs[i].str, str)) |
| 104 | return vs[i].value; |
| 105 | } |
| 106 | return -EINVAL; |
| 107 | } |
Harald Welte | a73e2f9 | 2010-03-04 10:50:32 +0100 | [diff] [blame] | 108 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 109 | /*! Convert BCD-encoded digit into printable character |
Harald Welte | 8598f18 | 2011-08-17 14:19:27 +0200 | [diff] [blame] | 110 | * \param[in] bcd A single BCD-encoded digit |
| 111 | * \returns single printable character |
| 112 | */ |
Pablo Neira Ayuso | 87f7b25 | 2011-05-07 12:43:08 +0200 | [diff] [blame] | 113 | char osmo_bcd2char(uint8_t bcd) |
Harald Welte | a73e2f9 | 2010-03-04 10:50:32 +0100 | [diff] [blame] | 114 | { |
| 115 | if (bcd < 0xa) |
| 116 | return '0' + bcd; |
| 117 | else |
| 118 | return 'A' + (bcd - 0xa); |
| 119 | } |
| 120 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 121 | /*! Convert number in ASCII to BCD value |
Harald Welte | de6e498 | 2012-12-06 21:25:27 +0100 | [diff] [blame] | 122 | * \param[in] c ASCII character |
| 123 | * \returns BCD encoded value of character |
| 124 | */ |
Pablo Neira Ayuso | 87f7b25 | 2011-05-07 12:43:08 +0200 | [diff] [blame] | 125 | uint8_t osmo_char2bcd(char c) |
Harald Welte | a73e2f9 | 2010-03-04 10:50:32 +0100 | [diff] [blame] | 126 | { |
Harald Welte | fa8983d | 2017-10-27 16:52:59 +0200 | [diff] [blame] | 127 | if (c >= '0' && c <= '9') |
| 128 | return c - 0x30; |
| 129 | else if (c >= 'A' && c <= 'F') |
| 130 | return 0xa + (c - 'A'); |
| 131 | else if (c >= 'a' && c <= 'f') |
| 132 | return 0xa + (c - 'a'); |
| 133 | else |
| 134 | return 0; |
Harald Welte | a73e2f9 | 2010-03-04 10:50:32 +0100 | [diff] [blame] | 135 | } |
Harald Welte | 3eba991 | 2010-07-30 10:37:29 +0200 | [diff] [blame] | 136 | |
Neels Hofmeyr | 7079e69 | 2018-12-05 21:02:36 +0100 | [diff] [blame] | 137 | /*! Convert BCD to string. |
| 138 | * The given nibble offsets are interpreted in BCD order, i.e. nibble 0 is bcd[0] & 0xf, nibble 1 is bcd[0] >> 4, nibble |
| 139 | * 3 is bcd[1] & 0xf, etc.. |
| 140 | * \param[out] dst Output string buffer, is always nul terminated when dst_size > 0. |
| 141 | * \param[in] dst_size sizeof() the output string buffer. |
| 142 | * \param[in] bcd Binary coded data buffer. |
| 143 | * \param[in] start_nibble Offset to start from, in nibbles, typically 1 to skip the first nibble. |
Neels Hofmeyr | 48b2de0 | 2018-12-11 02:13:57 +0100 | [diff] [blame] | 144 | * \param[in] end_nibble Offset to stop before, in nibbles, e.g. sizeof(bcd)*2 - (bcd[0] & GSM_MI_ODD? 0:1). |
Neels Hofmeyr | 7079e69 | 2018-12-05 21:02:36 +0100 | [diff] [blame] | 145 | * \param[in] allow_hex If false, return error if there are digits other than 0-9. If true, return those as [A-F]. |
| 146 | * \returns The strlen that would be written if the output buffer is large enough, excluding nul byte (like |
| 147 | * snprintf()), or -EINVAL if allow_hex is false and a digit > 9 is encountered. On -EINVAL, the conversion is |
| 148 | * still completed as if allow_hex were passed as true. Return -ENOMEM if dst is NULL or dst_size is zero. |
| 149 | * If end_nibble <= start_nibble, write an empty string to dst and return 0. |
| 150 | */ |
| 151 | int osmo_bcd2str(char *dst, size_t dst_size, const uint8_t *bcd, int start_nibble, int end_nibble, bool allow_hex) |
| 152 | { |
| 153 | char *dst_end = dst + dst_size - 1; |
| 154 | int nibble_i; |
| 155 | int rc = 0; |
| 156 | |
| 157 | if (!dst || dst_size < 1) |
| 158 | return -ENOMEM; |
| 159 | |
| 160 | for (nibble_i = start_nibble; nibble_i < end_nibble && dst < dst_end; nibble_i++, dst++) { |
| 161 | uint8_t nibble = bcd[nibble_i >> 1]; |
| 162 | if ((nibble_i & 1)) |
| 163 | nibble >>= 4; |
| 164 | nibble &= 0xf; |
| 165 | |
| 166 | if (!allow_hex && nibble > 9) |
| 167 | rc = -EINVAL; |
| 168 | |
| 169 | *dst = osmo_bcd2char(nibble); |
| 170 | } |
| 171 | *dst = '\0'; |
| 172 | |
| 173 | if (rc < 0) |
| 174 | return rc; |
| 175 | return OSMO_MAX(0, end_nibble - start_nibble); |
| 176 | } |
| 177 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 178 | /*! Parse a string containing hexadecimal digits |
Harald Welte | de6e498 | 2012-12-06 21:25:27 +0100 | [diff] [blame] | 179 | * \param[in] str string containing ASCII encoded hexadecimal digits |
| 180 | * \param[out] b output buffer |
| 181 | * \param[in] max_len maximum space in output buffer |
Neels Hofmeyr | 3de7b05 | 2015-09-23 23:16:53 +0200 | [diff] [blame] | 182 | * \returns number of parsed octets, or -1 on error |
Harald Welte | de6e498 | 2012-12-06 21:25:27 +0100 | [diff] [blame] | 183 | */ |
Pablo Neira Ayuso | 87f7b25 | 2011-05-07 12:43:08 +0200 | [diff] [blame] | 184 | int osmo_hexparse(const char *str, uint8_t *b, int max_len) |
Harald Welte | 3eba991 | 2010-07-30 10:37:29 +0200 | [diff] [blame] | 185 | |
| 186 | { |
Neels Hofmeyr | 437ed4a | 2017-02-14 15:54:31 +0100 | [diff] [blame] | 187 | char c; |
| 188 | uint8_t v; |
| 189 | const char *strpos; |
| 190 | unsigned int nibblepos = 0; |
Harald Welte | 3eba991 | 2010-07-30 10:37:29 +0200 | [diff] [blame] | 191 | |
| 192 | memset(b, 0x00, max_len); |
| 193 | |
Neels Hofmeyr | 437ed4a | 2017-02-14 15:54:31 +0100 | [diff] [blame] | 194 | for (strpos = str; (c = *strpos); strpos++) { |
| 195 | /* skip whitespace */ |
| 196 | if (c == ' ' || c == '\t' || c == '\n' || c == '\r') |
| 197 | continue; |
| 198 | |
| 199 | /* If the buffer is too small, error out */ |
| 200 | if (nibblepos >= (max_len << 1)) |
| 201 | return -1; |
| 202 | |
Harald Welte | 3eba991 | 2010-07-30 10:37:29 +0200 | [diff] [blame] | 203 | if (c >= '0' && c <= '9') |
| 204 | v = c - '0'; |
| 205 | else if (c >= 'a' && c <= 'f') |
| 206 | v = 10 + (c - 'a'); |
| 207 | else if (c >= 'A' && c <= 'F') |
| 208 | v = 10 + (c - 'A'); |
| 209 | else |
| 210 | return -1; |
Neels Hofmeyr | 437ed4a | 2017-02-14 15:54:31 +0100 | [diff] [blame] | 211 | |
| 212 | b[nibblepos >> 1] |= v << (nibblepos & 1 ? 0 : 4); |
| 213 | nibblepos ++; |
Harald Welte | 3eba991 | 2010-07-30 10:37:29 +0200 | [diff] [blame] | 214 | } |
| 215 | |
Neels Hofmeyr | 437ed4a | 2017-02-14 15:54:31 +0100 | [diff] [blame] | 216 | /* In case of uneven amount of digits, the last byte is not complete |
| 217 | * and that's an error. */ |
| 218 | if (nibblepos & 1) |
| 219 | return -1; |
| 220 | |
| 221 | return nibblepos >> 1; |
Harald Welte | 3eba991 | 2010-07-30 10:37:29 +0200 | [diff] [blame] | 222 | } |
Harald Welte | 40481e8 | 2010-07-30 11:40:32 +0200 | [diff] [blame] | 223 | |
Harald Welte | 171ef82 | 2019-03-28 10:49:05 +0100 | [diff] [blame] | 224 | static __thread char hexd_buff[4096]; |
Nils O. Selåsdal | 3244702 | 2014-01-02 14:04:43 +0100 | [diff] [blame] | 225 | static const char hex_chars[] = "0123456789abcdef"; |
Harald Welte | 40481e8 | 2010-07-30 11:40:32 +0200 | [diff] [blame] | 226 | |
Neels Hofmeyr | 0423b61 | 2019-01-14 23:32:53 +0100 | [diff] [blame] | 227 | /*! Convert binary sequence to hexadecimal ASCII string. |
| 228 | * \param[out] out_buf Output buffer to write the resulting string to. |
| 229 | * \param[in] out_buf_size sizeof(out_buf). |
| 230 | * \param[in] buf Input buffer, pointer to sequence of bytes. |
| 231 | * \param[in] len Length of input buf in number of bytes. |
| 232 | * \param[in] delim String to separate each byte; NULL or "" for no delim. |
| 233 | * \param[in] delim_after_last If true, end the string in delim (true: "1a:ef:d9:", false: "1a:ef:d9"); |
| 234 | * if out_buf has insufficient space, the string will always end in a delim. |
| 235 | * \returns out_buf, containing a zero-terminated string, or "" (empty string) if out_buf == NULL or out_buf_size < 1. |
| 236 | * |
| 237 | * This function will print a sequence of bytes as hexadecimal numbers, adding one delim between each byte (e.g. for |
| 238 | * delim passed as ":", return a string like "1a:ef:d9"). |
| 239 | * |
| 240 | * The delim_after_last argument exists to be able to exactly show the original osmo_hexdump() behavior, which always |
| 241 | * ends the string with a delimiter. |
| 242 | */ |
| 243 | const char *osmo_hexdump_buf(char *out_buf, size_t out_buf_size, const unsigned char *buf, int len, const char *delim, |
| 244 | bool delim_after_last) |
Harald Welte | 40481e8 | 2010-07-30 11:40:32 +0200 | [diff] [blame] | 245 | { |
| 246 | int i; |
Neels Hofmeyr | 0423b61 | 2019-01-14 23:32:53 +0100 | [diff] [blame] | 247 | char *cur = out_buf; |
| 248 | size_t delim_len; |
Harald Welte | 40481e8 | 2010-07-30 11:40:32 +0200 | [diff] [blame] | 249 | |
Neels Hofmeyr | 0423b61 | 2019-01-14 23:32:53 +0100 | [diff] [blame] | 250 | if (!out_buf || !out_buf_size) |
| 251 | return ""; |
| 252 | |
| 253 | delim = delim ? : ""; |
| 254 | delim_len = strlen(delim); |
| 255 | |
Harald Welte | 40481e8 | 2010-07-30 11:40:32 +0200 | [diff] [blame] | 256 | for (i = 0; i < len; i++) { |
Nils O. Selåsdal | 3244702 | 2014-01-02 14:04:43 +0100 | [diff] [blame] | 257 | const char *delimp = delim; |
Neels Hofmeyr | 0423b61 | 2019-01-14 23:32:53 +0100 | [diff] [blame] | 258 | int len_remain = out_buf_size - (cur - out_buf) - 1; |
| 259 | if (len_remain < (2 + delim_len) |
| 260 | && !(!delim_after_last && i == (len - 1) && len_remain >= 2)) |
Holger Hans Peter Freyther | 128d9e2 | 2011-07-15 16:07:23 +0200 | [diff] [blame] | 261 | break; |
Nils O. Selåsdal | 3244702 | 2014-01-02 14:04:43 +0100 | [diff] [blame] | 262 | |
| 263 | *cur++ = hex_chars[buf[i] >> 4]; |
| 264 | *cur++ = hex_chars[buf[i] & 0xf]; |
| 265 | |
Neels Hofmeyr | 0423b61 | 2019-01-14 23:32:53 +0100 | [diff] [blame] | 266 | if (i == (len - 1) && !delim_after_last) |
| 267 | break; |
| 268 | |
Nils O. Selåsdal | 3244702 | 2014-01-02 14:04:43 +0100 | [diff] [blame] | 269 | while (len_remain > 1 && *delimp) { |
| 270 | *cur++ = *delimp++; |
| 271 | len_remain--; |
| 272 | } |
Harald Welte | 40481e8 | 2010-07-30 11:40:32 +0200 | [diff] [blame] | 273 | } |
Neels Hofmeyr | 0423b61 | 2019-01-14 23:32:53 +0100 | [diff] [blame] | 274 | *cur = '\0'; |
| 275 | return out_buf; |
Harald Welte | 40481e8 | 2010-07-30 11:40:32 +0200 | [diff] [blame] | 276 | } |
Harald Welte | dee47cd | 2010-07-30 11:43:30 +0200 | [diff] [blame] | 277 | |
Harald Welte | 4a62eda | 2019-03-18 18:27:00 +0100 | [diff] [blame] | 278 | /*! Convert a sequence of unpacked bits to ASCII string, in user-supplied buffer. |
| 279 | * \param[out] buf caller-provided output string buffer |
| 280 | * \param[out] buf_len size of buf in bytes |
Harald Welte | 8598f18 | 2011-08-17 14:19:27 +0200 | [diff] [blame] | 281 | * \param[in] bits A sequence of unpacked bits |
| 282 | * \param[in] len Length of bits |
Neels Hofmeyr | dd7b6f9 | 2019-11-20 21:32:29 +0100 | [diff] [blame] | 283 | * \return The output buffer (buf). |
Harald Welte | 8598f18 | 2011-08-17 14:19:27 +0200 | [diff] [blame] | 284 | */ |
Harald Welte | 4a62eda | 2019-03-18 18:27:00 +0100 | [diff] [blame] | 285 | char *osmo_ubit_dump_buf(char *buf, size_t buf_len, const uint8_t *bits, unsigned int len) |
Harald Welte | 3d0ac5e | 2011-02-08 16:55:03 +0100 | [diff] [blame] | 286 | { |
| 287 | int i; |
| 288 | |
Harald Welte | 4a62eda | 2019-03-18 18:27:00 +0100 | [diff] [blame] | 289 | if (len > buf_len-1) |
| 290 | len = buf_len-1; |
| 291 | memset(buf, 0, buf_len); |
Harald Welte | 3d0ac5e | 2011-02-08 16:55:03 +0100 | [diff] [blame] | 292 | |
| 293 | for (i = 0; i < len; i++) { |
| 294 | char outch; |
| 295 | switch (bits[i]) { |
| 296 | case 0: |
| 297 | outch = '0'; |
| 298 | break; |
| 299 | case 0xff: |
| 300 | outch = '?'; |
| 301 | break; |
| 302 | case 1: |
| 303 | outch = '1'; |
| 304 | break; |
| 305 | default: |
| 306 | outch = 'E'; |
| 307 | break; |
| 308 | } |
Harald Welte | 4a62eda | 2019-03-18 18:27:00 +0100 | [diff] [blame] | 309 | buf[i] = outch; |
Harald Welte | 3d0ac5e | 2011-02-08 16:55:03 +0100 | [diff] [blame] | 310 | } |
Harald Welte | 4a62eda | 2019-03-18 18:27:00 +0100 | [diff] [blame] | 311 | buf[buf_len-1] = 0; |
| 312 | return buf; |
| 313 | } |
| 314 | |
| 315 | /*! Convert a sequence of unpacked bits to ASCII string, in static buffer. |
| 316 | * \param[in] bits A sequence of unpacked bits |
| 317 | * \param[in] len Length of bits |
| 318 | * \returns string representation in static buffer. |
| 319 | */ |
| 320 | char *osmo_ubit_dump(const uint8_t *bits, unsigned int len) |
| 321 | { |
| 322 | return osmo_ubit_dump_buf(hexd_buff, sizeof(hexd_buff), bits, len); |
Harald Welte | 3d0ac5e | 2011-02-08 16:55:03 +0100 | [diff] [blame] | 323 | } |
| 324 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 325 | /*! Convert binary sequence to hexadecimal ASCII string |
Harald Welte | 8598f18 | 2011-08-17 14:19:27 +0200 | [diff] [blame] | 326 | * \param[in] buf pointer to sequence of bytes |
| 327 | * \param[in] len length of buf in number of bytes |
| 328 | * \returns pointer to zero-terminated string |
| 329 | * |
| 330 | * This function will print a sequence of bytes as hexadecimal numbers, |
| 331 | * adding one space character between each byte (e.g. "1a ef d9") |
Harald Welte | 096a666 | 2017-10-16 14:33:11 +0200 | [diff] [blame] | 332 | * |
| 333 | * The maximum size of the output buffer is 4096 bytes, i.e. the maximum |
| 334 | * 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] | 335 | */ |
Pablo Neira Ayuso | 87f7b25 | 2011-05-07 12:43:08 +0200 | [diff] [blame] | 336 | char *osmo_hexdump(const unsigned char *buf, int len) |
Harald Welte | dee47cd | 2010-07-30 11:43:30 +0200 | [diff] [blame] | 337 | { |
Neels Hofmeyr | 0423b61 | 2019-01-14 23:32:53 +0100 | [diff] [blame] | 338 | osmo_hexdump_buf(hexd_buff, sizeof(hexd_buff), buf, len, " ", true); |
| 339 | return hexd_buff; |
Harald Welte | dee47cd | 2010-07-30 11:43:30 +0200 | [diff] [blame] | 340 | } |
| 341 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 342 | /*! Convert binary sequence to hexadecimal ASCII string |
Harald Welte | 179f357 | 2019-03-18 18:38:47 +0100 | [diff] [blame] | 343 | * \param[in] ctx talloc context from where to allocate the output string |
| 344 | * \param[in] buf pointer to sequence of bytes |
| 345 | * \param[in] len length of buf in number of bytes |
| 346 | * \returns pointer to zero-terminated string |
| 347 | * |
| 348 | * This function will print a sequence of bytes as hexadecimal numbers, |
| 349 | * adding one space character between each byte (e.g. "1a ef d9") |
| 350 | * |
| 351 | * The maximum size of the output buffer is 4096 bytes, i.e. the maximum |
| 352 | * number of input bytes that can be printed in one call is 1365! |
| 353 | */ |
| 354 | char *osmo_hexdump_c(const void *ctx, const unsigned char *buf, int len) |
| 355 | { |
Vadim Yanitskiy | 4f619c2 | 2019-04-12 21:48:07 +0700 | [diff] [blame] | 356 | size_t hexd_buff_len = len * 3 + 1; |
| 357 | char *hexd_buff = talloc_size(ctx, hexd_buff_len); |
Harald Welte | 179f357 | 2019-03-18 18:38:47 +0100 | [diff] [blame] | 358 | if (!hexd_buff) |
| 359 | return NULL; |
Vadim Yanitskiy | 4f619c2 | 2019-04-12 21:48:07 +0700 | [diff] [blame] | 360 | osmo_hexdump_buf(hexd_buff, hexd_buff_len, buf, len, " ", true); |
Harald Welte | 179f357 | 2019-03-18 18:38:47 +0100 | [diff] [blame] | 361 | return hexd_buff; |
| 362 | } |
| 363 | |
| 364 | /*! Convert binary sequence to hexadecimal ASCII string |
Harald Welte | 8598f18 | 2011-08-17 14:19:27 +0200 | [diff] [blame] | 365 | * \param[in] buf pointer to sequence of bytes |
| 366 | * \param[in] len length of buf in number of bytes |
| 367 | * \returns pointer to zero-terminated string |
| 368 | * |
| 369 | * This function will print a sequence of bytes as hexadecimal numbers, |
| 370 | * without any space character between each byte (e.g. "1aefd9") |
Harald Welte | 096a666 | 2017-10-16 14:33:11 +0200 | [diff] [blame] | 371 | * |
| 372 | * The maximum size of the output buffer is 4096 bytes, i.e. the maximum |
| 373 | * 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] | 374 | */ |
Sylvain Munaut | ff23d24 | 2011-11-10 23:03:18 +0100 | [diff] [blame] | 375 | char *osmo_hexdump_nospc(const unsigned char *buf, int len) |
Harald Welte | dee47cd | 2010-07-30 11:43:30 +0200 | [diff] [blame] | 376 | { |
Neels Hofmeyr | 0423b61 | 2019-01-14 23:32:53 +0100 | [diff] [blame] | 377 | osmo_hexdump_buf(hexd_buff, sizeof(hexd_buff), buf, len, "", true); |
| 378 | return hexd_buff; |
Harald Welte | dee47cd | 2010-07-30 11:43:30 +0200 | [diff] [blame] | 379 | } |
Harald Welte | 2822296 | 2011-02-18 20:37:04 +0100 | [diff] [blame] | 380 | |
Harald Welte | 179f357 | 2019-03-18 18:38:47 +0100 | [diff] [blame] | 381 | /*! Convert binary sequence to hexadecimal ASCII string |
| 382 | * \param[in] ctx talloc context from where to allocate the output string |
| 383 | * \param[in] buf pointer to sequence of bytes |
| 384 | * \param[in] len length of buf in number of bytes |
| 385 | * \returns pointer to zero-terminated string |
| 386 | * |
| 387 | * This function will print a sequence of bytes as hexadecimal numbers, |
| 388 | * without any space character between each byte (e.g. "1aefd9") |
| 389 | * |
| 390 | * The maximum size of the output buffer is 4096 bytes, i.e. the maximum |
| 391 | * number of input bytes that can be printed in one call is 2048! |
| 392 | */ |
| 393 | char *osmo_hexdump_nospc_c(const void *ctx, const unsigned char *buf, int len) |
| 394 | { |
Vadim Yanitskiy | 4f619c2 | 2019-04-12 21:48:07 +0700 | [diff] [blame] | 395 | size_t hexd_buff_len = len * 2 + 1; |
| 396 | char *hexd_buff = talloc_size(ctx, hexd_buff_len); |
Harald Welte | 179f357 | 2019-03-18 18:38:47 +0100 | [diff] [blame] | 397 | if (!hexd_buff) |
| 398 | return NULL; |
Vadim Yanitskiy | 4f619c2 | 2019-04-12 21:48:07 +0700 | [diff] [blame] | 399 | osmo_hexdump_buf(hexd_buff, hexd_buff_len, buf, len, "", true); |
Harald Welte | 179f357 | 2019-03-18 18:38:47 +0100 | [diff] [blame] | 400 | return hexd_buff; |
| 401 | } |
| 402 | |
| 403 | |
Holger Hans Peter Freyther | 9a1a5a1 | 2015-04-11 19:26:55 +0200 | [diff] [blame] | 404 | /* Compat with previous typo to preserve abi */ |
Sylvain Munaut | e55ae3a | 2011-11-11 23:06:55 +0100 | [diff] [blame] | 405 | 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] | 406 | #if defined(__MACH__) && defined(__APPLE__) |
| 407 | ; |
| 408 | #else |
Sylvain Munaut | 17af41d | 2011-11-19 22:30:39 +0100 | [diff] [blame] | 409 | __attribute__((weak, alias("osmo_hexdump_nospc"))); |
Holger Hans Peter Freyther | 9a1a5a1 | 2015-04-11 19:26:55 +0200 | [diff] [blame] | 410 | #endif |
Sylvain Munaut | e55ae3a | 2011-11-11 23:06:55 +0100 | [diff] [blame] | 411 | |
Harald Welte | 2822296 | 2011-02-18 20:37:04 +0100 | [diff] [blame] | 412 | #include "../config.h" |
| 413 | #ifdef HAVE_CTYPE_H |
| 414 | #include <ctype.h> |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 415 | /*! Convert an entire string to lower case |
Harald Welte | 8598f18 | 2011-08-17 14:19:27 +0200 | [diff] [blame] | 416 | * \param[out] out output string, caller-allocated |
| 417 | * \param[in] in input string |
| 418 | */ |
Harald Welte | 2822296 | 2011-02-18 20:37:04 +0100 | [diff] [blame] | 419 | void osmo_str2lower(char *out, const char *in) |
| 420 | { |
| 421 | unsigned int i; |
| 422 | |
| 423 | for (i = 0; i < strlen(in); i++) |
Pau Espin Pedrol | 399a6f0 | 2017-06-18 14:07:37 +0200 | [diff] [blame] | 424 | out[i] = tolower((const unsigned char)in[i]); |
Harald Welte | 2822296 | 2011-02-18 20:37:04 +0100 | [diff] [blame] | 425 | out[strlen(in)] = '\0'; |
| 426 | } |
| 427 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 428 | /*! Convert an entire string to upper case |
Harald Welte | 8598f18 | 2011-08-17 14:19:27 +0200 | [diff] [blame] | 429 | * \param[out] out output string, caller-allocated |
| 430 | * \param[in] in input string |
| 431 | */ |
Harald Welte | 2822296 | 2011-02-18 20:37:04 +0100 | [diff] [blame] | 432 | void osmo_str2upper(char *out, const char *in) |
| 433 | { |
| 434 | unsigned int i; |
| 435 | |
| 436 | for (i = 0; i < strlen(in); i++) |
Pau Espin Pedrol | 399a6f0 | 2017-06-18 14:07:37 +0200 | [diff] [blame] | 437 | out[i] = toupper((const unsigned char)in[i]); |
Harald Welte | 2822296 | 2011-02-18 20:37:04 +0100 | [diff] [blame] | 438 | out[strlen(in)] = '\0'; |
| 439 | } |
| 440 | #endif /* HAVE_CTYPE_H */ |
Harald Welte | 8598f18 | 2011-08-17 14:19:27 +0200 | [diff] [blame] | 441 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 442 | /*! Wishful thinking to generate a constant time compare |
Harald Welte | 9709b2e | 2016-04-25 18:47:53 +0200 | [diff] [blame] | 443 | * \param[in] exp Expected data |
| 444 | * \param[in] rel Comparison value |
| 445 | * \param[in] count Number of bytes to compare |
| 446 | * \returns 1 in case \a exp equals \a rel; zero otherwise |
| 447 | * |
| 448 | * Compare count bytes of exp to rel. Return 0 if they are identical, 1 |
| 449 | * otherwise. Do not return a mismatch on the first mismatching byte, |
| 450 | * but always compare all bytes, regardless. The idea is that the amount of |
| 451 | * matching bytes cannot be inferred from the time the comparison took. */ |
| 452 | int osmo_constant_time_cmp(const uint8_t *exp, const uint8_t *rel, const int count) |
| 453 | { |
| 454 | int x = 0, i; |
| 455 | |
| 456 | for (i = 0; i < count; ++i) |
| 457 | x |= exp[i] ^ rel[i]; |
| 458 | |
| 459 | /* if x is zero, all data was identical */ |
| 460 | return x? 1 : 0; |
| 461 | } |
| 462 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 463 | /*! Generic retrieval of 1..8 bytes as big-endian uint64_t |
Harald Welte | 9709b2e | 2016-04-25 18:47:53 +0200 | [diff] [blame] | 464 | * \param[in] data Input data as byte-array |
| 465 | * \param[in] data_len Length of \a data in octets |
| 466 | * \returns uint64_t of \a data interpreted as big-endian |
| 467 | * |
| 468 | * This is like osmo_load64be_ext, except that if data_len is less than |
| 469 | * sizeof(uint64_t), the data is interpreted as the least significant bytes |
| 470 | * (osmo_load64be_ext loads them as the most significant bytes into the |
| 471 | * returned uint64_t). In this way, any integer size up to 64 bits can be |
| 472 | * decoded conveniently by using sizeof(), without the need to call specific |
| 473 | * numbered functions (osmo_load16, 32, ...). */ |
| 474 | uint64_t osmo_decode_big_endian(const uint8_t *data, size_t data_len) |
| 475 | { |
| 476 | uint64_t value = 0; |
| 477 | |
| 478 | while (data_len > 0) { |
| 479 | value = (value << 8) + *data; |
| 480 | data += 1; |
| 481 | data_len -= 1; |
| 482 | } |
| 483 | |
| 484 | return value; |
| 485 | } |
| 486 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 487 | /*! Generic big-endian encoding of big endian number up to 64bit |
Harald Welte | 9709b2e | 2016-04-25 18:47:53 +0200 | [diff] [blame] | 488 | * \param[in] value unsigned integer value to be stored |
| 489 | * \param[in] data_len number of octets |
| 490 | * \returns static buffer containing big-endian stored value |
| 491 | * |
| 492 | * This is like osmo_store64be_ext, except that this returns a static buffer of |
| 493 | * the result (for convenience, but not threadsafe). If data_len is less than |
| 494 | * sizeof(uint64_t), only the least significant bytes of value are encoded. */ |
| 495 | uint8_t *osmo_encode_big_endian(uint64_t value, size_t data_len) |
| 496 | { |
Harald Welte | 171ef82 | 2019-03-28 10:49:05 +0100 | [diff] [blame] | 497 | static __thread uint8_t buf[sizeof(uint64_t)]; |
Harald Welte | 9709b2e | 2016-04-25 18:47:53 +0200 | [diff] [blame] | 498 | OSMO_ASSERT(data_len <= ARRAY_SIZE(buf)); |
| 499 | osmo_store64be_ext(value, buf, data_len); |
| 500 | return buf; |
| 501 | } |
Harald Welte | aeecc48 | 2016-11-26 10:41:40 +0100 | [diff] [blame] | 502 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 503 | /*! Copy a C-string into a sized buffer |
Harald Welte | aeecc48 | 2016-11-26 10:41:40 +0100 | [diff] [blame] | 504 | * \param[in] src source string |
| 505 | * \param[out] dst destination string |
Neels Hofmeyr | df83ece | 2017-01-13 13:55:43 +0100 | [diff] [blame] | 506 | * \param[in] siz size of the \a dst buffer |
| 507 | * \returns length of \a src |
Harald Welte | aeecc48 | 2016-11-26 10:41:40 +0100 | [diff] [blame] | 508 | * |
Neels Hofmeyr | df83ece | 2017-01-13 13:55:43 +0100 | [diff] [blame] | 509 | * Copy at most \a siz bytes from \a src to \a dst, ensuring that the result is |
| 510 | * NUL terminated. The NUL character is included in \a siz, i.e. passing the |
| 511 | * actual sizeof(*dst) is correct. |
Harald Welte | aeecc48 | 2016-11-26 10:41:40 +0100 | [diff] [blame] | 512 | */ |
| 513 | size_t osmo_strlcpy(char *dst, const char *src, size_t siz) |
| 514 | { |
Neels Hofmeyr | bcf9f23 | 2017-10-25 04:16:45 +0200 | [diff] [blame] | 515 | size_t ret = src ? strlen(src) : 0; |
Harald Welte | aeecc48 | 2016-11-26 10:41:40 +0100 | [diff] [blame] | 516 | |
| 517 | if (siz) { |
| 518 | size_t len = (ret >= siz) ? siz - 1 : ret; |
Neels Hofmeyr | ebd3cdd | 2017-11-18 23:07:38 +0100 | [diff] [blame] | 519 | if (src) |
| 520 | memcpy(dst, src, len); |
Harald Welte | aeecc48 | 2016-11-26 10:41:40 +0100 | [diff] [blame] | 521 | dst[len] = '\0'; |
| 522 | } |
| 523 | return ret; |
| 524 | } |
Neels Hofmeyr | 0aeda1b | 2017-01-13 14:16:02 +0100 | [diff] [blame] | 525 | |
Neels Hofmeyr | 4335bad | 2017-10-07 04:39:14 +0200 | [diff] [blame] | 526 | /*! Validate that a given string is a hex string within given size limits. |
| 527 | * Note that each hex digit amounts to a nibble, so if checking for a hex |
| 528 | * string to result in N bytes, pass amount of digits as 2*N. |
| 529 | * \param str A nul-terminated string to validate, or NULL. |
| 530 | * \param min_digits least permitted amount of digits. |
| 531 | * \param max_digits most permitted amount of digits. |
| 532 | * \param require_even if true, require an even amount of digits. |
| 533 | * \returns true when the hex_str contains only hexadecimal digits (no |
| 534 | * whitespace) and matches the requested length; also true |
| 535 | * when min_digits <= 0 and str is NULL. |
| 536 | */ |
| 537 | bool osmo_is_hexstr(const char *str, int min_digits, int max_digits, |
| 538 | bool require_even) |
| 539 | { |
| 540 | int len; |
| 541 | /* Use unsigned char * to avoid a compiler warning of |
| 542 | * "error: array subscript has type 'char' [-Werror=char-subscripts]" */ |
| 543 | const unsigned char *pos = (const unsigned char*)str; |
| 544 | if (!pos) |
| 545 | return min_digits < 1; |
| 546 | for (len = 0; *pos && len < max_digits; len++, pos++) |
| 547 | if (!isxdigit(*pos)) |
| 548 | return false; |
| 549 | if (len < min_digits) |
| 550 | return false; |
| 551 | /* With not too many digits, we should have reached *str == nul */ |
| 552 | if (*pos) |
| 553 | return false; |
| 554 | if (require_even && (len & 1)) |
| 555 | return false; |
Harald Welte | febe83c | 2017-10-03 17:41:59 +0800 | [diff] [blame] | 556 | |
| 557 | return true; |
| 558 | } |
| 559 | |
Neels Hofmeyr | b480b74 | 2019-04-11 06:58:44 +0200 | [diff] [blame] | 560 | static const char osmo_identifier_illegal_chars[] = "., {}[]()<>|~\\^`'\"?=;/+*&%$#!"; |
| 561 | |
Harald Welte | febe83c | 2017-10-03 17:41:59 +0800 | [diff] [blame] | 562 | /*! Determine if a given identifier is valid, i.e. doesn't contain illegal chars |
| 563 | * \param[in] str String to validate |
Neels Hofmeyr | 937ddea | 2017-12-16 00:46:50 +0100 | [diff] [blame] | 564 | * \param[in] sep_chars Permitted separation characters between identifiers. |
| 565 | * \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] | 566 | */ |
Neels Hofmeyr | 937ddea | 2017-12-16 00:46:50 +0100 | [diff] [blame] | 567 | bool osmo_separated_identifiers_valid(const char *str, const char *sep_chars) |
Harald Welte | febe83c | 2017-10-03 17:41:59 +0800 | [diff] [blame] | 568 | { |
| 569 | /* characters that are illegal in names */ |
Harald Welte | febe83c | 2017-10-03 17:41:59 +0800 | [diff] [blame] | 570 | unsigned int i; |
Neels Hofmeyr | 937ddea | 2017-12-16 00:46:50 +0100 | [diff] [blame] | 571 | size_t len; |
Harald Welte | febe83c | 2017-10-03 17:41:59 +0800 | [diff] [blame] | 572 | |
| 573 | /* an empty string is not a valid identifier */ |
Neels Hofmeyr | 937ddea | 2017-12-16 00:46:50 +0100 | [diff] [blame] | 574 | if (!str || (len = strlen(str)) == 0) |
Harald Welte | febe83c | 2017-10-03 17:41:59 +0800 | [diff] [blame] | 575 | return false; |
| 576 | |
Neels Hofmeyr | 937ddea | 2017-12-16 00:46:50 +0100 | [diff] [blame] | 577 | for (i = 0; i < len; i++) { |
| 578 | if (sep_chars && strchr(sep_chars, str[i])) |
| 579 | continue; |
Harald Welte | febe83c | 2017-10-03 17:41:59 +0800 | [diff] [blame] | 580 | /* check for 7-bit ASCII */ |
| 581 | if (str[i] & 0x80) |
| 582 | return false; |
Neels Hofmeyr | e5a2bdb | 2017-12-16 04:54:37 +0100 | [diff] [blame] | 583 | if (!isprint((int)str[i])) |
| 584 | return false; |
Harald Welte | febe83c | 2017-10-03 17:41:59 +0800 | [diff] [blame] | 585 | /* check for some explicit reserved control characters */ |
Neels Hofmeyr | b480b74 | 2019-04-11 06:58:44 +0200 | [diff] [blame] | 586 | if (strchr(osmo_identifier_illegal_chars, str[i])) |
Harald Welte | febe83c | 2017-10-03 17:41:59 +0800 | [diff] [blame] | 587 | return false; |
| 588 | } |
| 589 | |
Neels Hofmeyr | 4335bad | 2017-10-07 04:39:14 +0200 | [diff] [blame] | 590 | return true; |
| 591 | } |
| 592 | |
Neels Hofmeyr | 937ddea | 2017-12-16 00:46:50 +0100 | [diff] [blame] | 593 | /*! Determine if a given identifier is valid, i.e. doesn't contain illegal chars |
| 594 | * \param[in] str String to validate |
| 595 | * \returns true in case \a str contains valid identifier, false otherwise |
| 596 | */ |
| 597 | bool osmo_identifier_valid(const char *str) |
| 598 | { |
| 599 | return osmo_separated_identifiers_valid(str, NULL); |
| 600 | } |
| 601 | |
Neels Hofmeyr | b480b74 | 2019-04-11 06:58:44 +0200 | [diff] [blame] | 602 | /*! Replace characters in the given string buffer so that it is guaranteed to pass osmo_separated_identifiers_valid(). |
| 603 | * To guarantee passing osmo_separated_identifiers_valid(), replace_with must not itself be an illegal character. If in |
| 604 | * doubt, use '-'. |
| 605 | * \param[inout] str Identifier to sanitize, must be nul terminated and in a writable buffer. |
| 606 | * \param[in] sep_chars Additional characters that are allowed besides osmo_identifier_illegal_chars. |
| 607 | * \param[in] replace_with Replace any illegal characters with this character. |
| 608 | */ |
| 609 | void osmo_identifier_sanitize_buf(char *str, const char *sep_chars, char replace_with) |
| 610 | { |
| 611 | char *pos; |
| 612 | if (!str) |
| 613 | return; |
| 614 | for (pos = str; *pos; pos++) { |
| 615 | if (strchr(osmo_identifier_illegal_chars, *pos) |
| 616 | || (sep_chars && strchr(sep_chars, *pos))) |
| 617 | *pos = replace_with; |
| 618 | } |
| 619 | } |
| 620 | |
Neels Hofmeyr | ecef7ec | 2019-03-05 16:42:50 +0100 | [diff] [blame] | 621 | /*! Like osmo_escape_str_buf2, but with unusual ordering of arguments, and may sometimes return string constants instead |
| 622 | * of writing to buf for error cases or empty input. |
| 623 | * Most *_buf() functions have the buffer and size as first arguments, here the arguments are last. |
| 624 | * In particular, this function signature doesn't work with OSMO_STRBUF_APPEND_NOLEN(). |
Neels Hofmeyr | 9910bbc | 2017-12-16 00:54:52 +0100 | [diff] [blame] | 625 | * \param[in] str A string that may contain any characters. |
| 626 | * \param[in] len Pass -1 to print until nul char, or >= 0 to force a length. |
| 627 | * \param[inout] buf string buffer to write escaped characters to. |
| 628 | * \param[in] bufsize size of \a buf. |
Neels Hofmeyr | ecef7ec | 2019-03-05 16:42:50 +0100 | [diff] [blame] | 629 | * \returns buf containing an escaped representation, possibly truncated, |
| 630 | * or "(null)" if str == NULL, or "(error)" in case of errors. |
Neels Hofmeyr | 9910bbc | 2017-12-16 00:54:52 +0100 | [diff] [blame] | 631 | */ |
Neels Hofmeyr | ecef7ec | 2019-03-05 16:42:50 +0100 | [diff] [blame] | 632 | const char *osmo_escape_str_buf(const char *str, int in_len, char *buf, size_t bufsize) |
Neels Hofmeyr | 9910bbc | 2017-12-16 00:54:52 +0100 | [diff] [blame] | 633 | { |
Neels Hofmeyr | 9910bbc | 2017-12-16 00:54:52 +0100 | [diff] [blame] | 634 | if (!str) |
| 635 | return "(null)"; |
Neels Hofmeyr | ecef7ec | 2019-03-05 16:42:50 +0100 | [diff] [blame] | 636 | if (!buf || !bufsize) |
| 637 | return "(error)"; |
| 638 | return osmo_escape_str_buf2(buf, bufsize, str, in_len); |
| 639 | } |
| 640 | |
| 641 | /*! Copy N characters to a buffer with a function signature useful for OSMO_STRBUF_APPEND(). |
| 642 | * Similarly to snprintf(), the result is always nul terminated (except if buf is NULL or bufsize is 0). |
| 643 | * \param[out] buf Target buffer. |
| 644 | * \param[in] bufsize sizeof(buf). |
| 645 | * \param[in] str String to copy. |
| 646 | * \param[in] n Maximum number of non-nul characters to copy. |
| 647 | * \return Number of characters that would be written if bufsize were large enough excluding '\0' (like snprintf()). |
| 648 | */ |
| 649 | int osmo_print_n(char *buf, size_t bufsize, const char *str, size_t n) |
| 650 | { |
| 651 | size_t write_n; |
| 652 | |
| 653 | if (!str) |
| 654 | str = ""; |
| 655 | |
| 656 | n = strnlen(str, n); |
| 657 | |
| 658 | if (!buf || !bufsize) |
| 659 | return n; |
| 660 | write_n = n; |
| 661 | if (write_n >= bufsize) |
| 662 | write_n = bufsize - 1; |
| 663 | if (write_n) |
| 664 | strncpy(buf, str, write_n); |
| 665 | buf[write_n] = '\0'; |
| 666 | |
| 667 | return n; |
| 668 | } |
| 669 | |
| 670 | /*! Return the string with all non-printable characters escaped. |
| 671 | * \param[out] buf string buffer to write escaped characters to. |
| 672 | * \param[in] bufsize sizeof(buf). |
| 673 | * \param[in] str A string that may contain any characters. |
| 674 | * \param[in] in_len Pass -1 to print until nul char, or >= 0 to force a length (also past nul chars). |
Neels Hofmeyr | dd7b6f9 | 2019-11-20 21:32:29 +0100 | [diff] [blame] | 675 | * \return The output buffer (buf). |
Neels Hofmeyr | ecef7ec | 2019-03-05 16:42:50 +0100 | [diff] [blame] | 676 | */ |
| 677 | char *osmo_escape_str_buf2(char *buf, size_t bufsize, const char *str, int in_len) |
| 678 | { |
| 679 | struct osmo_strbuf sb = { .buf = buf, .len = bufsize }; |
| 680 | int in_pos = 0; |
| 681 | int next_unprintable = 0; |
| 682 | |
| 683 | if (!str) |
| 684 | in_len = 0; |
Neels Hofmeyr | 9910bbc | 2017-12-16 00:54:52 +0100 | [diff] [blame] | 685 | |
| 686 | if (in_len < 0) |
| 687 | in_len = strlen(str); |
| 688 | |
Neels Hofmeyr | ecef7ec | 2019-03-05 16:42:50 +0100 | [diff] [blame] | 689 | /* Make sure of '\0' termination */ |
| 690 | if (!in_len) |
| 691 | OSMO_STRBUF_PRINTF(sb, "%s", ""); |
| 692 | |
Neels Hofmeyr | 9910bbc | 2017-12-16 00:54:52 +0100 | [diff] [blame] | 693 | while (in_pos < in_len) { |
| 694 | for (next_unprintable = in_pos; |
| 695 | next_unprintable < in_len && isprint((int)str[next_unprintable]) |
| 696 | && str[next_unprintable] != '"' |
| 697 | && str[next_unprintable] != '\\'; |
| 698 | next_unprintable++); |
| 699 | |
Neels Hofmeyr | ecef7ec | 2019-03-05 16:42:50 +0100 | [diff] [blame] | 700 | OSMO_STRBUF_APPEND(sb, osmo_print_n, &str[in_pos], next_unprintable - in_pos); |
| 701 | in_pos = next_unprintable; |
Neels Hofmeyr | 9910bbc | 2017-12-16 00:54:52 +0100 | [diff] [blame] | 702 | |
Neels Hofmeyr | ecef7ec | 2019-03-05 16:42:50 +0100 | [diff] [blame] | 703 | if (in_pos == in_len) |
Neels Hofmeyr | 9910bbc | 2017-12-16 00:54:52 +0100 | [diff] [blame] | 704 | goto done; |
| 705 | |
| 706 | switch (str[next_unprintable]) { |
| 707 | #define BACKSLASH_CASE(c, repr) \ |
| 708 | case c: \ |
Neels Hofmeyr | ecef7ec | 2019-03-05 16:42:50 +0100 | [diff] [blame] | 709 | OSMO_STRBUF_PRINTF(sb, "\\%c", repr); \ |
Neels Hofmeyr | 9910bbc | 2017-12-16 00:54:52 +0100 | [diff] [blame] | 710 | break |
| 711 | |
| 712 | BACKSLASH_CASE('\n', 'n'); |
| 713 | BACKSLASH_CASE('\r', 'r'); |
| 714 | BACKSLASH_CASE('\t', 't'); |
| 715 | BACKSLASH_CASE('\0', '0'); |
| 716 | BACKSLASH_CASE('\a', 'a'); |
| 717 | BACKSLASH_CASE('\b', 'b'); |
| 718 | BACKSLASH_CASE('\v', 'v'); |
| 719 | BACKSLASH_CASE('\f', 'f'); |
| 720 | BACKSLASH_CASE('\\', '\\'); |
| 721 | BACKSLASH_CASE('"', '"'); |
| 722 | #undef BACKSLASH_CASE |
| 723 | |
| 724 | default: |
Neels Hofmeyr | ecef7ec | 2019-03-05 16:42:50 +0100 | [diff] [blame] | 725 | OSMO_STRBUF_PRINTF(sb, "\\%u", (unsigned char)str[in_pos]); |
Neels Hofmeyr | 9910bbc | 2017-12-16 00:54:52 +0100 | [diff] [blame] | 726 | break; |
| 727 | } |
| 728 | in_pos ++; |
| 729 | } |
| 730 | |
| 731 | done: |
Neels Hofmeyr | ecef7ec | 2019-03-05 16:42:50 +0100 | [diff] [blame] | 732 | return buf; |
Neels Hofmeyr | 9910bbc | 2017-12-16 00:54:52 +0100 | [diff] [blame] | 733 | } |
| 734 | |
| 735 | /*! Return the string with all non-printable characters escaped. |
| 736 | * Call osmo_escape_str_buf() with a static buffer. |
| 737 | * \param[in] str A string that may contain any characters. |
| 738 | * \param[in] len Pass -1 to print until nul char, or >= 0 to force a length. |
| 739 | * \returns buf containing an escaped representation, possibly truncated, or str itself. |
| 740 | */ |
| 741 | const char *osmo_escape_str(const char *str, int in_len) |
| 742 | { |
| 743 | return osmo_escape_str_buf(str, in_len, namebuf, sizeof(namebuf)); |
| 744 | } |
| 745 | |
Harald Welte | 179f357 | 2019-03-18 18:38:47 +0100 | [diff] [blame] | 746 | /*! Return the string with all non-printable characters escaped, in dynamically-allocated buffer. |
| 747 | * \param[in] str A string that may contain any characters. |
| 748 | * \param[in] len Pass -1 to print until nul char, or >= 0 to force a length. |
| 749 | * \returns dynamically-allocated output buffer, containing an escaped representation |
| 750 | */ |
| 751 | char *osmo_escape_str_c(const void *ctx, const char *str, int in_len) |
| 752 | { |
| 753 | char *buf = talloc_size(ctx, in_len+1); |
| 754 | if (!buf) |
| 755 | return NULL; |
Neels Hofmeyr | ecef7ec | 2019-03-05 16:42:50 +0100 | [diff] [blame] | 756 | return osmo_escape_str_buf2(buf, in_len+1, str, in_len); |
Harald Welte | 179f357 | 2019-03-18 18:38:47 +0100 | [diff] [blame] | 757 | } |
| 758 | |
Neels Hofmeyr | ecef7ec | 2019-03-05 16:42:50 +0100 | [diff] [blame] | 759 | /*! Like osmo_escape_str_buf2(), but returns double-quotes around a string, or "NULL" for a NULL string. |
Neels Hofmeyr | 04eb56f | 2018-04-09 00:41:28 +0200 | [diff] [blame] | 760 | * This allows passing any char* value and get its C representation as string. |
Neels Hofmeyr | ecef7ec | 2019-03-05 16:42:50 +0100 | [diff] [blame] | 761 | * The function signature is suitable for OSMO_STRBUF_APPEND_NOLEN(). |
| 762 | * \param[out] buf string buffer to write escaped characters to. |
| 763 | * \param[in] bufsize sizeof(buf). |
| 764 | * \param[in] str A string that may contain any characters. |
| 765 | * \param[in] in_len Pass -1 to print until nul char, or >= 0 to force a length. |
Neels Hofmeyr | dd7b6f9 | 2019-11-20 21:32:29 +0100 | [diff] [blame] | 766 | * \return The output buffer (buf). |
Neels Hofmeyr | ecef7ec | 2019-03-05 16:42:50 +0100 | [diff] [blame] | 767 | */ |
| 768 | char *osmo_quote_str_buf2(char *buf, size_t bufsize, const char *str, int in_len) |
| 769 | { |
| 770 | struct osmo_strbuf sb = { .buf = buf, .len = bufsize }; |
| 771 | if (!str) |
| 772 | OSMO_STRBUF_PRINTF(sb, "NULL"); |
| 773 | else { |
| 774 | OSMO_STRBUF_PRINTF(sb, "\""); |
| 775 | OSMO_STRBUF_APPEND_NOLEN(sb, osmo_escape_str_buf2, str, in_len); |
| 776 | OSMO_STRBUF_PRINTF(sb, "\""); |
| 777 | } |
| 778 | return buf; |
| 779 | } |
| 780 | |
| 781 | /*! Like osmo_quote_str_buf2, but with unusual ordering of arguments, and may sometimes return string constants instead |
| 782 | * of writing to buf for error cases or empty input. |
| 783 | * Most *_buf() functions have the buffer and size as first arguments, here the arguments are last. |
| 784 | * In particular, this function signature doesn't work with OSMO_STRBUF_APPEND_NOLEN(). |
Neels Hofmeyr | 04eb56f | 2018-04-09 00:41:28 +0200 | [diff] [blame] | 785 | * \param[in] str A string that may contain any characters. |
Neels Hofmeyr | 03e7553 | 2018-09-07 03:12:05 +0200 | [diff] [blame] | 786 | * \param[in] in_len Pass -1 to print until nul char, or >= 0 to force a length. |
| 787 | * \returns buf containing a quoted and escaped representation, possibly truncated. |
Neels Hofmeyr | 04eb56f | 2018-04-09 00:41:28 +0200 | [diff] [blame] | 788 | */ |
Neels Hofmeyr | ecef7ec | 2019-03-05 16:42:50 +0100 | [diff] [blame] | 789 | const char *osmo_quote_str_buf(const char *str, int in_len, char *buf, size_t bufsize) |
Neels Hofmeyr | 04eb56f | 2018-04-09 00:41:28 +0200 | [diff] [blame] | 790 | { |
Neels Hofmeyr | 04eb56f | 2018-04-09 00:41:28 +0200 | [diff] [blame] | 791 | if (!str) |
| 792 | return "NULL"; |
Neels Hofmeyr | ecef7ec | 2019-03-05 16:42:50 +0100 | [diff] [blame] | 793 | if (!buf || !bufsize) |
| 794 | return "(error)"; |
| 795 | osmo_quote_str_buf2(buf, bufsize, str, in_len); |
Neels Hofmeyr | 04eb56f | 2018-04-09 00:41:28 +0200 | [diff] [blame] | 796 | return buf; |
| 797 | } |
| 798 | |
Neels Hofmeyr | 03e7553 | 2018-09-07 03:12:05 +0200 | [diff] [blame] | 799 | /*! Like osmo_quote_str_buf() but returns the result in a static buffer. |
| 800 | * The static buffer is shared with get_value_string() and osmo_escape_str(). |
| 801 | * \param[in] str A string that may contain any characters. |
| 802 | * \param[in] in_len Pass -1 to print until nul char, or >= 0 to force a length. |
| 803 | * \returns static buffer containing a quoted and escaped representation, possibly truncated. |
| 804 | */ |
Neels Hofmeyr | 04eb56f | 2018-04-09 00:41:28 +0200 | [diff] [blame] | 805 | const char *osmo_quote_str(const char *str, int in_len) |
| 806 | { |
| 807 | return osmo_quote_str_buf(str, in_len, namebuf, sizeof(namebuf)); |
| 808 | } |
| 809 | |
Harald Welte | 179f357 | 2019-03-18 18:38:47 +0100 | [diff] [blame] | 810 | /*! Like osmo_quote_str_buf() but returns the result in a dynamically-allocated buffer. |
Harald Welte | 179f357 | 2019-03-18 18:38:47 +0100 | [diff] [blame] | 811 | * \param[in] str A string that may contain any characters. |
| 812 | * \param[in] in_len Pass -1 to print until nul char, or >= 0 to force a length. |
| 813 | * \returns dynamically-allocated buffer containing a quoted and escaped representation. |
| 814 | */ |
| 815 | char *osmo_quote_str_c(const void *ctx, const char *str, int in_len) |
| 816 | { |
Harald Welte | 37db7d3 | 2019-05-07 16:53:04 +0200 | [diff] [blame] | 817 | size_t len = in_len == -1 ? strlen(str) : in_len; |
| 818 | char *buf; |
| 819 | |
| 820 | /* account for two quote characters + terminating NUL */ |
| 821 | len += 3; |
| 822 | |
| 823 | /* some minimum length for things like "NULL" or "(error)" */ |
| 824 | if (len < 32) |
| 825 | len = 32; |
| 826 | |
| 827 | buf = talloc_size(ctx, len); |
Harald Welte | 179f357 | 2019-03-18 18:38:47 +0100 | [diff] [blame] | 828 | if (!buf) |
| 829 | return NULL; |
Harald Welte | 37db7d3 | 2019-05-07 16:53:04 +0200 | [diff] [blame] | 830 | |
| 831 | return osmo_quote_str_buf2(buf, len, str, in_len); |
Harald Welte | 179f357 | 2019-03-18 18:38:47 +0100 | [diff] [blame] | 832 | } |
| 833 | |
Harald Welte | 15a5f8d | 2018-06-06 16:58:17 +0200 | [diff] [blame] | 834 | /*! perform an integer square root operation on unsigned 32bit integer. |
| 835 | * This implementation is taken from "Hacker's Delight" Figure 11-1 "Integer square root, Newton's |
| 836 | * method", which can also be found at http://www.hackersdelight.org/hdcodetxt/isqrt.c.txt */ |
| 837 | uint32_t osmo_isqrt32(uint32_t x) |
| 838 | { |
| 839 | uint32_t x1; |
| 840 | int s, g0, g1; |
| 841 | |
| 842 | if (x <= 1) |
| 843 | return x; |
| 844 | |
| 845 | s = 1; |
| 846 | x1 = x - 1; |
| 847 | if (x1 > 0xffff) { |
| 848 | s = s + 8; |
| 849 | x1 = x1 >> 16; |
| 850 | } |
| 851 | if (x1 > 0xff) { |
| 852 | s = s + 4; |
| 853 | x1 = x1 >> 8; |
| 854 | } |
| 855 | if (x1 > 0xf) { |
| 856 | s = s + 2; |
| 857 | x1 = x1 >> 4; |
| 858 | } |
| 859 | if (x1 > 0x3) { |
| 860 | s = s + 1; |
| 861 | } |
| 862 | |
| 863 | g0 = 1 << s; /* g0 = 2**s */ |
| 864 | g1 = (g0 + (x >> s)) >> 1; /* g1 = (g0 + x/g0)/2 */ |
| 865 | |
| 866 | /* converges after four to five divisions for arguments up to 16,785,407 */ |
| 867 | while (g1 < g0) { |
| 868 | g0 = g1; |
| 869 | g1 = (g0 + (x/g0)) >> 1; |
| 870 | } |
| 871 | return g0; |
| 872 | } |
| 873 | |
Neels Hofmeyr | 7c74989 | 2018-09-07 03:01:38 +0200 | [diff] [blame] | 874 | /*! Convert a string to lowercase, while checking buffer size boundaries. |
| 875 | * The result written to \a dest is guaranteed to be nul terminated if \a dest_len > 0. |
| 876 | * If dest == src, the string is converted in-place, if necessary truncated at dest_len - 1 characters |
| 877 | * length as well as nul terminated. |
| 878 | * Note: similar osmo_str2lower(), but safe to use for src strings of arbitrary length. |
| 879 | * \param[out] dest Target buffer to write lowercase string. |
| 880 | * \param[in] dest_len Maximum buffer size of dest (e.g. sizeof(dest)). |
| 881 | * \param[in] src String to convert to lowercase. |
| 882 | * \returns Length of \a src, like osmo_strlcpy(), but if \a dest == \a src at most \a dest_len - 1. |
| 883 | */ |
| 884 | size_t osmo_str_tolower_buf(char *dest, size_t dest_len, const char *src) |
| 885 | { |
| 886 | size_t rc; |
| 887 | if (dest == src) { |
| 888 | if (dest_len < 1) |
| 889 | return 0; |
| 890 | dest[dest_len - 1] = '\0'; |
| 891 | rc = strlen(dest); |
| 892 | } else { |
| 893 | if (dest_len < 1) |
| 894 | return strlen(src); |
| 895 | rc = osmo_strlcpy(dest, src, dest_len); |
| 896 | } |
| 897 | for (; *dest; dest++) |
| 898 | *dest = tolower(*dest); |
| 899 | return rc; |
| 900 | } |
| 901 | |
| 902 | /*! Convert a string to lowercase, using a static buffer. |
| 903 | * The resulting string may be truncated if the internally used static buffer is shorter than src. |
| 904 | * The internal buffer is at least 128 bytes long, i.e. guaranteed to hold at least 127 characters and a |
Pau Espin Pedrol | a37f58e | 2019-08-01 18:11:41 +0200 | [diff] [blame] | 905 | * terminating nul. The static buffer returned is shared with osmo_str_toupper(). |
Neels Hofmeyr | 7c74989 | 2018-09-07 03:01:38 +0200 | [diff] [blame] | 906 | * See also osmo_str_tolower_buf(). |
| 907 | * \param[in] src String to convert to lowercase. |
| 908 | * \returns Resulting lowercase string in a static buffer, always nul terminated. |
| 909 | */ |
| 910 | const char *osmo_str_tolower(const char *src) |
| 911 | { |
Pau Espin Pedrol | a37f58e | 2019-08-01 18:11:41 +0200 | [diff] [blame] | 912 | osmo_str_tolower_buf(capsbuf, sizeof(capsbuf), src); |
| 913 | return capsbuf; |
Neels Hofmeyr | 7c74989 | 2018-09-07 03:01:38 +0200 | [diff] [blame] | 914 | } |
| 915 | |
Harald Welte | 179f357 | 2019-03-18 18:38:47 +0100 | [diff] [blame] | 916 | /*! Convert a string to lowercase, dynamically allocating the output from given talloc context |
| 917 | * See also osmo_str_tolower_buf(). |
| 918 | * \param[in] ctx talloc context from where to allocate the output string |
| 919 | * \param[in] src String to convert to lowercase. |
| 920 | * \returns Resulting lowercase string in a dynamically allocated buffer, always nul terminated. |
| 921 | */ |
| 922 | char *osmo_str_tolower_c(const void *ctx, const char *src) |
| 923 | { |
Vadim Yanitskiy | 4f619c2 | 2019-04-12 21:48:07 +0700 | [diff] [blame] | 924 | size_t buf_len = strlen(src) + 1; |
| 925 | char *buf = talloc_size(ctx, buf_len); |
Harald Welte | 179f357 | 2019-03-18 18:38:47 +0100 | [diff] [blame] | 926 | if (!buf) |
| 927 | return NULL; |
Vadim Yanitskiy | 4f619c2 | 2019-04-12 21:48:07 +0700 | [diff] [blame] | 928 | osmo_str_tolower_buf(buf, buf_len, src); |
Harald Welte | 179f357 | 2019-03-18 18:38:47 +0100 | [diff] [blame] | 929 | return buf; |
| 930 | } |
| 931 | |
Neels Hofmeyr | 7c74989 | 2018-09-07 03:01:38 +0200 | [diff] [blame] | 932 | /*! Convert a string to uppercase, while checking buffer size boundaries. |
| 933 | * The result written to \a dest is guaranteed to be nul terminated if \a dest_len > 0. |
| 934 | * If dest == src, the string is converted in-place, if necessary truncated at dest_len - 1 characters |
| 935 | * length as well as nul terminated. |
| 936 | * Note: similar osmo_str2upper(), but safe to use for src strings of arbitrary length. |
| 937 | * \param[out] dest Target buffer to write uppercase string. |
| 938 | * \param[in] dest_len Maximum buffer size of dest (e.g. sizeof(dest)). |
| 939 | * \param[in] src String to convert to uppercase. |
| 940 | * \returns Length of \a src, like osmo_strlcpy(), but if \a dest == \a src at most \a dest_len - 1. |
| 941 | */ |
| 942 | size_t osmo_str_toupper_buf(char *dest, size_t dest_len, const char *src) |
| 943 | { |
| 944 | size_t rc; |
| 945 | if (dest == src) { |
| 946 | if (dest_len < 1) |
| 947 | return 0; |
| 948 | dest[dest_len - 1] = '\0'; |
| 949 | rc = strlen(dest); |
| 950 | } else { |
| 951 | if (dest_len < 1) |
| 952 | return strlen(src); |
| 953 | rc = osmo_strlcpy(dest, src, dest_len); |
| 954 | } |
| 955 | for (; *dest; dest++) |
| 956 | *dest = toupper(*dest); |
| 957 | return rc; |
| 958 | } |
| 959 | |
| 960 | /*! Convert a string to uppercase, using a static buffer. |
| 961 | * The resulting string may be truncated if the internally used static buffer is shorter than src. |
| 962 | * The internal buffer is at least 128 bytes long, i.e. guaranteed to hold at least 127 characters and a |
Pau Espin Pedrol | a37f58e | 2019-08-01 18:11:41 +0200 | [diff] [blame] | 963 | * terminating nul. The static buffer returned is shared with osmo_str_tolower(). |
Neels Hofmeyr | 7c74989 | 2018-09-07 03:01:38 +0200 | [diff] [blame] | 964 | * See also osmo_str_toupper_buf(). |
| 965 | * \param[in] src String to convert to uppercase. |
| 966 | * \returns Resulting uppercase string in a static buffer, always nul terminated. |
| 967 | */ |
| 968 | const char *osmo_str_toupper(const char *src) |
| 969 | { |
Pau Espin Pedrol | a37f58e | 2019-08-01 18:11:41 +0200 | [diff] [blame] | 970 | osmo_str_toupper_buf(capsbuf, sizeof(capsbuf), src); |
| 971 | return capsbuf; |
Neels Hofmeyr | 7c74989 | 2018-09-07 03:01:38 +0200 | [diff] [blame] | 972 | } |
| 973 | |
Harald Welte | 179f357 | 2019-03-18 18:38:47 +0100 | [diff] [blame] | 974 | /*! Convert a string to uppercase, dynamically allocating the output from given talloc context |
| 975 | * See also osmo_str_tolower_buf(). |
| 976 | * \param[in] ctx talloc context from where to allocate the output string |
| 977 | * \param[in] src String to convert to uppercase. |
| 978 | * \returns Resulting uppercase string in a dynamically allocated buffer, always nul terminated. |
| 979 | */ |
| 980 | char *osmo_str_toupper_c(const void *ctx, const char *src) |
| 981 | { |
Vadim Yanitskiy | 4f619c2 | 2019-04-12 21:48:07 +0700 | [diff] [blame] | 982 | size_t buf_len = strlen(src) + 1; |
| 983 | char *buf = talloc_size(ctx, buf_len); |
Harald Welte | 179f357 | 2019-03-18 18:38:47 +0100 | [diff] [blame] | 984 | if (!buf) |
| 985 | return NULL; |
Vadim Yanitskiy | 4f619c2 | 2019-04-12 21:48:07 +0700 | [diff] [blame] | 986 | osmo_str_toupper_buf(buf, buf_len, src); |
Harald Welte | 179f357 | 2019-03-18 18:38:47 +0100 | [diff] [blame] | 987 | return buf; |
| 988 | } |
| 989 | |
Oliver Smith | 894be2d | 2019-01-11 13:13:37 +0100 | [diff] [blame] | 990 | /*! Calculate the Luhn checksum (as used for IMEIs). |
| 991 | * \param[in] in Input digits in ASCII string representation. |
| 992 | * \param[in] in_len Count of digits to use for the input (14 for IMEI). |
| 993 | * \returns checksum char (e.g. '3'); negative on error |
| 994 | */ |
Vadim Yanitskiy | d9fc604 | 2019-06-12 15:49:03 +0700 | [diff] [blame] | 995 | char osmo_luhn(const char* in, int in_len) |
Oliver Smith | 894be2d | 2019-01-11 13:13:37 +0100 | [diff] [blame] | 996 | { |
| 997 | int i, sum = 0; |
| 998 | |
| 999 | /* All input must be numbers */ |
| 1000 | for (i = 0; i < in_len; i++) { |
Kévin Redon | 1af2cd5 | 2019-05-23 19:00:19 +0200 | [diff] [blame] | 1001 | if (!isdigit((unsigned char)in[i])) |
Oliver Smith | 894be2d | 2019-01-11 13:13:37 +0100 | [diff] [blame] | 1002 | return -EINVAL; |
| 1003 | } |
| 1004 | |
| 1005 | /* Double every second digit and add it to sum */ |
| 1006 | for (i = in_len - 1; i >= 0; i -= 2) { |
| 1007 | int dbl = (in[i] - '0') * 2; |
| 1008 | if (dbl > 9) |
| 1009 | dbl -= 9; |
| 1010 | sum += dbl; |
| 1011 | } |
| 1012 | |
| 1013 | /* Add other digits to sum */ |
| 1014 | for (i = in_len - 2; i >= 0; i -= 2) |
| 1015 | sum += in[i] - '0'; |
| 1016 | |
| 1017 | /* Final checksum */ |
| 1018 | return (sum * 9) % 10 + '0'; |
| 1019 | } |
| 1020 | |
Neels Hofmeyr | d79ccc6 | 2019-03-07 23:08:40 +0100 | [diff] [blame] | 1021 | /*! Compare start of a string. |
| 1022 | * This is an optimisation of 'strstr(str, startswith_str) == str' because it doesn't search through the entire string. |
| 1023 | * \param str (Longer) string to compare. |
| 1024 | * \param startswith_str (Shorter) string to compare with the start of str. |
| 1025 | * \return true iff the first characters of str fully match startswith_str or startswith_str is empty. */ |
| 1026 | bool osmo_str_startswith(const char *str, const char *startswith_str) |
| 1027 | { |
| 1028 | if (!startswith_str || !*startswith_str) |
| 1029 | return true; |
| 1030 | if (!str) |
| 1031 | return false; |
| 1032 | return strncmp(str, startswith_str, strlen(startswith_str)) == 0; |
| 1033 | } |
| 1034 | |
Neels Hofmeyr | 0aeda1b | 2017-01-13 14:16:02 +0100 | [diff] [blame] | 1035 | /*! @} */ |