Harald Welte | 468b643 | 2014-09-11 13:05:51 +0800 | [diff] [blame] | 1 | /* |
| 2 | * (C) 2011 by Harald Welte <laforge@gnumonks.org> |
| 3 | * (C) 2011 by Sylvain Munaut <tnt@246tNt.com> |
| 4 | * (C) 2014 by Nils O. SelÄsdal <noselasd@fiane.dyndns.org> |
| 5 | * |
| 6 | * All Rights Reserved |
| 7 | * |
Harald Welte | e08da97 | 2017-11-13 01:00:26 +0900 | [diff] [blame] | 8 | * SPDX-License-Identifier: GPL-2.0+ |
| 9 | * |
Harald Welte | 468b643 | 2014-09-11 13:05:51 +0800 | [diff] [blame] | 10 | * This program is free software; you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU General Public License as published by |
| 12 | * the Free Software Foundation; either version 2 of the License, or |
| 13 | * (at your option) any later version. |
| 14 | * |
| 15 | * This program is distributed in the hope that it will be useful, |
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | * GNU General Public License for more details. |
| 19 | * |
| 20 | * You should have received a copy of the GNU General Public License along |
| 21 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 22 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 23 | * |
| 24 | */ |
| 25 | |
Harald Welte | d284cd9 | 2010-03-01 21:58:31 +0100 | [diff] [blame] | 26 | |
Harald Welte | febe83c | 2017-10-03 17:41:59 +0800 | [diff] [blame] | 27 | #include <stdbool.h> |
Harald Welte | d284cd9 | 2010-03-01 21:58:31 +0100 | [diff] [blame] | 28 | #include <string.h> |
| 29 | #include <stdint.h> |
| 30 | #include <errno.h> |
Harald Welte | b59f935 | 2010-03-25 11:37:04 +0800 | [diff] [blame] | 31 | #include <stdio.h> |
Pau Espin Pedrol | 4573502 | 2017-06-18 14:05:24 +0200 | [diff] [blame] | 32 | #include <inttypes.h> |
Harald Welte | d284cd9 | 2010-03-01 21:58:31 +0100 | [diff] [blame] | 33 | |
Pablo Neira Ayuso | 8341934 | 2011-03-22 16:36:13 +0100 | [diff] [blame] | 34 | #include <osmocom/core/utils.h> |
Harald Welte | 9709b2e | 2016-04-25 18:47:53 +0200 | [diff] [blame] | 35 | #include <osmocom/core/bit64gen.h> |
| 36 | |
Harald Welte | d284cd9 | 2010-03-01 21:58:31 +0100 | [diff] [blame] | 37 | |
Harald Welte | 8598f18 | 2011-08-17 14:19:27 +0200 | [diff] [blame] | 38 | /*! \addtogroup utils |
| 39 | * @{ |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 40 | * various utility routines |
Neels Hofmeyr | 17518fe | 2017-06-20 04:35:06 +0200 | [diff] [blame] | 41 | * |
| 42 | * \file utils.c */ |
Harald Welte | 8598f18 | 2011-08-17 14:19:27 +0200 | [diff] [blame] | 43 | |
Harald Welte | b59f935 | 2010-03-25 11:37:04 +0800 | [diff] [blame] | 44 | static char namebuf[255]; |
Harald Welte | 8598f18 | 2011-08-17 14:19:27 +0200 | [diff] [blame] | 45 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 46 | /*! get human-readable string for given value |
Harald Welte | 8598f18 | 2011-08-17 14:19:27 +0200 | [diff] [blame] | 47 | * \param[in] vs Array of value_string tuples |
| 48 | * \param[in] val Value to be converted |
| 49 | * \returns pointer to human-readable string |
Neels Hofmeyr | 8a3c83e | 2016-06-13 13:16:58 +0200 | [diff] [blame] | 50 | * |
| 51 | * If val is found in vs, the array's string entry is returned. Otherwise, an |
| 52 | * "unknown" string containing the actual value is composed in a static buffer |
| 53 | * that is reused across invocations. |
Harald Welte | 8598f18 | 2011-08-17 14:19:27 +0200 | [diff] [blame] | 54 | */ |
Harald Welte | d284cd9 | 2010-03-01 21:58:31 +0100 | [diff] [blame] | 55 | const char *get_value_string(const struct value_string *vs, uint32_t val) |
| 56 | { |
Neels Hofmeyr | 8d6dcd9 | 2016-06-06 18:05:23 +0200 | [diff] [blame] | 57 | const char *str = get_value_string_or_null(vs, val); |
| 58 | if (str) |
| 59 | return str; |
| 60 | |
Pau Espin Pedrol | 4573502 | 2017-06-18 14:05:24 +0200 | [diff] [blame] | 61 | snprintf(namebuf, sizeof(namebuf), "unknown 0x%"PRIx32, val); |
Neels Hofmeyr | 8d6dcd9 | 2016-06-06 18:05:23 +0200 | [diff] [blame] | 62 | namebuf[sizeof(namebuf) - 1] = '\0'; |
| 63 | return namebuf; |
| 64 | } |
| 65 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 66 | /*! get human-readable string or NULL for given value |
Neels Hofmeyr | 8d6dcd9 | 2016-06-06 18:05:23 +0200 | [diff] [blame] | 67 | * \param[in] vs Array of value_string tuples |
| 68 | * \param[in] val Value to be converted |
| 69 | * \returns pointer to human-readable string or NULL if val is not found |
| 70 | */ |
| 71 | const char *get_value_string_or_null(const struct value_string *vs, |
| 72 | uint32_t val) |
| 73 | { |
Harald Welte | d284cd9 | 2010-03-01 21:58:31 +0100 | [diff] [blame] | 74 | int i; |
| 75 | |
| 76 | for (i = 0;; i++) { |
| 77 | if (vs[i].value == 0 && vs[i].str == NULL) |
| 78 | break; |
| 79 | if (vs[i].value == val) |
| 80 | return vs[i].str; |
| 81 | } |
Harald Welte | b59f935 | 2010-03-25 11:37:04 +0800 | [diff] [blame] | 82 | |
Neels Hofmeyr | 8d6dcd9 | 2016-06-06 18:05:23 +0200 | [diff] [blame] | 83 | return NULL; |
Harald Welte | d284cd9 | 2010-03-01 21:58:31 +0100 | [diff] [blame] | 84 | } |
| 85 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 86 | /*! get numeric value for given human-readable string |
Harald Welte | 8598f18 | 2011-08-17 14:19:27 +0200 | [diff] [blame] | 87 | * \param[in] vs Array of value_string tuples |
| 88 | * \param[in] str human-readable string |
| 89 | * \returns numeric value (>0) or negative numer in case of error |
| 90 | */ |
Harald Welte | d284cd9 | 2010-03-01 21:58:31 +0100 | [diff] [blame] | 91 | int get_string_value(const struct value_string *vs, const char *str) |
| 92 | { |
| 93 | int i; |
| 94 | |
| 95 | for (i = 0;; i++) { |
| 96 | if (vs[i].value == 0 && vs[i].str == NULL) |
| 97 | break; |
| 98 | if (!strcasecmp(vs[i].str, str)) |
| 99 | return vs[i].value; |
| 100 | } |
| 101 | return -EINVAL; |
| 102 | } |
Harald Welte | a73e2f9 | 2010-03-04 10:50:32 +0100 | [diff] [blame] | 103 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 104 | /*! Convert BCD-encoded digit into printable character |
Harald Welte | 8598f18 | 2011-08-17 14:19:27 +0200 | [diff] [blame] | 105 | * \param[in] bcd A single BCD-encoded digit |
| 106 | * \returns single printable character |
| 107 | */ |
Pablo Neira Ayuso | 87f7b25 | 2011-05-07 12:43:08 +0200 | [diff] [blame] | 108 | char osmo_bcd2char(uint8_t bcd) |
Harald Welte | a73e2f9 | 2010-03-04 10:50:32 +0100 | [diff] [blame] | 109 | { |
| 110 | if (bcd < 0xa) |
| 111 | return '0' + bcd; |
| 112 | else |
| 113 | return 'A' + (bcd - 0xa); |
| 114 | } |
| 115 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 116 | /*! Convert number in ASCII to BCD value |
Harald Welte | de6e498 | 2012-12-06 21:25:27 +0100 | [diff] [blame] | 117 | * \param[in] c ASCII character |
| 118 | * \returns BCD encoded value of character |
| 119 | */ |
Pablo Neira Ayuso | 87f7b25 | 2011-05-07 12:43:08 +0200 | [diff] [blame] | 120 | uint8_t osmo_char2bcd(char c) |
Harald Welte | a73e2f9 | 2010-03-04 10:50:32 +0100 | [diff] [blame] | 121 | { |
Harald Welte | fa8983d | 2017-10-27 16:52:59 +0200 | [diff] [blame] | 122 | if (c >= '0' && c <= '9') |
| 123 | return c - 0x30; |
| 124 | else if (c >= 'A' && c <= 'F') |
| 125 | return 0xa + (c - 'A'); |
| 126 | else if (c >= 'a' && c <= 'f') |
| 127 | return 0xa + (c - 'a'); |
| 128 | else |
| 129 | return 0; |
Harald Welte | a73e2f9 | 2010-03-04 10:50:32 +0100 | [diff] [blame] | 130 | } |
Harald Welte | 3eba991 | 2010-07-30 10:37:29 +0200 | [diff] [blame] | 131 | |
Neels Hofmeyr | 7079e69 | 2018-12-05 21:02:36 +0100 | [diff] [blame] | 132 | /*! Convert BCD to string. |
| 133 | * The given nibble offsets are interpreted in BCD order, i.e. nibble 0 is bcd[0] & 0xf, nibble 1 is bcd[0] >> 4, nibble |
| 134 | * 3 is bcd[1] & 0xf, etc.. |
| 135 | * \param[out] dst Output string buffer, is always nul terminated when dst_size > 0. |
| 136 | * \param[in] dst_size sizeof() the output string buffer. |
| 137 | * \param[in] bcd Binary coded data buffer. |
| 138 | * \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] | 139 | * \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] | 140 | * \param[in] allow_hex If false, return error if there are digits other than 0-9. If true, return those as [A-F]. |
| 141 | * \returns The strlen that would be written if the output buffer is large enough, excluding nul byte (like |
| 142 | * snprintf()), or -EINVAL if allow_hex is false and a digit > 9 is encountered. On -EINVAL, the conversion is |
| 143 | * still completed as if allow_hex were passed as true. Return -ENOMEM if dst is NULL or dst_size is zero. |
| 144 | * If end_nibble <= start_nibble, write an empty string to dst and return 0. |
| 145 | */ |
| 146 | int osmo_bcd2str(char *dst, size_t dst_size, const uint8_t *bcd, int start_nibble, int end_nibble, bool allow_hex) |
| 147 | { |
| 148 | char *dst_end = dst + dst_size - 1; |
| 149 | int nibble_i; |
| 150 | int rc = 0; |
| 151 | |
| 152 | if (!dst || dst_size < 1) |
| 153 | return -ENOMEM; |
| 154 | |
| 155 | for (nibble_i = start_nibble; nibble_i < end_nibble && dst < dst_end; nibble_i++, dst++) { |
| 156 | uint8_t nibble = bcd[nibble_i >> 1]; |
| 157 | if ((nibble_i & 1)) |
| 158 | nibble >>= 4; |
| 159 | nibble &= 0xf; |
| 160 | |
| 161 | if (!allow_hex && nibble > 9) |
| 162 | rc = -EINVAL; |
| 163 | |
| 164 | *dst = osmo_bcd2char(nibble); |
| 165 | } |
| 166 | *dst = '\0'; |
| 167 | |
| 168 | if (rc < 0) |
| 169 | return rc; |
| 170 | return OSMO_MAX(0, end_nibble - start_nibble); |
| 171 | } |
| 172 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 173 | /*! Parse a string containing hexadecimal digits |
Harald Welte | de6e498 | 2012-12-06 21:25:27 +0100 | [diff] [blame] | 174 | * \param[in] str string containing ASCII encoded hexadecimal digits |
| 175 | * \param[out] b output buffer |
| 176 | * \param[in] max_len maximum space in output buffer |
Neels Hofmeyr | 3de7b05 | 2015-09-23 23:16:53 +0200 | [diff] [blame] | 177 | * \returns number of parsed octets, or -1 on error |
Harald Welte | de6e498 | 2012-12-06 21:25:27 +0100 | [diff] [blame] | 178 | */ |
Pablo Neira Ayuso | 87f7b25 | 2011-05-07 12:43:08 +0200 | [diff] [blame] | 179 | int osmo_hexparse(const char *str, uint8_t *b, int max_len) |
Harald Welte | 3eba991 | 2010-07-30 10:37:29 +0200 | [diff] [blame] | 180 | |
| 181 | { |
Neels Hofmeyr | 437ed4a | 2017-02-14 15:54:31 +0100 | [diff] [blame] | 182 | char c; |
| 183 | uint8_t v; |
| 184 | const char *strpos; |
| 185 | unsigned int nibblepos = 0; |
Harald Welte | 3eba991 | 2010-07-30 10:37:29 +0200 | [diff] [blame] | 186 | |
| 187 | memset(b, 0x00, max_len); |
| 188 | |
Neels Hofmeyr | 437ed4a | 2017-02-14 15:54:31 +0100 | [diff] [blame] | 189 | for (strpos = str; (c = *strpos); strpos++) { |
| 190 | /* skip whitespace */ |
| 191 | if (c == ' ' || c == '\t' || c == '\n' || c == '\r') |
| 192 | continue; |
| 193 | |
| 194 | /* If the buffer is too small, error out */ |
| 195 | if (nibblepos >= (max_len << 1)) |
| 196 | return -1; |
| 197 | |
Harald Welte | 3eba991 | 2010-07-30 10:37:29 +0200 | [diff] [blame] | 198 | if (c >= '0' && c <= '9') |
| 199 | v = c - '0'; |
| 200 | else if (c >= 'a' && c <= 'f') |
| 201 | v = 10 + (c - 'a'); |
| 202 | else if (c >= 'A' && c <= 'F') |
| 203 | v = 10 + (c - 'A'); |
| 204 | else |
| 205 | return -1; |
Neels Hofmeyr | 437ed4a | 2017-02-14 15:54:31 +0100 | [diff] [blame] | 206 | |
| 207 | b[nibblepos >> 1] |= v << (nibblepos & 1 ? 0 : 4); |
| 208 | nibblepos ++; |
Harald Welte | 3eba991 | 2010-07-30 10:37:29 +0200 | [diff] [blame] | 209 | } |
| 210 | |
Neels Hofmeyr | 437ed4a | 2017-02-14 15:54:31 +0100 | [diff] [blame] | 211 | /* In case of uneven amount of digits, the last byte is not complete |
| 212 | * and that's an error. */ |
| 213 | if (nibblepos & 1) |
| 214 | return -1; |
| 215 | |
| 216 | return nibblepos >> 1; |
Harald Welte | 3eba991 | 2010-07-30 10:37:29 +0200 | [diff] [blame] | 217 | } |
Harald Welte | 40481e8 | 2010-07-30 11:40:32 +0200 | [diff] [blame] | 218 | |
| 219 | static char hexd_buff[4096]; |
Nils O. SelÄsdal | 3244702 | 2014-01-02 14:04:43 +0100 | [diff] [blame] | 220 | static const char hex_chars[] = "0123456789abcdef"; |
Harald Welte | 40481e8 | 2010-07-30 11:40:32 +0200 | [diff] [blame] | 221 | |
Pablo Neira Ayuso | 87f7b25 | 2011-05-07 12:43:08 +0200 | [diff] [blame] | 222 | static char *_osmo_hexdump(const unsigned char *buf, int len, char *delim) |
Harald Welte | 40481e8 | 2010-07-30 11:40:32 +0200 | [diff] [blame] | 223 | { |
| 224 | int i; |
| 225 | char *cur = hexd_buff; |
| 226 | |
| 227 | hexd_buff[0] = 0; |
| 228 | for (i = 0; i < len; i++) { |
Nils O. SelÄsdal | 3244702 | 2014-01-02 14:04:43 +0100 | [diff] [blame] | 229 | const char *delimp = delim; |
Harald Welte | 40481e8 | 2010-07-30 11:40:32 +0200 | [diff] [blame] | 230 | int len_remain = sizeof(hexd_buff) - (cur - hexd_buff); |
Nils O. SelÄsdal | 3244702 | 2014-01-02 14:04:43 +0100 | [diff] [blame] | 231 | if (len_remain < 3) |
Holger Hans Peter Freyther | 128d9e2 | 2011-07-15 16:07:23 +0200 | [diff] [blame] | 232 | break; |
Nils O. SelÄsdal | 3244702 | 2014-01-02 14:04:43 +0100 | [diff] [blame] | 233 | |
| 234 | *cur++ = hex_chars[buf[i] >> 4]; |
| 235 | *cur++ = hex_chars[buf[i] & 0xf]; |
| 236 | |
| 237 | while (len_remain > 1 && *delimp) { |
| 238 | *cur++ = *delimp++; |
| 239 | len_remain--; |
| 240 | } |
| 241 | |
| 242 | *cur = 0; |
Harald Welte | 40481e8 | 2010-07-30 11:40:32 +0200 | [diff] [blame] | 243 | } |
| 244 | hexd_buff[sizeof(hexd_buff)-1] = 0; |
| 245 | return hexd_buff; |
| 246 | } |
Harald Welte | dee47cd | 2010-07-30 11:43:30 +0200 | [diff] [blame] | 247 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 248 | /*! Convert a sequence of unpacked bits to ASCII string |
Harald Welte | 8598f18 | 2011-08-17 14:19:27 +0200 | [diff] [blame] | 249 | * \param[in] bits A sequence of unpacked bits |
| 250 | * \param[in] len Length of bits |
| 251 | */ |
Pablo Neira Ayuso | 87f7b25 | 2011-05-07 12:43:08 +0200 | [diff] [blame] | 252 | char *osmo_ubit_dump(const uint8_t *bits, unsigned int len) |
Harald Welte | 3d0ac5e | 2011-02-08 16:55:03 +0100 | [diff] [blame] | 253 | { |
| 254 | int i; |
| 255 | |
| 256 | if (len > sizeof(hexd_buff)-1) |
| 257 | len = sizeof(hexd_buff)-1; |
| 258 | memset(hexd_buff, 0, sizeof(hexd_buff)); |
| 259 | |
| 260 | for (i = 0; i < len; i++) { |
| 261 | char outch; |
| 262 | switch (bits[i]) { |
| 263 | case 0: |
| 264 | outch = '0'; |
| 265 | break; |
| 266 | case 0xff: |
| 267 | outch = '?'; |
| 268 | break; |
| 269 | case 1: |
| 270 | outch = '1'; |
| 271 | break; |
| 272 | default: |
| 273 | outch = 'E'; |
| 274 | break; |
| 275 | } |
| 276 | hexd_buff[i] = outch; |
| 277 | } |
| 278 | hexd_buff[sizeof(hexd_buff)-1] = 0; |
| 279 | return hexd_buff; |
| 280 | } |
| 281 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 282 | /*! Convert binary sequence to hexadecimal ASCII string |
Harald Welte | 8598f18 | 2011-08-17 14:19:27 +0200 | [diff] [blame] | 283 | * \param[in] buf pointer to sequence of bytes |
| 284 | * \param[in] len length of buf in number of bytes |
| 285 | * \returns pointer to zero-terminated string |
| 286 | * |
| 287 | * This function will print a sequence of bytes as hexadecimal numbers, |
| 288 | * adding one space character between each byte (e.g. "1a ef d9") |
Harald Welte | 096a666 | 2017-10-16 14:33:11 +0200 | [diff] [blame] | 289 | * |
| 290 | * The maximum size of the output buffer is 4096 bytes, i.e. the maximum |
| 291 | * 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] | 292 | */ |
Pablo Neira Ayuso | 87f7b25 | 2011-05-07 12:43:08 +0200 | [diff] [blame] | 293 | char *osmo_hexdump(const unsigned char *buf, int len) |
Harald Welte | dee47cd | 2010-07-30 11:43:30 +0200 | [diff] [blame] | 294 | { |
Pablo Neira Ayuso | 87f7b25 | 2011-05-07 12:43:08 +0200 | [diff] [blame] | 295 | return _osmo_hexdump(buf, len, " "); |
Harald Welte | dee47cd | 2010-07-30 11:43:30 +0200 | [diff] [blame] | 296 | } |
| 297 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 298 | /*! Convert binary sequence to hexadecimal ASCII string |
Harald Welte | 8598f18 | 2011-08-17 14:19:27 +0200 | [diff] [blame] | 299 | * \param[in] buf pointer to sequence of bytes |
| 300 | * \param[in] len length of buf in number of bytes |
| 301 | * \returns pointer to zero-terminated string |
| 302 | * |
| 303 | * This function will print a sequence of bytes as hexadecimal numbers, |
| 304 | * without any space character between each byte (e.g. "1aefd9") |
Harald Welte | 096a666 | 2017-10-16 14:33:11 +0200 | [diff] [blame] | 305 | * |
| 306 | * The maximum size of the output buffer is 4096 bytes, i.e. the maximum |
| 307 | * 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] | 308 | */ |
Sylvain Munaut | ff23d24 | 2011-11-10 23:03:18 +0100 | [diff] [blame] | 309 | char *osmo_hexdump_nospc(const unsigned char *buf, int len) |
Harald Welte | dee47cd | 2010-07-30 11:43:30 +0200 | [diff] [blame] | 310 | { |
Pablo Neira Ayuso | 87f7b25 | 2011-05-07 12:43:08 +0200 | [diff] [blame] | 311 | return _osmo_hexdump(buf, len, ""); |
Harald Welte | dee47cd | 2010-07-30 11:43:30 +0200 | [diff] [blame] | 312 | } |
Harald Welte | 2822296 | 2011-02-18 20:37:04 +0100 | [diff] [blame] | 313 | |
Holger Hans Peter Freyther | 9a1a5a1 | 2015-04-11 19:26:55 +0200 | [diff] [blame] | 314 | /* Compat with previous typo to preserve abi */ |
Sylvain Munaut | e55ae3a | 2011-11-11 23:06:55 +0100 | [diff] [blame] | 315 | 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] | 316 | #if defined(__MACH__) && defined(__APPLE__) |
| 317 | ; |
| 318 | #else |
Sylvain Munaut | 17af41d | 2011-11-19 22:30:39 +0100 | [diff] [blame] | 319 | __attribute__((weak, alias("osmo_hexdump_nospc"))); |
Holger Hans Peter Freyther | 9a1a5a1 | 2015-04-11 19:26:55 +0200 | [diff] [blame] | 320 | #endif |
Sylvain Munaut | e55ae3a | 2011-11-11 23:06:55 +0100 | [diff] [blame] | 321 | |
Harald Welte | 2822296 | 2011-02-18 20:37:04 +0100 | [diff] [blame] | 322 | #include "../config.h" |
| 323 | #ifdef HAVE_CTYPE_H |
| 324 | #include <ctype.h> |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 325 | /*! Convert an entire string to lower case |
Harald Welte | 8598f18 | 2011-08-17 14:19:27 +0200 | [diff] [blame] | 326 | * \param[out] out output string, caller-allocated |
| 327 | * \param[in] in input string |
| 328 | */ |
Harald Welte | 2822296 | 2011-02-18 20:37:04 +0100 | [diff] [blame] | 329 | void osmo_str2lower(char *out, const char *in) |
| 330 | { |
| 331 | unsigned int i; |
| 332 | |
| 333 | for (i = 0; i < strlen(in); i++) |
Pau Espin Pedrol | 399a6f0 | 2017-06-18 14:07:37 +0200 | [diff] [blame] | 334 | out[i] = tolower((const unsigned char)in[i]); |
Harald Welte | 2822296 | 2011-02-18 20:37:04 +0100 | [diff] [blame] | 335 | out[strlen(in)] = '\0'; |
| 336 | } |
| 337 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 338 | /*! Convert an entire string to upper case |
Harald Welte | 8598f18 | 2011-08-17 14:19:27 +0200 | [diff] [blame] | 339 | * \param[out] out output string, caller-allocated |
| 340 | * \param[in] in input string |
| 341 | */ |
Harald Welte | 2822296 | 2011-02-18 20:37:04 +0100 | [diff] [blame] | 342 | void osmo_str2upper(char *out, const char *in) |
| 343 | { |
| 344 | unsigned int i; |
| 345 | |
| 346 | for (i = 0; i < strlen(in); i++) |
Pau Espin Pedrol | 399a6f0 | 2017-06-18 14:07:37 +0200 | [diff] [blame] | 347 | out[i] = toupper((const unsigned char)in[i]); |
Harald Welte | 2822296 | 2011-02-18 20:37:04 +0100 | [diff] [blame] | 348 | out[strlen(in)] = '\0'; |
| 349 | } |
| 350 | #endif /* HAVE_CTYPE_H */ |
Harald Welte | 8598f18 | 2011-08-17 14:19:27 +0200 | [diff] [blame] | 351 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 352 | /*! Wishful thinking to generate a constant time compare |
Harald Welte | 9709b2e | 2016-04-25 18:47:53 +0200 | [diff] [blame] | 353 | * \param[in] exp Expected data |
| 354 | * \param[in] rel Comparison value |
| 355 | * \param[in] count Number of bytes to compare |
| 356 | * \returns 1 in case \a exp equals \a rel; zero otherwise |
| 357 | * |
| 358 | * Compare count bytes of exp to rel. Return 0 if they are identical, 1 |
| 359 | * otherwise. Do not return a mismatch on the first mismatching byte, |
| 360 | * but always compare all bytes, regardless. The idea is that the amount of |
| 361 | * matching bytes cannot be inferred from the time the comparison took. */ |
| 362 | int osmo_constant_time_cmp(const uint8_t *exp, const uint8_t *rel, const int count) |
| 363 | { |
| 364 | int x = 0, i; |
| 365 | |
| 366 | for (i = 0; i < count; ++i) |
| 367 | x |= exp[i] ^ rel[i]; |
| 368 | |
| 369 | /* if x is zero, all data was identical */ |
| 370 | return x? 1 : 0; |
| 371 | } |
| 372 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 373 | /*! Generic retrieval of 1..8 bytes as big-endian uint64_t |
Harald Welte | 9709b2e | 2016-04-25 18:47:53 +0200 | [diff] [blame] | 374 | * \param[in] data Input data as byte-array |
| 375 | * \param[in] data_len Length of \a data in octets |
| 376 | * \returns uint64_t of \a data interpreted as big-endian |
| 377 | * |
| 378 | * This is like osmo_load64be_ext, except that if data_len is less than |
| 379 | * sizeof(uint64_t), the data is interpreted as the least significant bytes |
| 380 | * (osmo_load64be_ext loads them as the most significant bytes into the |
| 381 | * returned uint64_t). In this way, any integer size up to 64 bits can be |
| 382 | * decoded conveniently by using sizeof(), without the need to call specific |
| 383 | * numbered functions (osmo_load16, 32, ...). */ |
| 384 | uint64_t osmo_decode_big_endian(const uint8_t *data, size_t data_len) |
| 385 | { |
| 386 | uint64_t value = 0; |
| 387 | |
| 388 | while (data_len > 0) { |
| 389 | value = (value << 8) + *data; |
| 390 | data += 1; |
| 391 | data_len -= 1; |
| 392 | } |
| 393 | |
| 394 | return value; |
| 395 | } |
| 396 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 397 | /*! Generic big-endian encoding of big endian number up to 64bit |
Harald Welte | 9709b2e | 2016-04-25 18:47:53 +0200 | [diff] [blame] | 398 | * \param[in] value unsigned integer value to be stored |
| 399 | * \param[in] data_len number of octets |
| 400 | * \returns static buffer containing big-endian stored value |
| 401 | * |
| 402 | * This is like osmo_store64be_ext, except that this returns a static buffer of |
| 403 | * the result (for convenience, but not threadsafe). If data_len is less than |
| 404 | * sizeof(uint64_t), only the least significant bytes of value are encoded. */ |
| 405 | uint8_t *osmo_encode_big_endian(uint64_t value, size_t data_len) |
| 406 | { |
| 407 | static uint8_t buf[sizeof(uint64_t)]; |
| 408 | OSMO_ASSERT(data_len <= ARRAY_SIZE(buf)); |
| 409 | osmo_store64be_ext(value, buf, data_len); |
| 410 | return buf; |
| 411 | } |
Harald Welte | aeecc48 | 2016-11-26 10:41:40 +0100 | [diff] [blame] | 412 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 413 | /*! Copy a C-string into a sized buffer |
Harald Welte | aeecc48 | 2016-11-26 10:41:40 +0100 | [diff] [blame] | 414 | * \param[in] src source string |
| 415 | * \param[out] dst destination string |
Neels Hofmeyr | df83ece | 2017-01-13 13:55:43 +0100 | [diff] [blame] | 416 | * \param[in] siz size of the \a dst buffer |
| 417 | * \returns length of \a src |
Harald Welte | aeecc48 | 2016-11-26 10:41:40 +0100 | [diff] [blame] | 418 | * |
Neels Hofmeyr | df83ece | 2017-01-13 13:55:43 +0100 | [diff] [blame] | 419 | * Copy at most \a siz bytes from \a src to \a dst, ensuring that the result is |
| 420 | * NUL terminated. The NUL character is included in \a siz, i.e. passing the |
| 421 | * actual sizeof(*dst) is correct. |
Harald Welte | aeecc48 | 2016-11-26 10:41:40 +0100 | [diff] [blame] | 422 | */ |
| 423 | size_t osmo_strlcpy(char *dst, const char *src, size_t siz) |
| 424 | { |
Neels Hofmeyr | bcf9f23 | 2017-10-25 04:16:45 +0200 | [diff] [blame] | 425 | size_t ret = src ? strlen(src) : 0; |
Harald Welte | aeecc48 | 2016-11-26 10:41:40 +0100 | [diff] [blame] | 426 | |
| 427 | if (siz) { |
| 428 | size_t len = (ret >= siz) ? siz - 1 : ret; |
Neels Hofmeyr | ebd3cdd | 2017-11-18 23:07:38 +0100 | [diff] [blame] | 429 | if (src) |
| 430 | memcpy(dst, src, len); |
Harald Welte | aeecc48 | 2016-11-26 10:41:40 +0100 | [diff] [blame] | 431 | dst[len] = '\0'; |
| 432 | } |
| 433 | return ret; |
| 434 | } |
Neels Hofmeyr | 0aeda1b | 2017-01-13 14:16:02 +0100 | [diff] [blame] | 435 | |
Neels Hofmeyr | 4335bad | 2017-10-07 04:39:14 +0200 | [diff] [blame] | 436 | /*! Validate that a given string is a hex string within given size limits. |
| 437 | * Note that each hex digit amounts to a nibble, so if checking for a hex |
| 438 | * string to result in N bytes, pass amount of digits as 2*N. |
| 439 | * \param str A nul-terminated string to validate, or NULL. |
| 440 | * \param min_digits least permitted amount of digits. |
| 441 | * \param max_digits most permitted amount of digits. |
| 442 | * \param require_even if true, require an even amount of digits. |
| 443 | * \returns true when the hex_str contains only hexadecimal digits (no |
| 444 | * whitespace) and matches the requested length; also true |
| 445 | * when min_digits <= 0 and str is NULL. |
| 446 | */ |
| 447 | bool osmo_is_hexstr(const char *str, int min_digits, int max_digits, |
| 448 | bool require_even) |
| 449 | { |
| 450 | int len; |
| 451 | /* Use unsigned char * to avoid a compiler warning of |
| 452 | * "error: array subscript has type 'char' [-Werror=char-subscripts]" */ |
| 453 | const unsigned char *pos = (const unsigned char*)str; |
| 454 | if (!pos) |
| 455 | return min_digits < 1; |
| 456 | for (len = 0; *pos && len < max_digits; len++, pos++) |
| 457 | if (!isxdigit(*pos)) |
| 458 | return false; |
| 459 | if (len < min_digits) |
| 460 | return false; |
| 461 | /* With not too many digits, we should have reached *str == nul */ |
| 462 | if (*pos) |
| 463 | return false; |
| 464 | if (require_even && (len & 1)) |
| 465 | return false; |
Harald Welte | febe83c | 2017-10-03 17:41:59 +0800 | [diff] [blame] | 466 | |
| 467 | return true; |
| 468 | } |
| 469 | |
| 470 | /*! Determine if a given identifier is valid, i.e. doesn't contain illegal chars |
| 471 | * \param[in] str String to validate |
Neels Hofmeyr | 937ddea | 2017-12-16 00:46:50 +0100 | [diff] [blame] | 472 | * \param[in] sep_chars Permitted separation characters between identifiers. |
| 473 | * \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] | 474 | */ |
Neels Hofmeyr | 937ddea | 2017-12-16 00:46:50 +0100 | [diff] [blame] | 475 | bool osmo_separated_identifiers_valid(const char *str, const char *sep_chars) |
Harald Welte | febe83c | 2017-10-03 17:41:59 +0800 | [diff] [blame] | 476 | { |
| 477 | /* characters that are illegal in names */ |
| 478 | static const char illegal_chars[] = "., {}[]()<>|~\\^`'\"?=;/+*&%$#!"; |
| 479 | unsigned int i; |
Neels Hofmeyr | 937ddea | 2017-12-16 00:46:50 +0100 | [diff] [blame] | 480 | size_t len; |
Harald Welte | febe83c | 2017-10-03 17:41:59 +0800 | [diff] [blame] | 481 | |
| 482 | /* an empty string is not a valid identifier */ |
Neels Hofmeyr | 937ddea | 2017-12-16 00:46:50 +0100 | [diff] [blame] | 483 | if (!str || (len = strlen(str)) == 0) |
Harald Welte | febe83c | 2017-10-03 17:41:59 +0800 | [diff] [blame] | 484 | return false; |
| 485 | |
Neels Hofmeyr | 937ddea | 2017-12-16 00:46:50 +0100 | [diff] [blame] | 486 | for (i = 0; i < len; i++) { |
| 487 | if (sep_chars && strchr(sep_chars, str[i])) |
| 488 | continue; |
Harald Welte | febe83c | 2017-10-03 17:41:59 +0800 | [diff] [blame] | 489 | /* check for 7-bit ASCII */ |
| 490 | if (str[i] & 0x80) |
| 491 | return false; |
Neels Hofmeyr | e5a2bdb | 2017-12-16 04:54:37 +0100 | [diff] [blame] | 492 | if (!isprint((int)str[i])) |
| 493 | return false; |
Harald Welte | febe83c | 2017-10-03 17:41:59 +0800 | [diff] [blame] | 494 | /* check for some explicit reserved control characters */ |
| 495 | if (strchr(illegal_chars, str[i])) |
| 496 | return false; |
| 497 | } |
| 498 | |
Neels Hofmeyr | 4335bad | 2017-10-07 04:39:14 +0200 | [diff] [blame] | 499 | return true; |
| 500 | } |
| 501 | |
Neels Hofmeyr | 937ddea | 2017-12-16 00:46:50 +0100 | [diff] [blame] | 502 | /*! Determine if a given identifier is valid, i.e. doesn't contain illegal chars |
| 503 | * \param[in] str String to validate |
| 504 | * \returns true in case \a str contains valid identifier, false otherwise |
| 505 | */ |
| 506 | bool osmo_identifier_valid(const char *str) |
| 507 | { |
| 508 | return osmo_separated_identifiers_valid(str, NULL); |
| 509 | } |
| 510 | |
Neels Hofmeyr | 9910bbc | 2017-12-16 00:54:52 +0100 | [diff] [blame] | 511 | /*! Return the string with all non-printable characters escaped. |
| 512 | * \param[in] str A string that may contain any characters. |
| 513 | * \param[in] len Pass -1 to print until nul char, or >= 0 to force a length. |
| 514 | * \param[inout] buf string buffer to write escaped characters to. |
| 515 | * \param[in] bufsize size of \a buf. |
| 516 | * \returns buf containing an escaped representation, possibly truncated, or str itself. |
| 517 | */ |
| 518 | const char *osmo_escape_str_buf(const char *str, int in_len, char *buf, size_t bufsize) |
| 519 | { |
| 520 | int in_pos = 0; |
| 521 | int next_unprintable = 0; |
| 522 | int out_pos = 0; |
| 523 | char *out = buf; |
| 524 | /* -1 to leave space for a final \0 */ |
| 525 | int out_len = bufsize-1; |
| 526 | |
| 527 | if (!str) |
| 528 | return "(null)"; |
| 529 | |
| 530 | if (in_len < 0) |
| 531 | in_len = strlen(str); |
| 532 | |
| 533 | while (in_pos < in_len) { |
| 534 | for (next_unprintable = in_pos; |
| 535 | next_unprintable < in_len && isprint((int)str[next_unprintable]) |
| 536 | && str[next_unprintable] != '"' |
| 537 | && str[next_unprintable] != '\\'; |
| 538 | next_unprintable++); |
| 539 | |
| 540 | if (next_unprintable == in_len |
| 541 | && in_pos == 0) |
| 542 | return str; |
| 543 | |
| 544 | while (in_pos < next_unprintable && out_pos < out_len) |
| 545 | out[out_pos++] = str[in_pos++]; |
| 546 | |
| 547 | if (out_pos == out_len || in_pos == in_len) |
| 548 | goto done; |
| 549 | |
| 550 | switch (str[next_unprintable]) { |
| 551 | #define BACKSLASH_CASE(c, repr) \ |
| 552 | case c: \ |
| 553 | if (out_pos > out_len-2) \ |
| 554 | goto done; \ |
| 555 | out[out_pos++] = '\\'; \ |
| 556 | out[out_pos++] = repr; \ |
| 557 | break |
| 558 | |
| 559 | BACKSLASH_CASE('\n', 'n'); |
| 560 | BACKSLASH_CASE('\r', 'r'); |
| 561 | BACKSLASH_CASE('\t', 't'); |
| 562 | BACKSLASH_CASE('\0', '0'); |
| 563 | BACKSLASH_CASE('\a', 'a'); |
| 564 | BACKSLASH_CASE('\b', 'b'); |
| 565 | BACKSLASH_CASE('\v', 'v'); |
| 566 | BACKSLASH_CASE('\f', 'f'); |
| 567 | BACKSLASH_CASE('\\', '\\'); |
| 568 | BACKSLASH_CASE('"', '"'); |
| 569 | #undef BACKSLASH_CASE |
| 570 | |
| 571 | default: |
| 572 | out_pos += snprintf(&out[out_pos], out_len - out_pos, "\\%u", (unsigned char)str[in_pos]); |
| 573 | if (out_pos > out_len) { |
| 574 | out_pos = out_len; |
| 575 | goto done; |
| 576 | } |
| 577 | break; |
| 578 | } |
| 579 | in_pos ++; |
| 580 | } |
| 581 | |
| 582 | done: |
| 583 | out[out_pos] = '\0'; |
| 584 | return out; |
| 585 | } |
| 586 | |
| 587 | /*! Return the string with all non-printable characters escaped. |
| 588 | * Call osmo_escape_str_buf() with a static buffer. |
| 589 | * \param[in] str A string that may contain any characters. |
| 590 | * \param[in] len Pass -1 to print until nul char, or >= 0 to force a length. |
| 591 | * \returns buf containing an escaped representation, possibly truncated, or str itself. |
| 592 | */ |
| 593 | const char *osmo_escape_str(const char *str, int in_len) |
| 594 | { |
| 595 | return osmo_escape_str_buf(str, in_len, namebuf, sizeof(namebuf)); |
| 596 | } |
| 597 | |
Neels Hofmeyr | 04eb56f | 2018-04-09 00:41:28 +0200 | [diff] [blame] | 598 | /*! Like osmo_escape_str(), but returns double-quotes around a string, or "NULL" for a NULL string. |
| 599 | * This allows passing any char* value and get its C representation as string. |
| 600 | * \param[in] str A string that may contain any characters. |
Neels Hofmeyr | 03e7553 | 2018-09-07 03:12:05 +0200 | [diff] [blame] | 601 | * \param[in] in_len Pass -1 to print until nul char, or >= 0 to force a length. |
| 602 | * \returns buf containing a quoted and escaped representation, possibly truncated. |
Neels Hofmeyr | 04eb56f | 2018-04-09 00:41:28 +0200 | [diff] [blame] | 603 | */ |
| 604 | const char *osmo_quote_str_buf(const char *str, int in_len, char *buf, size_t bufsize) |
| 605 | { |
| 606 | const char *res; |
| 607 | int l; |
| 608 | if (!str) |
| 609 | return "NULL"; |
| 610 | if (bufsize < 3) |
| 611 | return "<buf-too-small>"; |
| 612 | buf[0] = '"'; |
| 613 | res = osmo_escape_str_buf(str, in_len, buf + 1, bufsize - 2); |
| 614 | /* if osmo_escape_str_buf() returned the str itself, we need to copy it to buf to be able to |
| 615 | * quote it. */ |
| 616 | if (res == str) { |
| 617 | /* max_len = bufsize - two quotes - nul term */ |
| 618 | int max_len = bufsize - 2 - 1; |
| 619 | if (in_len >= 0) |
| 620 | max_len = OSMO_MIN(in_len, max_len); |
| 621 | /* It is not allowed to pass unterminated strings into osmo_strlcpy() :/ */ |
| 622 | strncpy(buf + 1, str, max_len); |
| 623 | buf[1 + max_len] = '\0'; |
| 624 | } |
| 625 | l = strlen(buf); |
| 626 | buf[l] = '"'; |
| 627 | buf[l+1] = '\0'; /* both osmo_escape_str_buf() and max_len above ensure room for '\0' */ |
| 628 | return buf; |
| 629 | } |
| 630 | |
Neels Hofmeyr | 03e7553 | 2018-09-07 03:12:05 +0200 | [diff] [blame] | 631 | /*! Like osmo_quote_str_buf() but returns the result in a static buffer. |
| 632 | * The static buffer is shared with get_value_string() and osmo_escape_str(). |
| 633 | * \param[in] str A string that may contain any characters. |
| 634 | * \param[in] in_len Pass -1 to print until nul char, or >= 0 to force a length. |
| 635 | * \returns static buffer containing a quoted and escaped representation, possibly truncated. |
| 636 | */ |
Neels Hofmeyr | 04eb56f | 2018-04-09 00:41:28 +0200 | [diff] [blame] | 637 | const char *osmo_quote_str(const char *str, int in_len) |
| 638 | { |
| 639 | return osmo_quote_str_buf(str, in_len, namebuf, sizeof(namebuf)); |
| 640 | } |
| 641 | |
Harald Welte | 15a5f8d | 2018-06-06 16:58:17 +0200 | [diff] [blame] | 642 | /*! perform an integer square root operation on unsigned 32bit integer. |
| 643 | * This implementation is taken from "Hacker's Delight" Figure 11-1 "Integer square root, Newton's |
| 644 | * method", which can also be found at http://www.hackersdelight.org/hdcodetxt/isqrt.c.txt */ |
| 645 | uint32_t osmo_isqrt32(uint32_t x) |
| 646 | { |
| 647 | uint32_t x1; |
| 648 | int s, g0, g1; |
| 649 | |
| 650 | if (x <= 1) |
| 651 | return x; |
| 652 | |
| 653 | s = 1; |
| 654 | x1 = x - 1; |
| 655 | if (x1 > 0xffff) { |
| 656 | s = s + 8; |
| 657 | x1 = x1 >> 16; |
| 658 | } |
| 659 | if (x1 > 0xff) { |
| 660 | s = s + 4; |
| 661 | x1 = x1 >> 8; |
| 662 | } |
| 663 | if (x1 > 0xf) { |
| 664 | s = s + 2; |
| 665 | x1 = x1 >> 4; |
| 666 | } |
| 667 | if (x1 > 0x3) { |
| 668 | s = s + 1; |
| 669 | } |
| 670 | |
| 671 | g0 = 1 << s; /* g0 = 2**s */ |
| 672 | g1 = (g0 + (x >> s)) >> 1; /* g1 = (g0 + x/g0)/2 */ |
| 673 | |
| 674 | /* converges after four to five divisions for arguments up to 16,785,407 */ |
| 675 | while (g1 < g0) { |
| 676 | g0 = g1; |
| 677 | g1 = (g0 + (x/g0)) >> 1; |
| 678 | } |
| 679 | return g0; |
| 680 | } |
| 681 | |
Neels Hofmeyr | 7c74989 | 2018-09-07 03:01:38 +0200 | [diff] [blame] | 682 | /*! Convert a string to lowercase, while checking buffer size boundaries. |
| 683 | * The result written to \a dest is guaranteed to be nul terminated if \a dest_len > 0. |
| 684 | * If dest == src, the string is converted in-place, if necessary truncated at dest_len - 1 characters |
| 685 | * length as well as nul terminated. |
| 686 | * Note: similar osmo_str2lower(), but safe to use for src strings of arbitrary length. |
| 687 | * \param[out] dest Target buffer to write lowercase string. |
| 688 | * \param[in] dest_len Maximum buffer size of dest (e.g. sizeof(dest)). |
| 689 | * \param[in] src String to convert to lowercase. |
| 690 | * \returns Length of \a src, like osmo_strlcpy(), but if \a dest == \a src at most \a dest_len - 1. |
| 691 | */ |
| 692 | size_t osmo_str_tolower_buf(char *dest, size_t dest_len, const char *src) |
| 693 | { |
| 694 | size_t rc; |
| 695 | if (dest == src) { |
| 696 | if (dest_len < 1) |
| 697 | return 0; |
| 698 | dest[dest_len - 1] = '\0'; |
| 699 | rc = strlen(dest); |
| 700 | } else { |
| 701 | if (dest_len < 1) |
| 702 | return strlen(src); |
| 703 | rc = osmo_strlcpy(dest, src, dest_len); |
| 704 | } |
| 705 | for (; *dest; dest++) |
| 706 | *dest = tolower(*dest); |
| 707 | return rc; |
| 708 | } |
| 709 | |
| 710 | /*! Convert a string to lowercase, using a static buffer. |
| 711 | * The resulting string may be truncated if the internally used static buffer is shorter than src. |
| 712 | * The internal buffer is at least 128 bytes long, i.e. guaranteed to hold at least 127 characters and a |
| 713 | * terminating nul. |
| 714 | * See also osmo_str_tolower_buf(). |
| 715 | * \param[in] src String to convert to lowercase. |
| 716 | * \returns Resulting lowercase string in a static buffer, always nul terminated. |
| 717 | */ |
| 718 | const char *osmo_str_tolower(const char *src) |
| 719 | { |
| 720 | static char buf[128]; |
| 721 | osmo_str_tolower_buf(buf, sizeof(buf), src); |
| 722 | return buf; |
| 723 | } |
| 724 | |
| 725 | /*! Convert a string to uppercase, while checking buffer size boundaries. |
| 726 | * The result written to \a dest is guaranteed to be nul terminated if \a dest_len > 0. |
| 727 | * If dest == src, the string is converted in-place, if necessary truncated at dest_len - 1 characters |
| 728 | * length as well as nul terminated. |
| 729 | * Note: similar osmo_str2upper(), but safe to use for src strings of arbitrary length. |
| 730 | * \param[out] dest Target buffer to write uppercase string. |
| 731 | * \param[in] dest_len Maximum buffer size of dest (e.g. sizeof(dest)). |
| 732 | * \param[in] src String to convert to uppercase. |
| 733 | * \returns Length of \a src, like osmo_strlcpy(), but if \a dest == \a src at most \a dest_len - 1. |
| 734 | */ |
| 735 | size_t osmo_str_toupper_buf(char *dest, size_t dest_len, const char *src) |
| 736 | { |
| 737 | size_t rc; |
| 738 | if (dest == src) { |
| 739 | if (dest_len < 1) |
| 740 | return 0; |
| 741 | dest[dest_len - 1] = '\0'; |
| 742 | rc = strlen(dest); |
| 743 | } else { |
| 744 | if (dest_len < 1) |
| 745 | return strlen(src); |
| 746 | rc = osmo_strlcpy(dest, src, dest_len); |
| 747 | } |
| 748 | for (; *dest; dest++) |
| 749 | *dest = toupper(*dest); |
| 750 | return rc; |
| 751 | } |
| 752 | |
| 753 | /*! Convert a string to uppercase, using a static buffer. |
| 754 | * The resulting string may be truncated if the internally used static buffer is shorter than src. |
| 755 | * The internal buffer is at least 128 bytes long, i.e. guaranteed to hold at least 127 characters and a |
| 756 | * terminating nul. |
| 757 | * See also osmo_str_toupper_buf(). |
| 758 | * \param[in] src String to convert to uppercase. |
| 759 | * \returns Resulting uppercase string in a static buffer, always nul terminated. |
| 760 | */ |
| 761 | const char *osmo_str_toupper(const char *src) |
| 762 | { |
| 763 | static char buf[128]; |
| 764 | osmo_str_toupper_buf(buf, sizeof(buf), src); |
| 765 | return buf; |
| 766 | } |
| 767 | |
Oliver Smith | 894be2d | 2019-01-11 13:13:37 +0100 | [diff] [blame] | 768 | /*! Calculate the Luhn checksum (as used for IMEIs). |
| 769 | * \param[in] in Input digits in ASCII string representation. |
| 770 | * \param[in] in_len Count of digits to use for the input (14 for IMEI). |
| 771 | * \returns checksum char (e.g. '3'); negative on error |
| 772 | */ |
| 773 | const char osmo_luhn(const char* in, int in_len) |
| 774 | { |
| 775 | int i, sum = 0; |
| 776 | |
| 777 | /* All input must be numbers */ |
| 778 | for (i = 0; i < in_len; i++) { |
| 779 | if (!isdigit(in[i])) |
| 780 | return -EINVAL; |
| 781 | } |
| 782 | |
| 783 | /* Double every second digit and add it to sum */ |
| 784 | for (i = in_len - 1; i >= 0; i -= 2) { |
| 785 | int dbl = (in[i] - '0') * 2; |
| 786 | if (dbl > 9) |
| 787 | dbl -= 9; |
| 788 | sum += dbl; |
| 789 | } |
| 790 | |
| 791 | /* Add other digits to sum */ |
| 792 | for (i = in_len - 2; i >= 0; i -= 2) |
| 793 | sum += in[i] - '0'; |
| 794 | |
| 795 | /* Final checksum */ |
| 796 | return (sum * 9) % 10 + '0'; |
| 797 | } |
| 798 | |
Neels Hofmeyr | 0aeda1b | 2017-01-13 14:16:02 +0100 | [diff] [blame] | 799 | /*! @} */ |