Sylvain Munaut | 12ba778 | 2014-06-16 10:13:40 +0200 | [diff] [blame] | 1 | #pragma once |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 2 | |
Neels Hofmeyr | 4335bad | 2017-10-07 04:39:14 +0200 | [diff] [blame] | 3 | #include <stdbool.h> |
| 4 | |
Harald Welte | b8add36 | 2013-07-06 23:49:41 +0200 | [diff] [blame] | 5 | #include <osmocom/core/backtrace.h> |
Harald Welte | f3c7e85 | 2014-08-20 22:50:47 +0200 | [diff] [blame] | 6 | #include <osmocom/core/talloc.h> |
Harald Welte | b8add36 | 2013-07-06 23:49:41 +0200 | [diff] [blame] | 7 | |
Harald Welte | 8598f18 | 2011-08-17 14:19:27 +0200 | [diff] [blame] | 8 | /*! \defgroup utils General-purpose utility functions |
| 9 | * @{ |
Neels Hofmeyr | 17518fe | 2017-06-20 04:35:06 +0200 | [diff] [blame] | 10 | * \file utils.h */ |
Harald Welte | 8598f18 | 2011-08-17 14:19:27 +0200 | [diff] [blame] | 11 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 12 | /*! Determine number of elements in an array of static size */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 13 | #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 14 | /*! Return the maximum of two specified values */ |
Holger Hans Peter Freyther | 08b2862 | 2012-08-15 17:02:59 +0200 | [diff] [blame] | 15 | #define OSMO_MAX(a, b) ((a) >= (b) ? (a) : (b)) |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 16 | /*! Return the minimum of two specified values */ |
Holger Hans Peter Freyther | 08b2862 | 2012-08-15 17:02:59 +0200 | [diff] [blame] | 17 | #define OSMO_MIN(a, b) ((a) >= (b) ? (b) : (a)) |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 18 | /*! Stringify the contents of a macro, e.g. a port number */ |
Neels Hofmeyr | 1808096 | 2016-12-16 13:43:54 +0100 | [diff] [blame] | 19 | #define OSMO_STRINGIFY(x) #x |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 20 | /*! Make a value_string entry from an enum value name */ |
Neels Hofmeyr | 8a5d60b | 2017-03-09 23:01:37 +0100 | [diff] [blame] | 21 | #define OSMO_VALUE_STRING(x) { x, #x } |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 22 | /*! Number of bytes necessary to store given BITS */ |
Max | dd75bac | 2017-06-13 15:07:01 +0200 | [diff] [blame] | 23 | #define OSMO_BYTES_FOR_BITS(BITS) ((BITS + 8 - 1) / 8) |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 24 | |
Harald Welte | febe83c | 2017-10-03 17:41:59 +0800 | [diff] [blame] | 25 | #include <stdbool.h> |
Harald Welte | d284cd9 | 2010-03-01 21:58:31 +0100 | [diff] [blame] | 26 | #include <stdint.h> |
Harald Welte | 75c0e2f | 2017-01-15 17:59:21 +0100 | [diff] [blame] | 27 | #include <stdio.h> |
Harald Welte | d284cd9 | 2010-03-01 21:58:31 +0100 | [diff] [blame] | 28 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 29 | /*! A mapping between human-readable string and numeric value */ |
Harald Welte | d284cd9 | 2010-03-01 21:58:31 +0100 | [diff] [blame] | 30 | struct value_string { |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 31 | unsigned int value; /*!< numeric value */ |
| 32 | const char *str; /*!< human-readable string */ |
Harald Welte | d284cd9 | 2010-03-01 21:58:31 +0100 | [diff] [blame] | 33 | }; |
| 34 | |
| 35 | const char *get_value_string(const struct value_string *vs, uint32_t val); |
Neels Hofmeyr | 8d6dcd9 | 2016-06-06 18:05:23 +0200 | [diff] [blame] | 36 | const char *get_value_string_or_null(const struct value_string *vs, |
| 37 | uint32_t val); |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame] | 38 | |
Harald Welte | d284cd9 | 2010-03-01 21:58:31 +0100 | [diff] [blame] | 39 | int get_string_value(const struct value_string *vs, const char *str); |
| 40 | |
Pablo Neira Ayuso | 87f7b25 | 2011-05-07 12:43:08 +0200 | [diff] [blame] | 41 | char osmo_bcd2char(uint8_t bcd); |
Harald Welte | a73e2f9 | 2010-03-04 10:50:32 +0100 | [diff] [blame] | 42 | /* only works for numbers in ascci */ |
Pablo Neira Ayuso | 87f7b25 | 2011-05-07 12:43:08 +0200 | [diff] [blame] | 43 | uint8_t osmo_char2bcd(char c); |
Harald Welte | d284cd9 | 2010-03-01 21:58:31 +0100 | [diff] [blame] | 44 | |
Pablo Neira Ayuso | 87f7b25 | 2011-05-07 12:43:08 +0200 | [diff] [blame] | 45 | int osmo_hexparse(const char *str, uint8_t *b, int max_len); |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame] | 46 | |
Pablo Neira Ayuso | 87f7b25 | 2011-05-07 12:43:08 +0200 | [diff] [blame] | 47 | char *osmo_ubit_dump(const uint8_t *bits, unsigned int len); |
Harald Welte | 8598f18 | 2011-08-17 14:19:27 +0200 | [diff] [blame] | 48 | char *osmo_hexdump(const unsigned char *buf, int len); |
Sylvain Munaut | ff23d24 | 2011-11-10 23:03:18 +0100 | [diff] [blame] | 49 | char *osmo_hexdump_nospc(const unsigned char *buf, int len); |
Sylvain Munaut | 4cfbae8 | 2011-11-13 23:04:00 +0100 | [diff] [blame] | 50 | char *osmo_osmo_hexdump_nospc(const unsigned char *buf, int len) __attribute__((__deprecated__)); |
Harald Welte | 3eba991 | 2010-07-30 10:37:29 +0200 | [diff] [blame] | 51 | |
Holger Hans Peter Freyther | 0f40ae1 | 2015-04-13 09:21:05 +0200 | [diff] [blame] | 52 | #define osmo_static_assert(exp, name) typedef int dummy##name [(exp) ? 1 : -1] __attribute__((__unused__)); |
Holger Hans Peter Freyther | 52c07ca | 2011-01-16 17:37:27 +0100 | [diff] [blame] | 53 | |
Harald Welte | 2822296 | 2011-02-18 20:37:04 +0100 | [diff] [blame] | 54 | void osmo_str2lower(char *out, const char *in); |
| 55 | void osmo_str2upper(char *out, const char *in); |
| 56 | |
Pablo Neira Ayuso | 3abad6a | 2011-03-28 19:24:22 +0200 | [diff] [blame] | 57 | #define OSMO_SNPRINTF_RET(ret, rem, offset, len) \ |
| 58 | do { \ |
| 59 | len += ret; \ |
| 60 | if (ret > rem) \ |
| 61 | ret = rem; \ |
| 62 | offset += ret; \ |
| 63 | rem -= ret; \ |
| 64 | } while (0) |
| 65 | |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 66 | /*! Helper macro to terminate when an assertion failes |
| 67 | * \param[in] exp Predicate to verify |
| 68 | * This function will generate a backtrace and terminate the program if |
| 69 | * the predicate evaluates to false (0). |
| 70 | */ |
Katerina Barone-Adesi | 008e53b | 2013-03-03 10:36:52 +0000 | [diff] [blame] | 71 | #define OSMO_ASSERT(exp) \ |
| 72 | if (!(exp)) { \ |
Harald Welte | 3d79240 | 2016-05-10 15:23:06 +0200 | [diff] [blame] | 73 | fprintf(stderr, "Assert failed %s %s:%d\n", #exp, __BASE_FILE__, __LINE__); \ |
Katerina Barone-Adesi | 55cf022 | 2013-03-12 10:23:52 +0100 | [diff] [blame] | 74 | osmo_generate_backtrace(); \ |
Katerina Barone-Adesi | 008e53b | 2013-03-03 10:36:52 +0000 | [diff] [blame] | 75 | abort(); \ |
| 76 | } |
| 77 | |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 78 | /*! duplicate a string using talloc and release its prior content (if any) |
| 79 | * \param[in] ctx Talloc context to use for allocation |
| 80 | * \param[out] dst pointer to string, will be updated with ptr to new string |
| 81 | * \param[in] newstr String that will be copieed to newly allocated string */ |
Harald Welte | 881dcaf | 2016-07-28 08:02:48 +0200 | [diff] [blame] | 82 | static inline void osmo_talloc_replace_string(void *ctx, char **dst, const char *newstr) |
Harald Welte | f3c7e85 | 2014-08-20 22:50:47 +0200 | [diff] [blame] | 83 | { |
| 84 | if (*dst) |
| 85 | talloc_free(*dst); |
| 86 | *dst = talloc_strdup(ctx, newstr); |
| 87 | } |
Katerina Barone-Adesi | 008e53b | 2013-03-03 10:36:52 +0000 | [diff] [blame] | 88 | |
Neels Hofmeyr | abdd7a2 | 2017-10-19 02:40:01 +0200 | [diff] [blame] | 89 | /*! Append to a string and re-/allocate if necessary. |
| 90 | * \param[in] ctx Talloc context to use for initial allocation. |
| 91 | * \param[in,out] dest char* to re-/allocate and append to. |
| 92 | * \param[in] fmt printf-like string format. |
| 93 | * \param[in] args Arguments for fmt. |
| 94 | * |
| 95 | * \a dest may be passed in NULL, or a string previously allocated by talloc. |
| 96 | * If an existing string is passed in, it will remain associated with whichever |
| 97 | * ctx it was allocated before, regardless whether it matches \a ctx or not. |
| 98 | */ |
| 99 | #define osmo_talloc_asprintf(ctx, dest, fmt, args ...) \ |
| 100 | do { \ |
| 101 | if (!dest) \ |
| 102 | dest = talloc_asprintf(ctx, fmt, ## args); \ |
| 103 | else \ |
| 104 | dest = talloc_asprintf_append((char*)dest, fmt, ## args); \ |
| 105 | } while (0) |
| 106 | |
Harald Welte | 9709b2e | 2016-04-25 18:47:53 +0200 | [diff] [blame] | 107 | int osmo_constant_time_cmp(const uint8_t *exp, const uint8_t *rel, const int count); |
| 108 | uint64_t osmo_decode_big_endian(const uint8_t *data, size_t data_len); |
| 109 | uint8_t *osmo_encode_big_endian(uint64_t value, size_t data_len); |
| 110 | |
Harald Welte | aeecc48 | 2016-11-26 10:41:40 +0100 | [diff] [blame] | 111 | size_t osmo_strlcpy(char *dst, const char *src, size_t siz); |
| 112 | |
Neels Hofmeyr | 4335bad | 2017-10-07 04:39:14 +0200 | [diff] [blame] | 113 | bool osmo_is_hexstr(const char *str, int min_digits, int max_digits, |
| 114 | bool require_even); |
| 115 | |
Harald Welte | febe83c | 2017-10-03 17:41:59 +0800 | [diff] [blame] | 116 | bool osmo_identifier_valid(const char *str); |
| 117 | |
Sylvain Munaut | dca7d2c | 2012-04-18 21:53:23 +0200 | [diff] [blame] | 118 | /*! @} */ |