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