Harald Welte | 468b643 | 2014-09-11 13:05:51 +0800 | [diff] [blame] | 1 | /* |
| 2 | * (C) 2011 by Harald Welte <laforge@gnumonks.org> |
| 3 | * (C) 2011 by Sylvain Munaut <tnt@246tNt.com> |
| 4 | * (C) 2014 by Nils O. SelÄsdal <noselasd@fiane.dyndns.org> |
| 5 | * |
| 6 | * All Rights Reserved |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License along |
| 19 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 21 | * |
| 22 | */ |
| 23 | |
Harald Welte | d284cd9 | 2010-03-01 21:58:31 +0100 | [diff] [blame] | 24 | |
| 25 | #include <string.h> |
| 26 | #include <stdint.h> |
| 27 | #include <errno.h> |
Harald Welte | b59f935 | 2010-03-25 11:37:04 +0800 | [diff] [blame] | 28 | #include <stdio.h> |
Harald Welte | d284cd9 | 2010-03-01 21:58:31 +0100 | [diff] [blame] | 29 | |
Pablo Neira Ayuso | 8341934 | 2011-03-22 16:36:13 +0100 | [diff] [blame] | 30 | #include <osmocom/core/utils.h> |
Harald Welte | 9709b2e | 2016-04-25 18:47:53 +0200 | [diff] [blame] | 31 | #include <osmocom/core/bit64gen.h> |
| 32 | |
Harald Welte | d284cd9 | 2010-03-01 21:58:31 +0100 | [diff] [blame] | 33 | |
Harald Welte | 8598f18 | 2011-08-17 14:19:27 +0200 | [diff] [blame] | 34 | /*! \addtogroup utils |
| 35 | * @{ |
Harald Welte | 96e2a00 | 2017-06-12 21:44:18 +0200 | [diff] [blame] | 36 | * \brief various utility routines |
Harald Welte | 8598f18 | 2011-08-17 14:19:27 +0200 | [diff] [blame] | 37 | */ |
| 38 | |
| 39 | /*! \file utils.c */ |
| 40 | |
Harald Welte | b59f935 | 2010-03-25 11:37:04 +0800 | [diff] [blame] | 41 | static char namebuf[255]; |
Harald Welte | 8598f18 | 2011-08-17 14:19:27 +0200 | [diff] [blame] | 42 | |
| 43 | /*! \brief get human-readable string for given value |
| 44 | * \param[in] vs Array of value_string tuples |
| 45 | * \param[in] val Value to be converted |
| 46 | * \returns pointer to human-readable string |
Neels Hofmeyr | 8a3c83e | 2016-06-13 13:16:58 +0200 | [diff] [blame] | 47 | * |
| 48 | * If val is found in vs, the array's string entry is returned. Otherwise, an |
| 49 | * "unknown" string containing the actual value is composed in a static buffer |
| 50 | * that is reused across invocations. |
Harald Welte | 8598f18 | 2011-08-17 14:19:27 +0200 | [diff] [blame] | 51 | */ |
Harald Welte | d284cd9 | 2010-03-01 21:58:31 +0100 | [diff] [blame] | 52 | const char *get_value_string(const struct value_string *vs, uint32_t val) |
| 53 | { |
Neels Hofmeyr | 8d6dcd9 | 2016-06-06 18:05:23 +0200 | [diff] [blame] | 54 | const char *str = get_value_string_or_null(vs, val); |
| 55 | if (str) |
| 56 | return str; |
| 57 | |
| 58 | snprintf(namebuf, sizeof(namebuf), "unknown 0x%x", val); |
| 59 | namebuf[sizeof(namebuf) - 1] = '\0'; |
| 60 | return namebuf; |
| 61 | } |
| 62 | |
| 63 | /*! \brief get human-readable string or NULL for given value |
| 64 | * \param[in] vs Array of value_string tuples |
| 65 | * \param[in] val Value to be converted |
| 66 | * \returns pointer to human-readable string or NULL if val is not found |
| 67 | */ |
| 68 | const char *get_value_string_or_null(const struct value_string *vs, |
| 69 | uint32_t val) |
| 70 | { |
Harald Welte | d284cd9 | 2010-03-01 21:58:31 +0100 | [diff] [blame] | 71 | int i; |
| 72 | |
| 73 | for (i = 0;; i++) { |
| 74 | if (vs[i].value == 0 && vs[i].str == NULL) |
| 75 | break; |
| 76 | if (vs[i].value == val) |
| 77 | return vs[i].str; |
| 78 | } |
Harald Welte | b59f935 | 2010-03-25 11:37:04 +0800 | [diff] [blame] | 79 | |
Neels Hofmeyr | 8d6dcd9 | 2016-06-06 18:05:23 +0200 | [diff] [blame] | 80 | return NULL; |
Harald Welte | d284cd9 | 2010-03-01 21:58:31 +0100 | [diff] [blame] | 81 | } |
| 82 | |
Harald Welte | 8598f18 | 2011-08-17 14:19:27 +0200 | [diff] [blame] | 83 | /*! \brief get numeric value for given human-readable string |
| 84 | * \param[in] vs Array of value_string tuples |
| 85 | * \param[in] str human-readable string |
| 86 | * \returns numeric value (>0) or negative numer in case of error |
| 87 | */ |
Harald Welte | d284cd9 | 2010-03-01 21:58:31 +0100 | [diff] [blame] | 88 | int get_string_value(const struct value_string *vs, const char *str) |
| 89 | { |
| 90 | int i; |
| 91 | |
| 92 | for (i = 0;; i++) { |
| 93 | if (vs[i].value == 0 && vs[i].str == NULL) |
| 94 | break; |
| 95 | if (!strcasecmp(vs[i].str, str)) |
| 96 | return vs[i].value; |
| 97 | } |
| 98 | return -EINVAL; |
| 99 | } |
Harald Welte | a73e2f9 | 2010-03-04 10:50:32 +0100 | [diff] [blame] | 100 | |
Harald Welte | 8598f18 | 2011-08-17 14:19:27 +0200 | [diff] [blame] | 101 | /*! \brief Convert BCD-encoded digit into printable character |
| 102 | * \param[in] bcd A single BCD-encoded digit |
| 103 | * \returns single printable character |
| 104 | */ |
Pablo Neira Ayuso | 87f7b25 | 2011-05-07 12:43:08 +0200 | [diff] [blame] | 105 | char osmo_bcd2char(uint8_t bcd) |
Harald Welte | a73e2f9 | 2010-03-04 10:50:32 +0100 | [diff] [blame] | 106 | { |
| 107 | if (bcd < 0xa) |
| 108 | return '0' + bcd; |
| 109 | else |
| 110 | return 'A' + (bcd - 0xa); |
| 111 | } |
| 112 | |
Harald Welte | de6e498 | 2012-12-06 21:25:27 +0100 | [diff] [blame] | 113 | /*! \brief Convert number in ASCII to BCD value |
| 114 | * \param[in] c ASCII character |
| 115 | * \returns BCD encoded value of character |
| 116 | */ |
Pablo Neira Ayuso | 87f7b25 | 2011-05-07 12:43:08 +0200 | [diff] [blame] | 117 | uint8_t osmo_char2bcd(char c) |
Harald Welte | a73e2f9 | 2010-03-04 10:50:32 +0100 | [diff] [blame] | 118 | { |
| 119 | return c - 0x30; |
| 120 | } |
Harald Welte | 3eba991 | 2010-07-30 10:37:29 +0200 | [diff] [blame] | 121 | |
Neels Hofmeyr | b6d58e8 | 2016-12-21 14:35:46 +0100 | [diff] [blame] | 122 | /*! \brief Parse a string containing hexadecimal digits |
Harald Welte | de6e498 | 2012-12-06 21:25:27 +0100 | [diff] [blame] | 123 | * \param[in] str string containing ASCII encoded hexadecimal digits |
| 124 | * \param[out] b output buffer |
| 125 | * \param[in] max_len maximum space in output buffer |
Neels Hofmeyr | 3de7b05 | 2015-09-23 23:16:53 +0200 | [diff] [blame] | 126 | * \returns number of parsed octets, or -1 on error |
Harald Welte | de6e498 | 2012-12-06 21:25:27 +0100 | [diff] [blame] | 127 | */ |
Pablo Neira Ayuso | 87f7b25 | 2011-05-07 12:43:08 +0200 | [diff] [blame] | 128 | int osmo_hexparse(const char *str, uint8_t *b, int max_len) |
Harald Welte | 3eba991 | 2010-07-30 10:37:29 +0200 | [diff] [blame] | 129 | |
| 130 | { |
Neels Hofmeyr | 437ed4a | 2017-02-14 15:54:31 +0100 | [diff] [blame] | 131 | char c; |
| 132 | uint8_t v; |
| 133 | const char *strpos; |
| 134 | unsigned int nibblepos = 0; |
Harald Welte | 3eba991 | 2010-07-30 10:37:29 +0200 | [diff] [blame] | 135 | |
| 136 | memset(b, 0x00, max_len); |
| 137 | |
Neels Hofmeyr | 437ed4a | 2017-02-14 15:54:31 +0100 | [diff] [blame] | 138 | for (strpos = str; (c = *strpos); strpos++) { |
| 139 | /* skip whitespace */ |
| 140 | if (c == ' ' || c == '\t' || c == '\n' || c == '\r') |
| 141 | continue; |
| 142 | |
| 143 | /* If the buffer is too small, error out */ |
| 144 | if (nibblepos >= (max_len << 1)) |
| 145 | return -1; |
| 146 | |
Harald Welte | 3eba991 | 2010-07-30 10:37:29 +0200 | [diff] [blame] | 147 | if (c >= '0' && c <= '9') |
| 148 | v = c - '0'; |
| 149 | else if (c >= 'a' && c <= 'f') |
| 150 | v = 10 + (c - 'a'); |
| 151 | else if (c >= 'A' && c <= 'F') |
| 152 | v = 10 + (c - 'A'); |
| 153 | else |
| 154 | return -1; |
Neels Hofmeyr | 437ed4a | 2017-02-14 15:54:31 +0100 | [diff] [blame] | 155 | |
| 156 | b[nibblepos >> 1] |= v << (nibblepos & 1 ? 0 : 4); |
| 157 | nibblepos ++; |
Harald Welte | 3eba991 | 2010-07-30 10:37:29 +0200 | [diff] [blame] | 158 | } |
| 159 | |
Neels Hofmeyr | 437ed4a | 2017-02-14 15:54:31 +0100 | [diff] [blame] | 160 | /* In case of uneven amount of digits, the last byte is not complete |
| 161 | * and that's an error. */ |
| 162 | if (nibblepos & 1) |
| 163 | return -1; |
| 164 | |
| 165 | return nibblepos >> 1; |
Harald Welte | 3eba991 | 2010-07-30 10:37:29 +0200 | [diff] [blame] | 166 | } |
Harald Welte | 40481e8 | 2010-07-30 11:40:32 +0200 | [diff] [blame] | 167 | |
| 168 | static char hexd_buff[4096]; |
Nils O. SelÄsdal | 3244702 | 2014-01-02 14:04:43 +0100 | [diff] [blame] | 169 | static const char hex_chars[] = "0123456789abcdef"; |
Harald Welte | 40481e8 | 2010-07-30 11:40:32 +0200 | [diff] [blame] | 170 | |
Pablo Neira Ayuso | 87f7b25 | 2011-05-07 12:43:08 +0200 | [diff] [blame] | 171 | static char *_osmo_hexdump(const unsigned char *buf, int len, char *delim) |
Harald Welte | 40481e8 | 2010-07-30 11:40:32 +0200 | [diff] [blame] | 172 | { |
| 173 | int i; |
| 174 | char *cur = hexd_buff; |
| 175 | |
| 176 | hexd_buff[0] = 0; |
| 177 | for (i = 0; i < len; i++) { |
Nils O. SelÄsdal | 3244702 | 2014-01-02 14:04:43 +0100 | [diff] [blame] | 178 | const char *delimp = delim; |
Harald Welte | 40481e8 | 2010-07-30 11:40:32 +0200 | [diff] [blame] | 179 | int len_remain = sizeof(hexd_buff) - (cur - hexd_buff); |
Nils O. SelÄsdal | 3244702 | 2014-01-02 14:04:43 +0100 | [diff] [blame] | 180 | if (len_remain < 3) |
Holger Hans Peter Freyther | 128d9e2 | 2011-07-15 16:07:23 +0200 | [diff] [blame] | 181 | break; |
Nils O. SelÄsdal | 3244702 | 2014-01-02 14:04:43 +0100 | [diff] [blame] | 182 | |
| 183 | *cur++ = hex_chars[buf[i] >> 4]; |
| 184 | *cur++ = hex_chars[buf[i] & 0xf]; |
| 185 | |
| 186 | while (len_remain > 1 && *delimp) { |
| 187 | *cur++ = *delimp++; |
| 188 | len_remain--; |
| 189 | } |
| 190 | |
| 191 | *cur = 0; |
Harald Welte | 40481e8 | 2010-07-30 11:40:32 +0200 | [diff] [blame] | 192 | } |
| 193 | hexd_buff[sizeof(hexd_buff)-1] = 0; |
| 194 | return hexd_buff; |
| 195 | } |
Harald Welte | dee47cd | 2010-07-30 11:43:30 +0200 | [diff] [blame] | 196 | |
Harald Welte | 8598f18 | 2011-08-17 14:19:27 +0200 | [diff] [blame] | 197 | /*! \brief Convert a sequence of unpacked bits to ASCII string |
| 198 | * \param[in] bits A sequence of unpacked bits |
| 199 | * \param[in] len Length of bits |
| 200 | */ |
Pablo Neira Ayuso | 87f7b25 | 2011-05-07 12:43:08 +0200 | [diff] [blame] | 201 | char *osmo_ubit_dump(const uint8_t *bits, unsigned int len) |
Harald Welte | 3d0ac5e | 2011-02-08 16:55:03 +0100 | [diff] [blame] | 202 | { |
| 203 | int i; |
| 204 | |
| 205 | if (len > sizeof(hexd_buff)-1) |
| 206 | len = sizeof(hexd_buff)-1; |
| 207 | memset(hexd_buff, 0, sizeof(hexd_buff)); |
| 208 | |
| 209 | for (i = 0; i < len; i++) { |
| 210 | char outch; |
| 211 | switch (bits[i]) { |
| 212 | case 0: |
| 213 | outch = '0'; |
| 214 | break; |
| 215 | case 0xff: |
| 216 | outch = '?'; |
| 217 | break; |
| 218 | case 1: |
| 219 | outch = '1'; |
| 220 | break; |
| 221 | default: |
| 222 | outch = 'E'; |
| 223 | break; |
| 224 | } |
| 225 | hexd_buff[i] = outch; |
| 226 | } |
| 227 | hexd_buff[sizeof(hexd_buff)-1] = 0; |
| 228 | return hexd_buff; |
| 229 | } |
| 230 | |
Harald Welte | 8598f18 | 2011-08-17 14:19:27 +0200 | [diff] [blame] | 231 | /*! \brief Convert binary sequence to hexadecimal ASCII string |
| 232 | * \param[in] buf pointer to sequence of bytes |
| 233 | * \param[in] len length of buf in number of bytes |
| 234 | * \returns pointer to zero-terminated string |
| 235 | * |
| 236 | * This function will print a sequence of bytes as hexadecimal numbers, |
| 237 | * adding one space character between each byte (e.g. "1a ef d9") |
| 238 | */ |
Pablo Neira Ayuso | 87f7b25 | 2011-05-07 12:43:08 +0200 | [diff] [blame] | 239 | char *osmo_hexdump(const unsigned char *buf, int len) |
Harald Welte | dee47cd | 2010-07-30 11:43:30 +0200 | [diff] [blame] | 240 | { |
Pablo Neira Ayuso | 87f7b25 | 2011-05-07 12:43:08 +0200 | [diff] [blame] | 241 | return _osmo_hexdump(buf, len, " "); |
Harald Welte | dee47cd | 2010-07-30 11:43:30 +0200 | [diff] [blame] | 242 | } |
| 243 | |
Harald Welte | 8598f18 | 2011-08-17 14:19:27 +0200 | [diff] [blame] | 244 | /*! \brief Convert binary sequence to hexadecimal ASCII string |
| 245 | * \param[in] buf pointer to sequence of bytes |
| 246 | * \param[in] len length of buf in number of bytes |
| 247 | * \returns pointer to zero-terminated string |
| 248 | * |
| 249 | * This function will print a sequence of bytes as hexadecimal numbers, |
| 250 | * without any space character between each byte (e.g. "1aefd9") |
| 251 | */ |
Sylvain Munaut | ff23d24 | 2011-11-10 23:03:18 +0100 | [diff] [blame] | 252 | char *osmo_hexdump_nospc(const unsigned char *buf, int len) |
Harald Welte | dee47cd | 2010-07-30 11:43:30 +0200 | [diff] [blame] | 253 | { |
Pablo Neira Ayuso | 87f7b25 | 2011-05-07 12:43:08 +0200 | [diff] [blame] | 254 | return _osmo_hexdump(buf, len, ""); |
Harald Welte | dee47cd | 2010-07-30 11:43:30 +0200 | [diff] [blame] | 255 | } |
Harald Welte | 2822296 | 2011-02-18 20:37:04 +0100 | [diff] [blame] | 256 | |
Holger Hans Peter Freyther | 9a1a5a1 | 2015-04-11 19:26:55 +0200 | [diff] [blame] | 257 | /* Compat with previous typo to preserve abi */ |
Sylvain Munaut | e55ae3a | 2011-11-11 23:06:55 +0100 | [diff] [blame] | 258 | 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] | 259 | #if defined(__MACH__) && defined(__APPLE__) |
| 260 | ; |
| 261 | #else |
Sylvain Munaut | 17af41d | 2011-11-19 22:30:39 +0100 | [diff] [blame] | 262 | __attribute__((weak, alias("osmo_hexdump_nospc"))); |
Holger Hans Peter Freyther | 9a1a5a1 | 2015-04-11 19:26:55 +0200 | [diff] [blame] | 263 | #endif |
Sylvain Munaut | e55ae3a | 2011-11-11 23:06:55 +0100 | [diff] [blame] | 264 | |
Harald Welte | 2822296 | 2011-02-18 20:37:04 +0100 | [diff] [blame] | 265 | #include "../config.h" |
| 266 | #ifdef HAVE_CTYPE_H |
| 267 | #include <ctype.h> |
Harald Welte | 8598f18 | 2011-08-17 14:19:27 +0200 | [diff] [blame] | 268 | /*! \brief Convert an entire string to lower case |
| 269 | * \param[out] out output string, caller-allocated |
| 270 | * \param[in] in input string |
| 271 | */ |
Harald Welte | 2822296 | 2011-02-18 20:37:04 +0100 | [diff] [blame] | 272 | void osmo_str2lower(char *out, const char *in) |
| 273 | { |
| 274 | unsigned int i; |
| 275 | |
| 276 | for (i = 0; i < strlen(in); i++) |
| 277 | out[i] = tolower(in[i]); |
| 278 | out[strlen(in)] = '\0'; |
| 279 | } |
| 280 | |
Harald Welte | 8598f18 | 2011-08-17 14:19:27 +0200 | [diff] [blame] | 281 | /*! \brief Convert an entire string to upper case |
| 282 | * \param[out] out output string, caller-allocated |
| 283 | * \param[in] in input string |
| 284 | */ |
Harald Welte | 2822296 | 2011-02-18 20:37:04 +0100 | [diff] [blame] | 285 | void osmo_str2upper(char *out, const char *in) |
| 286 | { |
| 287 | unsigned int i; |
| 288 | |
| 289 | for (i = 0; i < strlen(in); i++) |
| 290 | out[i] = toupper(in[i]); |
| 291 | out[strlen(in)] = '\0'; |
| 292 | } |
| 293 | #endif /* HAVE_CTYPE_H */ |
Harald Welte | 8598f18 | 2011-08-17 14:19:27 +0200 | [diff] [blame] | 294 | |
Harald Welte | 9709b2e | 2016-04-25 18:47:53 +0200 | [diff] [blame] | 295 | /*! \brief Wishful thinking to generate a constant time compare |
| 296 | * \param[in] exp Expected data |
| 297 | * \param[in] rel Comparison value |
| 298 | * \param[in] count Number of bytes to compare |
| 299 | * \returns 1 in case \a exp equals \a rel; zero otherwise |
| 300 | * |
| 301 | * Compare count bytes of exp to rel. Return 0 if they are identical, 1 |
| 302 | * otherwise. Do not return a mismatch on the first mismatching byte, |
| 303 | * but always compare all bytes, regardless. The idea is that the amount of |
| 304 | * matching bytes cannot be inferred from the time the comparison took. */ |
| 305 | int osmo_constant_time_cmp(const uint8_t *exp, const uint8_t *rel, const int count) |
| 306 | { |
| 307 | int x = 0, i; |
| 308 | |
| 309 | for (i = 0; i < count; ++i) |
| 310 | x |= exp[i] ^ rel[i]; |
| 311 | |
| 312 | /* if x is zero, all data was identical */ |
| 313 | return x? 1 : 0; |
| 314 | } |
| 315 | |
| 316 | /*! \brief Generic retrieval of 1..8 bytes as big-endian uint64_t |
| 317 | * \param[in] data Input data as byte-array |
| 318 | * \param[in] data_len Length of \a data in octets |
| 319 | * \returns uint64_t of \a data interpreted as big-endian |
| 320 | * |
| 321 | * This is like osmo_load64be_ext, except that if data_len is less than |
| 322 | * sizeof(uint64_t), the data is interpreted as the least significant bytes |
| 323 | * (osmo_load64be_ext loads them as the most significant bytes into the |
| 324 | * returned uint64_t). In this way, any integer size up to 64 bits can be |
| 325 | * decoded conveniently by using sizeof(), without the need to call specific |
| 326 | * numbered functions (osmo_load16, 32, ...). */ |
| 327 | uint64_t osmo_decode_big_endian(const uint8_t *data, size_t data_len) |
| 328 | { |
| 329 | uint64_t value = 0; |
| 330 | |
| 331 | while (data_len > 0) { |
| 332 | value = (value << 8) + *data; |
| 333 | data += 1; |
| 334 | data_len -= 1; |
| 335 | } |
| 336 | |
| 337 | return value; |
| 338 | } |
| 339 | |
| 340 | /*! \brief Generic big-endian encoding of big endian number up to 64bit |
| 341 | * \param[in] value unsigned integer value to be stored |
| 342 | * \param[in] data_len number of octets |
| 343 | * \returns static buffer containing big-endian stored value |
| 344 | * |
| 345 | * This is like osmo_store64be_ext, except that this returns a static buffer of |
| 346 | * the result (for convenience, but not threadsafe). If data_len is less than |
| 347 | * sizeof(uint64_t), only the least significant bytes of value are encoded. */ |
| 348 | uint8_t *osmo_encode_big_endian(uint64_t value, size_t data_len) |
| 349 | { |
| 350 | static uint8_t buf[sizeof(uint64_t)]; |
| 351 | OSMO_ASSERT(data_len <= ARRAY_SIZE(buf)); |
| 352 | osmo_store64be_ext(value, buf, data_len); |
| 353 | return buf; |
| 354 | } |
Harald Welte | aeecc48 | 2016-11-26 10:41:40 +0100 | [diff] [blame] | 355 | |
| 356 | /*! \brief Copy a C-string into a sized buffer |
| 357 | * \param[in] src source string |
| 358 | * \param[out] dst destination string |
Neels Hofmeyr | df83ece | 2017-01-13 13:55:43 +0100 | [diff] [blame] | 359 | * \param[in] siz size of the \a dst buffer |
| 360 | * \returns length of \a src |
Harald Welte | aeecc48 | 2016-11-26 10:41:40 +0100 | [diff] [blame] | 361 | * |
Neels Hofmeyr | df83ece | 2017-01-13 13:55:43 +0100 | [diff] [blame] | 362 | * Copy at most \a siz bytes from \a src to \a dst, ensuring that the result is |
| 363 | * NUL terminated. The NUL character is included in \a siz, i.e. passing the |
| 364 | * actual sizeof(*dst) is correct. |
Harald Welte | aeecc48 | 2016-11-26 10:41:40 +0100 | [diff] [blame] | 365 | */ |
| 366 | size_t osmo_strlcpy(char *dst, const char *src, size_t siz) |
| 367 | { |
| 368 | size_t ret = strlen(src); |
| 369 | |
| 370 | if (siz) { |
| 371 | size_t len = (ret >= siz) ? siz - 1 : ret; |
| 372 | memcpy(dst, src, len); |
| 373 | dst[len] = '\0'; |
| 374 | } |
| 375 | return ret; |
| 376 | } |
Neels Hofmeyr | 0aeda1b | 2017-01-13 14:16:02 +0100 | [diff] [blame] | 377 | |
| 378 | /*! @} */ |