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> |
Pau Espin Pedrol | 4573502 | 2017-06-18 14:05:24 +0200 | [diff] [blame] | 29 | #include <inttypes.h> |
Harald Welte | d284cd9 | 2010-03-01 21:58:31 +0100 | [diff] [blame] | 30 | |
Pablo Neira Ayuso | 8341934 | 2011-03-22 16:36:13 +0100 | [diff] [blame] | 31 | #include <osmocom/core/utils.h> |
Harald Welte | 9709b2e | 2016-04-25 18:47:53 +0200 | [diff] [blame] | 32 | #include <osmocom/core/bit64gen.h> |
| 33 | |
Harald Welte | d284cd9 | 2010-03-01 21:58:31 +0100 | [diff] [blame] | 34 | |
Harald Welte | 8598f18 | 2011-08-17 14:19:27 +0200 | [diff] [blame] | 35 | /*! \addtogroup utils |
| 36 | * @{ |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 37 | * various utility routines |
Neels Hofmeyr | 17518fe | 2017-06-20 04:35:06 +0200 | [diff] [blame] | 38 | * |
| 39 | * \file utils.c */ |
Harald Welte | 8598f18 | 2011-08-17 14:19:27 +0200 | [diff] [blame] | 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 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 43 | /*! get human-readable string for given value |
Harald Welte | 8598f18 | 2011-08-17 14:19:27 +0200 | [diff] [blame] | 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 | |
Pau Espin Pedrol | 4573502 | 2017-06-18 14:05:24 +0200 | [diff] [blame] | 58 | snprintf(namebuf, sizeof(namebuf), "unknown 0x%"PRIx32, val); |
Neels Hofmeyr | 8d6dcd9 | 2016-06-06 18:05:23 +0200 | [diff] [blame] | 59 | namebuf[sizeof(namebuf) - 1] = '\0'; |
| 60 | return namebuf; |
| 61 | } |
| 62 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 63 | /*! get human-readable string or NULL for given value |
Neels Hofmeyr | 8d6dcd9 | 2016-06-06 18:05:23 +0200 | [diff] [blame] | 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 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 83 | /*! get numeric value for given human-readable string |
Harald Welte | 8598f18 | 2011-08-17 14:19:27 +0200 | [diff] [blame] | 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 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 101 | /*! Convert BCD-encoded digit into printable character |
Harald Welte | 8598f18 | 2011-08-17 14:19:27 +0200 | [diff] [blame] | 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 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 113 | /*! Convert number in ASCII to BCD value |
Harald Welte | de6e498 | 2012-12-06 21:25:27 +0100 | [diff] [blame] | 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 | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 122 | /*! 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 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 197 | /*! Convert a sequence of unpacked bits to ASCII string |
Harald Welte | 8598f18 | 2011-08-17 14:19:27 +0200 | [diff] [blame] | 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 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 231 | /*! Convert binary sequence to hexadecimal ASCII string |
Harald Welte | 8598f18 | 2011-08-17 14:19:27 +0200 | [diff] [blame] | 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") |
Harald Welte | 096a666 | 2017-10-16 14:33:11 +0200 | [diff] [blame] | 238 | * |
| 239 | * The maximum size of the output buffer is 4096 bytes, i.e. the maximum |
| 240 | * 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] | 241 | */ |
Pablo Neira Ayuso | 87f7b25 | 2011-05-07 12:43:08 +0200 | [diff] [blame] | 242 | char *osmo_hexdump(const unsigned char *buf, int len) |
Harald Welte | dee47cd | 2010-07-30 11:43:30 +0200 | [diff] [blame] | 243 | { |
Pablo Neira Ayuso | 87f7b25 | 2011-05-07 12:43:08 +0200 | [diff] [blame] | 244 | return _osmo_hexdump(buf, len, " "); |
Harald Welte | dee47cd | 2010-07-30 11:43:30 +0200 | [diff] [blame] | 245 | } |
| 246 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 247 | /*! Convert binary sequence to hexadecimal ASCII string |
Harald Welte | 8598f18 | 2011-08-17 14:19:27 +0200 | [diff] [blame] | 248 | * \param[in] buf pointer to sequence of bytes |
| 249 | * \param[in] len length of buf in number of bytes |
| 250 | * \returns pointer to zero-terminated string |
| 251 | * |
| 252 | * This function will print a sequence of bytes as hexadecimal numbers, |
| 253 | * without any space character between each byte (e.g. "1aefd9") |
Harald Welte | 096a666 | 2017-10-16 14:33:11 +0200 | [diff] [blame] | 254 | * |
| 255 | * The maximum size of the output buffer is 4096 bytes, i.e. the maximum |
| 256 | * 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] | 257 | */ |
Sylvain Munaut | ff23d24 | 2011-11-10 23:03:18 +0100 | [diff] [blame] | 258 | char *osmo_hexdump_nospc(const unsigned char *buf, int len) |
Harald Welte | dee47cd | 2010-07-30 11:43:30 +0200 | [diff] [blame] | 259 | { |
Pablo Neira Ayuso | 87f7b25 | 2011-05-07 12:43:08 +0200 | [diff] [blame] | 260 | return _osmo_hexdump(buf, len, ""); |
Harald Welte | dee47cd | 2010-07-30 11:43:30 +0200 | [diff] [blame] | 261 | } |
Harald Welte | 2822296 | 2011-02-18 20:37:04 +0100 | [diff] [blame] | 262 | |
Holger Hans Peter Freyther | 9a1a5a1 | 2015-04-11 19:26:55 +0200 | [diff] [blame] | 263 | /* Compat with previous typo to preserve abi */ |
Sylvain Munaut | e55ae3a | 2011-11-11 23:06:55 +0100 | [diff] [blame] | 264 | 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] | 265 | #if defined(__MACH__) && defined(__APPLE__) |
| 266 | ; |
| 267 | #else |
Sylvain Munaut | 17af41d | 2011-11-19 22:30:39 +0100 | [diff] [blame] | 268 | __attribute__((weak, alias("osmo_hexdump_nospc"))); |
Holger Hans Peter Freyther | 9a1a5a1 | 2015-04-11 19:26:55 +0200 | [diff] [blame] | 269 | #endif |
Sylvain Munaut | e55ae3a | 2011-11-11 23:06:55 +0100 | [diff] [blame] | 270 | |
Harald Welte | 2822296 | 2011-02-18 20:37:04 +0100 | [diff] [blame] | 271 | #include "../config.h" |
| 272 | #ifdef HAVE_CTYPE_H |
| 273 | #include <ctype.h> |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 274 | /*! Convert an entire string to lower case |
Harald Welte | 8598f18 | 2011-08-17 14:19:27 +0200 | [diff] [blame] | 275 | * \param[out] out output string, caller-allocated |
| 276 | * \param[in] in input string |
| 277 | */ |
Harald Welte | 2822296 | 2011-02-18 20:37:04 +0100 | [diff] [blame] | 278 | void osmo_str2lower(char *out, const char *in) |
| 279 | { |
| 280 | unsigned int i; |
| 281 | |
| 282 | for (i = 0; i < strlen(in); i++) |
Pau Espin Pedrol | 399a6f0 | 2017-06-18 14:07:37 +0200 | [diff] [blame] | 283 | out[i] = tolower((const unsigned char)in[i]); |
Harald Welte | 2822296 | 2011-02-18 20:37:04 +0100 | [diff] [blame] | 284 | out[strlen(in)] = '\0'; |
| 285 | } |
| 286 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 287 | /*! Convert an entire string to upper case |
Harald Welte | 8598f18 | 2011-08-17 14:19:27 +0200 | [diff] [blame] | 288 | * \param[out] out output string, caller-allocated |
| 289 | * \param[in] in input string |
| 290 | */ |
Harald Welte | 2822296 | 2011-02-18 20:37:04 +0100 | [diff] [blame] | 291 | void osmo_str2upper(char *out, const char *in) |
| 292 | { |
| 293 | unsigned int i; |
| 294 | |
| 295 | for (i = 0; i < strlen(in); i++) |
Pau Espin Pedrol | 399a6f0 | 2017-06-18 14:07:37 +0200 | [diff] [blame] | 296 | out[i] = toupper((const unsigned char)in[i]); |
Harald Welte | 2822296 | 2011-02-18 20:37:04 +0100 | [diff] [blame] | 297 | out[strlen(in)] = '\0'; |
| 298 | } |
| 299 | #endif /* HAVE_CTYPE_H */ |
Harald Welte | 8598f18 | 2011-08-17 14:19:27 +0200 | [diff] [blame] | 300 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 301 | /*! Wishful thinking to generate a constant time compare |
Harald Welte | 9709b2e | 2016-04-25 18:47:53 +0200 | [diff] [blame] | 302 | * \param[in] exp Expected data |
| 303 | * \param[in] rel Comparison value |
| 304 | * \param[in] count Number of bytes to compare |
| 305 | * \returns 1 in case \a exp equals \a rel; zero otherwise |
| 306 | * |
| 307 | * Compare count bytes of exp to rel. Return 0 if they are identical, 1 |
| 308 | * otherwise. Do not return a mismatch on the first mismatching byte, |
| 309 | * but always compare all bytes, regardless. The idea is that the amount of |
| 310 | * matching bytes cannot be inferred from the time the comparison took. */ |
| 311 | int osmo_constant_time_cmp(const uint8_t *exp, const uint8_t *rel, const int count) |
| 312 | { |
| 313 | int x = 0, i; |
| 314 | |
| 315 | for (i = 0; i < count; ++i) |
| 316 | x |= exp[i] ^ rel[i]; |
| 317 | |
| 318 | /* if x is zero, all data was identical */ |
| 319 | return x? 1 : 0; |
| 320 | } |
| 321 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 322 | /*! Generic retrieval of 1..8 bytes as big-endian uint64_t |
Harald Welte | 9709b2e | 2016-04-25 18:47:53 +0200 | [diff] [blame] | 323 | * \param[in] data Input data as byte-array |
| 324 | * \param[in] data_len Length of \a data in octets |
| 325 | * \returns uint64_t of \a data interpreted as big-endian |
| 326 | * |
| 327 | * This is like osmo_load64be_ext, except that if data_len is less than |
| 328 | * sizeof(uint64_t), the data is interpreted as the least significant bytes |
| 329 | * (osmo_load64be_ext loads them as the most significant bytes into the |
| 330 | * returned uint64_t). In this way, any integer size up to 64 bits can be |
| 331 | * decoded conveniently by using sizeof(), without the need to call specific |
| 332 | * numbered functions (osmo_load16, 32, ...). */ |
| 333 | uint64_t osmo_decode_big_endian(const uint8_t *data, size_t data_len) |
| 334 | { |
| 335 | uint64_t value = 0; |
| 336 | |
| 337 | while (data_len > 0) { |
| 338 | value = (value << 8) + *data; |
| 339 | data += 1; |
| 340 | data_len -= 1; |
| 341 | } |
| 342 | |
| 343 | return value; |
| 344 | } |
| 345 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 346 | /*! Generic big-endian encoding of big endian number up to 64bit |
Harald Welte | 9709b2e | 2016-04-25 18:47:53 +0200 | [diff] [blame] | 347 | * \param[in] value unsigned integer value to be stored |
| 348 | * \param[in] data_len number of octets |
| 349 | * \returns static buffer containing big-endian stored value |
| 350 | * |
| 351 | * This is like osmo_store64be_ext, except that this returns a static buffer of |
| 352 | * the result (for convenience, but not threadsafe). If data_len is less than |
| 353 | * sizeof(uint64_t), only the least significant bytes of value are encoded. */ |
| 354 | uint8_t *osmo_encode_big_endian(uint64_t value, size_t data_len) |
| 355 | { |
| 356 | static uint8_t buf[sizeof(uint64_t)]; |
| 357 | OSMO_ASSERT(data_len <= ARRAY_SIZE(buf)); |
| 358 | osmo_store64be_ext(value, buf, data_len); |
| 359 | return buf; |
| 360 | } |
Harald Welte | aeecc48 | 2016-11-26 10:41:40 +0100 | [diff] [blame] | 361 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 362 | /*! Copy a C-string into a sized buffer |
Harald Welte | aeecc48 | 2016-11-26 10:41:40 +0100 | [diff] [blame] | 363 | * \param[in] src source string |
| 364 | * \param[out] dst destination string |
Neels Hofmeyr | df83ece | 2017-01-13 13:55:43 +0100 | [diff] [blame] | 365 | * \param[in] siz size of the \a dst buffer |
| 366 | * \returns length of \a src |
Harald Welte | aeecc48 | 2016-11-26 10:41:40 +0100 | [diff] [blame] | 367 | * |
Neels Hofmeyr | df83ece | 2017-01-13 13:55:43 +0100 | [diff] [blame] | 368 | * Copy at most \a siz bytes from \a src to \a dst, ensuring that the result is |
| 369 | * NUL terminated. The NUL character is included in \a siz, i.e. passing the |
| 370 | * actual sizeof(*dst) is correct. |
Harald Welte | aeecc48 | 2016-11-26 10:41:40 +0100 | [diff] [blame] | 371 | */ |
| 372 | size_t osmo_strlcpy(char *dst, const char *src, size_t siz) |
| 373 | { |
| 374 | size_t ret = strlen(src); |
| 375 | |
| 376 | if (siz) { |
| 377 | size_t len = (ret >= siz) ? siz - 1 : ret; |
| 378 | memcpy(dst, src, len); |
| 379 | dst[len] = '\0'; |
| 380 | } |
| 381 | return ret; |
| 382 | } |
Neels Hofmeyr | 0aeda1b | 2017-01-13 14:16:02 +0100 | [diff] [blame] | 383 | |
Neels Hofmeyr | 4335bad | 2017-10-07 04:39:14 +0200 | [diff] [blame] | 384 | /*! Validate that a given string is a hex string within given size limits. |
| 385 | * Note that each hex digit amounts to a nibble, so if checking for a hex |
| 386 | * string to result in N bytes, pass amount of digits as 2*N. |
| 387 | * \param str A nul-terminated string to validate, or NULL. |
| 388 | * \param min_digits least permitted amount of digits. |
| 389 | * \param max_digits most permitted amount of digits. |
| 390 | * \param require_even if true, require an even amount of digits. |
| 391 | * \returns true when the hex_str contains only hexadecimal digits (no |
| 392 | * whitespace) and matches the requested length; also true |
| 393 | * when min_digits <= 0 and str is NULL. |
| 394 | */ |
| 395 | bool osmo_is_hexstr(const char *str, int min_digits, int max_digits, |
| 396 | bool require_even) |
| 397 | { |
| 398 | int len; |
| 399 | /* Use unsigned char * to avoid a compiler warning of |
| 400 | * "error: array subscript has type 'char' [-Werror=char-subscripts]" */ |
| 401 | const unsigned char *pos = (const unsigned char*)str; |
| 402 | if (!pos) |
| 403 | return min_digits < 1; |
| 404 | for (len = 0; *pos && len < max_digits; len++, pos++) |
| 405 | if (!isxdigit(*pos)) |
| 406 | return false; |
| 407 | if (len < min_digits) |
| 408 | return false; |
| 409 | /* With not too many digits, we should have reached *str == nul */ |
| 410 | if (*pos) |
| 411 | return false; |
| 412 | if (require_even && (len & 1)) |
| 413 | return false; |
| 414 | return true; |
| 415 | } |
| 416 | |
Neels Hofmeyr | 0aeda1b | 2017-01-13 14:16:02 +0100 | [diff] [blame] | 417 | /*! @} */ |