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> |
Vadim Yanitskiy | 5594430 | 2018-09-05 02:58:03 +0700 | [diff] [blame] | 4 | #include <stdint.h> |
| 5 | #include <stdio.h> |
Neels Hofmeyr | 823073a | 2019-10-28 04:58:04 +0100 | [diff] [blame] | 6 | #include <string.h> |
Neels Hofmeyr | 4335bad | 2017-10-07 04:39:14 +0200 | [diff] [blame] | 7 | |
Harald Welte | b8add36 | 2013-07-06 23:49:41 +0200 | [diff] [blame] | 8 | #include <osmocom/core/backtrace.h> |
Harald Welte | f3c7e85 | 2014-08-20 22:50:47 +0200 | [diff] [blame] | 9 | #include <osmocom/core/talloc.h> |
Harald Welte | 459a180 | 2018-06-28 09:24:17 +0200 | [diff] [blame] | 10 | #include <osmocom/core/panic.h> |
Neels Hofmeyr | 7c74989 | 2018-09-07 03:01:38 +0200 | [diff] [blame] | 11 | #include <osmocom/core/defs.h> |
Harald Welte | b8add36 | 2013-07-06 23:49:41 +0200 | [diff] [blame] | 12 | |
Harald Welte | 8598f18 | 2011-08-17 14:19:27 +0200 | [diff] [blame] | 13 | /*! \defgroup utils General-purpose utility functions |
| 14 | * @{ |
Neels Hofmeyr | 17518fe | 2017-06-20 04:35:06 +0200 | [diff] [blame] | 15 | * \file utils.h */ |
Harald Welte | 8598f18 | 2011-08-17 14:19:27 +0200 | [diff] [blame] | 16 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 17 | /*! Determine number of elements in an array of static size */ |
Kévin Redon | d1e220f | 2019-06-13 13:41:00 +0200 | [diff] [blame] | 18 | #ifndef ARRAY_SIZE |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 19 | #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) |
Kévin Redon | d1e220f | 2019-06-13 13:41:00 +0200 | [diff] [blame] | 20 | #endif |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 21 | /*! Return the maximum of two specified values */ |
Holger Hans Peter Freyther | 08b2862 | 2012-08-15 17:02:59 +0200 | [diff] [blame] | 22 | #define OSMO_MAX(a, b) ((a) >= (b) ? (a) : (b)) |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 23 | /*! Return the minimum of two specified values */ |
Holger Hans Peter Freyther | 08b2862 | 2012-08-15 17:02:59 +0200 | [diff] [blame] | 24 | #define OSMO_MIN(a, b) ((a) >= (b) ? (b) : (a)) |
Neels Hofmeyr | 002a51d | 2019-10-30 04:37:47 +0100 | [diff] [blame] | 25 | /*! Return a typical cmp result for comparable entities a and b. */ |
| 26 | #define OSMO_CMP(a, b) ((a) < (b)? -1 : ((a) > (b)? 1 : 0)) |
Neels Hofmeyr | dc75b11 | 2017-12-09 05:51:42 +0100 | [diff] [blame] | 27 | /*! Stringify the name of a macro x, e.g. an FSM event name. |
| 28 | * Note: if nested within another preprocessor macro, this will |
| 29 | * stringify the value of x instead of its name. */ |
Neels Hofmeyr | 1808096 | 2016-12-16 13:43:54 +0100 | [diff] [blame] | 30 | #define OSMO_STRINGIFY(x) #x |
Neels Hofmeyr | 84ea2e0 | 2017-12-09 05:53:18 +0100 | [diff] [blame] | 31 | /*! Stringify the value of a macro x, e.g. a port number. */ |
| 32 | #define OSMO_STRINGIFY_VAL(x) OSMO_STRINGIFY(x) |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 33 | /*! Make a value_string entry from an enum value name */ |
Neels Hofmeyr | 8a5d60b | 2017-03-09 23:01:37 +0100 | [diff] [blame] | 34 | #define OSMO_VALUE_STRING(x) { x, #x } |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 35 | /*! Number of bytes necessary to store given BITS */ |
Pau Espin Pedrol | 812766e | 2022-09-08 14:14:08 +0200 | [diff] [blame] | 36 | #define OSMO_BYTES_FOR_BITS(BITS) (((BITS) + 7) / 8) |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 37 | |
Max | 18c014d | 2018-01-30 14:33:01 +0100 | [diff] [blame] | 38 | /*! Copy a C-string into a sized buffer using sizeof to detect buffer's size */ |
| 39 | #define OSMO_STRLCPY_ARRAY(array, src) osmo_strlcpy(array, src, sizeof(array)) |
| 40 | |
Vadim Yanitskiy | e59e839 | 2022-02-02 01:17:45 +0600 | [diff] [blame] | 41 | /*! Branch prediction optimizations */ |
| 42 | #if defined(__GNUC__) |
| 43 | #define OSMO_LIKELY(exp) __builtin_expect(!!(exp), 1) |
| 44 | #define OSMO_UNLIKELY(exp) __builtin_expect(!!(exp), 0) |
| 45 | #else |
| 46 | #define OSMO_LIKELY(exp) exp |
| 47 | #define OSMO_UNLIKELY(exp) exp |
| 48 | #endif |
| 49 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 50 | /*! A mapping between human-readable string and numeric value */ |
Harald Welte | d284cd9 | 2010-03-01 21:58:31 +0100 | [diff] [blame] | 51 | struct value_string { |
Harald Welte | 7d6166a | 2022-01-09 11:57:01 +0100 | [diff] [blame] | 52 | uint32_t value; /*!< numeric value */ |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 53 | const char *str; /*!< human-readable string */ |
Harald Welte | d284cd9 | 2010-03-01 21:58:31 +0100 | [diff] [blame] | 54 | }; |
| 55 | |
| 56 | 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] | 57 | const char *get_value_string_or_null(const struct value_string *vs, |
| 58 | uint32_t val); |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame] | 59 | |
Harald Welte | d284cd9 | 2010-03-01 21:58:31 +0100 | [diff] [blame] | 60 | int get_string_value(const struct value_string *vs, const char *str); |
| 61 | |
Pablo Neira Ayuso | 87f7b25 | 2011-05-07 12:43:08 +0200 | [diff] [blame] | 62 | char osmo_bcd2char(uint8_t bcd); |
Kévin Redon | 77021c7 | 2019-06-13 18:12:33 +0200 | [diff] [blame] | 63 | /* only works for numbers in ASCII */ |
Pablo Neira Ayuso | 87f7b25 | 2011-05-07 12:43:08 +0200 | [diff] [blame] | 64 | uint8_t osmo_char2bcd(char c); |
Harald Welte | d284cd9 | 2010-03-01 21:58:31 +0100 | [diff] [blame] | 65 | |
Neels Hofmeyr | 7079e69 | 2018-12-05 21:02:36 +0100 | [diff] [blame] | 66 | int osmo_bcd2str(char *dst, size_t dst_size, const uint8_t *bcd, int start_nibble, int end_nibble, bool allow_hex); |
Neels Hofmeyr | 83025bf | 2020-05-26 02:45:23 +0200 | [diff] [blame] | 67 | int osmo_str2bcd(uint8_t *dst, size_t dst_size, const char *digits, int start_nibble, int end_nibble, bool allow_hex); |
Neels Hofmeyr | 7079e69 | 2018-12-05 21:02:36 +0100 | [diff] [blame] | 68 | |
Harald Welte | 7d6166a | 2022-01-09 11:57:01 +0100 | [diff] [blame] | 69 | int osmo_hexparse(const char *str, uint8_t *b, unsigned int max_len); |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame] | 70 | |
Harald Welte | 4a62eda | 2019-03-18 18:27:00 +0100 | [diff] [blame] | 71 | char *osmo_ubit_dump_buf(char *buf, size_t buf_len, const uint8_t *bits, unsigned int len); |
Pablo Neira Ayuso | 87f7b25 | 2011-05-07 12:43:08 +0200 | [diff] [blame] | 72 | char *osmo_ubit_dump(const uint8_t *bits, unsigned int len); |
Harald Welte | 8598f18 | 2011-08-17 14:19:27 +0200 | [diff] [blame] | 73 | char *osmo_hexdump(const unsigned char *buf, int len); |
Harald Welte | 179f357 | 2019-03-18 18:38:47 +0100 | [diff] [blame] | 74 | char *osmo_hexdump_c(const void *ctx, const unsigned char *buf, int len); |
Sylvain Munaut | ff23d24 | 2011-11-10 23:03:18 +0100 | [diff] [blame] | 75 | char *osmo_hexdump_nospc(const unsigned char *buf, int len); |
Harald Welte | 179f357 | 2019-03-18 18:38:47 +0100 | [diff] [blame] | 76 | char *osmo_hexdump_nospc_c(const void *ctx, const unsigned char *buf, int len); |
Neels Hofmeyr | 0423b61 | 2019-01-14 23:32:53 +0100 | [diff] [blame] | 77 | const char *osmo_hexdump_buf(char *out_buf, size_t out_buf_size, const unsigned char *buf, int len, const char *delim, |
| 78 | bool delim_after_last); |
| 79 | |
Sylvain Munaut | 4cfbae8 | 2011-11-13 23:04:00 +0100 | [diff] [blame] | 80 | 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] | 81 | |
Holger Hans Peter Freyther | 0f40ae1 | 2015-04-13 09:21:05 +0200 | [diff] [blame] | 82 | #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] | 83 | |
Neels Hofmeyr | 7c74989 | 2018-09-07 03:01:38 +0200 | [diff] [blame] | 84 | void osmo_str2lower(char *out, const char *in) |
| 85 | OSMO_DEPRECATED("Use osmo_str_tolower() or osmo_str_tolower_buf() instead," |
| 86 | " to properly check target memory bounds"); |
| 87 | void osmo_str2upper(char *out, const char *in) |
| 88 | OSMO_DEPRECATED("Use osmo_str_toupper() or osmo_str_toupper_buf() instead," |
| 89 | " to properly check target memory bounds"); |
| 90 | |
| 91 | size_t osmo_str_tolower_buf(char *dest, size_t dest_len, const char *src); |
| 92 | const char *osmo_str_tolower(const char *src); |
Harald Welte | 179f357 | 2019-03-18 18:38:47 +0100 | [diff] [blame] | 93 | char *osmo_str_tolower_c(const void *ctx, const char *src); |
Neels Hofmeyr | 7c74989 | 2018-09-07 03:01:38 +0200 | [diff] [blame] | 94 | |
| 95 | size_t osmo_str_toupper_buf(char *dest, size_t dest_len, const char *src); |
| 96 | const char *osmo_str_toupper(const char *src); |
Harald Welte | 179f357 | 2019-03-18 18:38:47 +0100 | [diff] [blame] | 97 | char *osmo_str_toupper_c(const void *ctx, const char *src); |
Harald Welte | 2822296 | 2011-02-18 20:37:04 +0100 | [diff] [blame] | 98 | |
Pablo Neira Ayuso | 3abad6a | 2011-03-28 19:24:22 +0200 | [diff] [blame] | 99 | #define OSMO_SNPRINTF_RET(ret, rem, offset, len) \ |
| 100 | do { \ |
| 101 | len += ret; \ |
| 102 | if (ret > rem) \ |
| 103 | ret = rem; \ |
| 104 | offset += ret; \ |
| 105 | rem -= ret; \ |
| 106 | } while (0) |
| 107 | |
Kévin Redon | 77021c7 | 2019-06-13 18:12:33 +0200 | [diff] [blame] | 108 | /*! Helper macro to terminate when an assertion fails |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 109 | * \param[in] exp Predicate to verify |
| 110 | * This function will generate a backtrace and terminate the program if |
| 111 | * the predicate evaluates to false (0). |
| 112 | */ |
Katerina Barone-Adesi | 008e53b | 2013-03-03 10:36:52 +0000 | [diff] [blame] | 113 | #define OSMO_ASSERT(exp) \ |
Vadim Yanitskiy | 9ac355a | 2022-02-08 12:27:22 +0600 | [diff] [blame] | 114 | do { \ |
Vadim Yanitskiy | c797e25 | 2022-02-08 12:37:03 +0600 | [diff] [blame] | 115 | if (OSMO_UNLIKELY(!(exp))) { \ |
Neels Hofmeyr | 983dcb9 | 2018-08-20 12:33:22 +0200 | [diff] [blame] | 116 | osmo_panic("Assert failed %s %s:%d\n", #exp, __FILE__, __LINE__); \ |
Vadim Yanitskiy | 9ac355a | 2022-02-08 12:27:22 +0600 | [diff] [blame] | 117 | } \ |
| 118 | } while (0); /* some code invokes OSMO_ASSERT() without the semicolon */ |
Katerina Barone-Adesi | 008e53b | 2013-03-03 10:36:52 +0000 | [diff] [blame] | 119 | |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 120 | /*! duplicate a string using talloc and release its prior content (if any) |
| 121 | * \param[in] ctx Talloc context to use for allocation |
| 122 | * \param[out] dst pointer to string, will be updated with ptr to new string |
Kévin Redon | 77021c7 | 2019-06-13 18:12:33 +0200 | [diff] [blame] | 123 | * \param[in] newstr String that will be copied to newly allocated string */ |
Harald Welte | 881dcaf | 2016-07-28 08:02:48 +0200 | [diff] [blame] | 124 | 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] | 125 | { |
| 126 | if (*dst) |
| 127 | talloc_free(*dst); |
| 128 | *dst = talloc_strdup(ctx, newstr); |
| 129 | } |
Katerina Barone-Adesi | 008e53b | 2013-03-03 10:36:52 +0000 | [diff] [blame] | 130 | |
Vadim Yanitskiy | c549719 | 2021-06-07 16:30:28 +0200 | [diff] [blame] | 131 | void osmo_talloc_replace_string_fmt(void *ctx, char **dst, const char *fmt, ...); |
| 132 | |
Neels Hofmeyr | abdd7a2 | 2017-10-19 02:40:01 +0200 | [diff] [blame] | 133 | /*! Append to a string and re-/allocate if necessary. |
| 134 | * \param[in] ctx Talloc context to use for initial allocation. |
| 135 | * \param[in,out] dest char* to re-/allocate and append to. |
| 136 | * \param[in] fmt printf-like string format. |
| 137 | * \param[in] args Arguments for fmt. |
| 138 | * |
| 139 | * \a dest may be passed in NULL, or a string previously allocated by talloc. |
| 140 | * If an existing string is passed in, it will remain associated with whichever |
| 141 | * ctx it was allocated before, regardless whether it matches \a ctx or not. |
| 142 | */ |
| 143 | #define osmo_talloc_asprintf(ctx, dest, fmt, args ...) \ |
| 144 | do { \ |
| 145 | if (!dest) \ |
| 146 | dest = talloc_asprintf(ctx, fmt, ## args); \ |
| 147 | else \ |
| 148 | dest = talloc_asprintf_append((char*)dest, fmt, ## args); \ |
| 149 | } while (0) |
| 150 | |
Harald Welte | 9709b2e | 2016-04-25 18:47:53 +0200 | [diff] [blame] | 151 | int osmo_constant_time_cmp(const uint8_t *exp, const uint8_t *rel, const int count); |
| 152 | uint64_t osmo_decode_big_endian(const uint8_t *data, size_t data_len); |
| 153 | uint8_t *osmo_encode_big_endian(uint64_t value, size_t data_len); |
| 154 | |
Harald Welte | aeecc48 | 2016-11-26 10:41:40 +0100 | [diff] [blame] | 155 | size_t osmo_strlcpy(char *dst, const char *src, size_t siz); |
Neels Hofmeyr | 06356fd | 2019-11-19 01:38:10 +0100 | [diff] [blame] | 156 | const char *osmo_strnchr(const char *str, size_t str_size, char c); |
Harald Welte | aeecc48 | 2016-11-26 10:41:40 +0100 | [diff] [blame] | 157 | |
Neels Hofmeyr | 4335bad | 2017-10-07 04:39:14 +0200 | [diff] [blame] | 158 | bool osmo_is_hexstr(const char *str, int min_digits, int max_digits, |
| 159 | bool require_even); |
| 160 | |
Harald Welte | febe83c | 2017-10-03 17:41:59 +0800 | [diff] [blame] | 161 | bool osmo_identifier_valid(const char *str); |
Neels Hofmeyr | 937ddea | 2017-12-16 00:46:50 +0100 | [diff] [blame] | 162 | bool osmo_separated_identifiers_valid(const char *str, const char *sep_chars); |
Neels Hofmeyr | b480b74 | 2019-04-11 06:58:44 +0200 | [diff] [blame] | 163 | void osmo_identifier_sanitize_buf(char *str, const char *sep_chars, char replace_with); |
Harald Welte | febe83c | 2017-10-03 17:41:59 +0800 | [diff] [blame] | 164 | |
Neels Hofmeyr | 8a7eed5 | 2019-11-21 00:12:10 +0100 | [diff] [blame] | 165 | size_t osmo_escape_cstr_buf(char *buf, size_t bufsize, const char *str, int in_len); |
| 166 | char *osmo_escape_cstr_c(void *ctx, const char *str, int in_len); |
| 167 | size_t osmo_quote_cstr_buf(char *buf, size_t bufsize, const char *str, int in_len); |
| 168 | char *osmo_quote_cstr_c(void *ctx, const char *str, int in_len); |
| 169 | |
Neels Hofmeyr | 9910bbc | 2017-12-16 00:54:52 +0100 | [diff] [blame] | 170 | const char *osmo_escape_str(const char *str, int len); |
Neels Hofmeyr | 1633735 | 2022-01-31 15:56:02 +0100 | [diff] [blame] | 171 | int osmo_escape_str_buf3(char *buf, size_t bufsize, const char *str, int in_len); |
Neels Hofmeyr | ecef7ec | 2019-03-05 16:42:50 +0100 | [diff] [blame] | 172 | char *osmo_escape_str_buf2(char *buf, size_t bufsize, const char *str, int in_len); |
| 173 | const char *osmo_escape_str_buf(const char *str, int in_len, char *buf, size_t bufsize); |
Harald Welte | 179f357 | 2019-03-18 18:38:47 +0100 | [diff] [blame] | 174 | char *osmo_escape_str_c(const void *ctx, const char *str, int in_len); |
Neels Hofmeyr | 04eb56f | 2018-04-09 00:41:28 +0200 | [diff] [blame] | 175 | const char *osmo_quote_str(const char *str, int in_len); |
Neels Hofmeyr | 1633735 | 2022-01-31 15:56:02 +0100 | [diff] [blame] | 176 | int osmo_quote_str_buf3(char *buf, size_t bufsize, const char *str, int in_len); |
Neels Hofmeyr | ecef7ec | 2019-03-05 16:42:50 +0100 | [diff] [blame] | 177 | char *osmo_quote_str_buf2(char *buf, size_t bufsize, const char *str, int in_len); |
| 178 | const char *osmo_quote_str_buf(const char *str, int in_len, char *buf, size_t bufsize); |
Harald Welte | 179f357 | 2019-03-18 18:38:47 +0100 | [diff] [blame] | 179 | char *osmo_quote_str_c(const void *ctx, const char *str, int in_len); |
Neels Hofmeyr | 9910bbc | 2017-12-16 00:54:52 +0100 | [diff] [blame] | 180 | |
Neels Hofmeyr | ecef7ec | 2019-03-05 16:42:50 +0100 | [diff] [blame] | 181 | int osmo_print_n(char *buf, size_t bufsize, const char *str, size_t n); |
| 182 | |
Harald Welte | 15a5f8d | 2018-06-06 16:58:17 +0200 | [diff] [blame] | 183 | uint32_t osmo_isqrt32(uint32_t x); |
| 184 | |
Philipp Maier | 94705d0 | 2023-01-11 10:55:47 +0100 | [diff] [blame] | 185 | /*! Floored Modulo (See also: Daan Leijen, Division and Modulus for Computer Scientists). |
| 186 | * \param[in] x dividend. |
| 187 | * \param[in] y divisor. |
| 188 | * \returns remainder of x divided by y. */ |
| 189 | #define OSMO_MOD_FLR(x, y) (((x) > 0 && (y) < 0) || ((x) < 0 && (y) > 0) ? (x) % (y) + (y) : (x) % (y)) |
| 190 | |
| 191 | /*! Euclidean Modulo (See also: Daan Leijen, Division and Modulus for Computer Scientists). |
| 192 | * \param[in] x dividend. |
| 193 | * \param[in] y divisor. |
| 194 | * \returns remainder of x divided by y. */ |
| 195 | #define OSMO_MOD_EUC(x, y) ((x) % (y) < 0 ? (y) > 0 ? (x) % (y) + (y) : (x) % (y) - (y) : (x) % (y)) |
| 196 | |
Vadim Yanitskiy | d9fc604 | 2019-06-12 15:49:03 +0700 | [diff] [blame] | 197 | char osmo_luhn(const char* in, int in_len); |
Oliver Smith | 894be2d | 2019-01-11 13:13:37 +0100 | [diff] [blame] | 198 | |
Neels Hofmeyr | 2cbe25f | 2019-02-11 20:32:06 +0100 | [diff] [blame] | 199 | /*! State for OSMO_STRBUF_APPEND() and OSMO_STRBUF_PRINTF(). See there for examples. */ |
| 200 | struct osmo_strbuf { |
| 201 | /*! Point to the start of a string buffer. */ |
| 202 | char *buf; |
| 203 | /*! Total sizeof() the buffer buf points at. */ |
| 204 | size_t len; |
| 205 | /*! Current writing position in buf (end of the string written so far). */ |
| 206 | char *pos; |
| 207 | /*! After all OSMO_STRBUF_APPEND operations, reflects the total number of characters that would be written had |
| 208 | * buf been large enough. Like snprintf()'s return value, this does not include the terminating nul character. |
| 209 | * Hence, to allocate an adequately sized buffer, add 1 to this number. */ |
| 210 | size_t chars_needed; |
| 211 | }; |
| 212 | |
| 213 | /*! Append a string to a buffer, as printed by an snprintf()-like function and with similar bounds checking. |
| 214 | * Make sure to never write past the end of the buffer, and collect the total size that would be needed. |
| 215 | * |
| 216 | * // an example function implementation to append: write N spaces. |
| 217 | * int print_spaces(char *dst, size_t dst_len, int n) |
| 218 | * { |
| 219 | * int i; |
| 220 | * if (n < 0) |
| 221 | * return -EINVAL; |
| 222 | * for (i = 0; i < n && i < dst_len; i++) |
| 223 | * dst[i] = ' '; |
| 224 | * if (dst_len) |
| 225 | * dst[OSMO_MIN(dst_len - 1, n)] = '\0'; |
| 226 | * // return the n that we would have liked to write if space were available: |
| 227 | * return n; |
| 228 | * } |
| 229 | * |
| 230 | * // append above spaces as well as an snprintf() |
| 231 | * void strbuf_example() |
| 232 | * { |
| 233 | * char buf[23]; |
| 234 | * struct osmo_strbuf sb = { .buf = buf, .len = sizeof(buf) }; |
| 235 | * |
| 236 | * OSMO_STRBUF_APPEND(sb, print_spaces, 5); |
| 237 | * OSMO_STRBUF_APPEND(sb, snprintf, "The answer is %d but what is the question?", 42); |
| 238 | * OSMO_STRBUF_APPEND(sb, print_spaces, 423423); |
| 239 | * |
| 240 | * printf("%s\n", buf); |
| 241 | * printf("would have needed %zu bytes\n", sb.chars_needed); |
| 242 | * } |
| 243 | * |
| 244 | * \param[inout] STRBUF A struct osmo_strbuf instance. |
| 245 | * \param[in] func A function with a signature of int func(char *dst, size_t dst_len [, args]) with semantics like |
| 246 | * snprintf(). |
| 247 | * \param[in] args Arguments passed to func, if any. |
| 248 | */ |
| 249 | #define OSMO_STRBUF_APPEND(STRBUF, func, args...) do { \ |
| 250 | if (!(STRBUF).pos) \ |
| 251 | (STRBUF).pos = (STRBUF).buf; \ |
Neels Hofmeyr | 271baad | 2023-12-04 06:55:24 +0100 | [diff] [blame] | 252 | size_t _sb_remain = OSMO_STRBUF_REMAIN(STRBUF); \ |
Neels Hofmeyr | 8531d66 | 2019-04-11 07:16:02 +0200 | [diff] [blame] | 253 | int _sb_l = func((STRBUF).pos, _sb_remain, ##args); \ |
Pau Espin Pedrol | 7af860f | 2021-07-27 16:32:36 +0200 | [diff] [blame] | 254 | if (_sb_l < 0 || (size_t)_sb_l > _sb_remain) \ |
Neels Hofmeyr | 2cbe25f | 2019-02-11 20:32:06 +0100 | [diff] [blame] | 255 | (STRBUF).pos = (STRBUF).buf + (STRBUF).len; \ |
Neels Hofmeyr | 8531d66 | 2019-04-11 07:16:02 +0200 | [diff] [blame] | 256 | else if ((STRBUF).pos) \ |
| 257 | (STRBUF).pos += _sb_l; \ |
| 258 | if (_sb_l > 0) \ |
| 259 | (STRBUF).chars_needed += _sb_l; \ |
Neels Hofmeyr | 2cbe25f | 2019-02-11 20:32:06 +0100 | [diff] [blame] | 260 | } while(0) |
| 261 | |
| 262 | /*! Shortcut for OSMO_STRBUF_APPEND() invocation using snprintf(). |
| 263 | * |
| 264 | * int strbuf_example2(char *buf, size_t buflen) |
| 265 | * { |
| 266 | * int i; |
| 267 | * struct osmo_strbuf sb = { .buf = buf, .len = buflen }; |
| 268 | * |
| 269 | * OSMO_STRBUF_PRINTF(sb, "T minus"); |
| 270 | * for (i = 10; i; i--) |
| 271 | * OSMO_STRBUF_PRINTF(sb, " %d", i); |
| 272 | * OSMO_STRBUF_PRINTF(sb, " ... Lift off!"); |
| 273 | * |
| 274 | * return sb.chars_needed; |
| 275 | * } |
| 276 | * |
| 277 | * \param[inout] STRBUF A struct osmo_strbuf instance. |
| 278 | * \param[in] fmt Format string passed to snprintf. |
| 279 | * \param[in] args Additional arguments passed to snprintf, if any. |
| 280 | */ |
| 281 | #define OSMO_STRBUF_PRINTF(STRBUF, fmt, args...) \ |
| 282 | OSMO_STRBUF_APPEND(STRBUF, snprintf, fmt, ##args) |
| 283 | |
Vadim Yanitskiy | 9df820b | 2023-12-11 13:34:02 +0700 | [diff] [blame] | 284 | /*! Get remaining space for characters and terminating nul in the given struct osmo_strbuf. |
| 285 | * \param[in] sb the string buffer to get the remaining space for. |
| 286 | * \returns remaining space in the given struct osmo_strbuf. */ |
| 287 | static inline size_t _osmo_strbuf_remain(const struct osmo_strbuf *sb) |
| 288 | { |
| 289 | if (OSMO_UNLIKELY(sb == NULL || sb->buf == NULL)) |
| 290 | return 0; |
| 291 | if (sb->pos == NULL) |
| 292 | return sb->len; |
| 293 | return sb->len - (sb->pos - sb->buf); |
| 294 | } |
| 295 | |
Neels Hofmeyr | 271baad | 2023-12-04 06:55:24 +0100 | [diff] [blame] | 296 | /*! Return remaining space for characters and terminating nul in the given struct osmo_strbuf. */ |
Vadim Yanitskiy | 9df820b | 2023-12-11 13:34:02 +0700 | [diff] [blame] | 297 | #define OSMO_STRBUF_REMAIN(STRBUF) \ |
| 298 | _osmo_strbuf_remain(&(STRBUF)) |
Neels Hofmeyr | 271baad | 2023-12-04 06:55:24 +0100 | [diff] [blame] | 299 | |
Vadim Yanitskiy | 0f59ceb | 2023-12-11 14:07:53 +0700 | [diff] [blame] | 300 | /*! Get number of actual characters (without terminating nul) in the given struct osmo_strbuf. |
| 301 | * \param[in] sb the string buffer to get the number of characters for. |
| 302 | * \returns number of actual characters (without terminating nul). */ |
| 303 | static inline size_t _osmo_strbuf_char_count(const struct osmo_strbuf *sb) |
| 304 | { |
| 305 | if (OSMO_UNLIKELY(sb == NULL || sb->buf == NULL)) |
| 306 | return 0; |
| 307 | if (sb->pos == NULL || sb->pos <= sb->buf) |
| 308 | return 0; |
Vadim Yanitskiy | fb4ce0b | 2023-12-31 16:38:40 +0700 | [diff] [blame] | 309 | return OSMO_MIN((size_t)(sb->pos - sb->buf), sb->len - 1); |
Vadim Yanitskiy | 0f59ceb | 2023-12-11 14:07:53 +0700 | [diff] [blame] | 310 | } |
| 311 | |
Neels Hofmeyr | d511a9d | 2023-12-04 07:48:55 +0100 | [diff] [blame] | 312 | /*! Return number of actual characters contained in struct osmo_strbuf (without terminating nul). */ |
Vadim Yanitskiy | 0f59ceb | 2023-12-11 14:07:53 +0700 | [diff] [blame] | 313 | #define OSMO_STRBUF_CHAR_COUNT(STRBUF) \ |
| 314 | _osmo_strbuf_char_count(&(STRBUF)) |
Neels Hofmeyr | d511a9d | 2023-12-04 07:48:55 +0100 | [diff] [blame] | 315 | |
Neels Hofmeyr | 8531d66 | 2019-04-11 07:16:02 +0200 | [diff] [blame] | 316 | /*! Like OSMO_STRBUF_APPEND(), but for function signatures that return the char* buffer instead of a length. |
| 317 | * When using this function, the final STRBUF.chars_needed may not reflect the actual number of characters needed, since |
| 318 | * that number cannot be obtained from this kind of function signature. |
| 319 | * \param[inout] STRBUF A struct osmo_strbuf instance. |
| 320 | * \param[in] func A function with a signature of char *func(char *dst, size_t dst_len [, args]) where |
| 321 | * the returned string is always written to dst. |
| 322 | * \param[in] args Arguments passed to func, if any. |
| 323 | */ |
| 324 | #define OSMO_STRBUF_APPEND_NOLEN(STRBUF, func, args...) do { \ |
| 325 | if (!(STRBUF).pos) \ |
| 326 | (STRBUF).pos = (STRBUF).buf; \ |
Neels Hofmeyr | 271baad | 2023-12-04 06:55:24 +0100 | [diff] [blame] | 327 | size_t _sb_remain = OSMO_STRBUF_REMAIN(STRBUF); \ |
Neels Hofmeyr | 8531d66 | 2019-04-11 07:16:02 +0200 | [diff] [blame] | 328 | if (_sb_remain) { \ |
| 329 | func((STRBUF).pos, _sb_remain, ##args); \ |
| 330 | } \ |
| 331 | size_t _sb_l = (STRBUF).pos ? strnlen((STRBUF).pos, _sb_remain) : 0; \ |
| 332 | if (_sb_l > _sb_remain) \ |
| 333 | (STRBUF).pos = (STRBUF).buf + (STRBUF).len; \ |
| 334 | else if ((STRBUF).pos) \ |
| 335 | (STRBUF).pos += _sb_l; \ |
| 336 | (STRBUF).chars_needed += _sb_l; \ |
| 337 | } while(0) |
| 338 | |
Neels Hofmeyr | d511a9d | 2023-12-04 07:48:55 +0100 | [diff] [blame] | 339 | void osmo_strbuf_drop_tail(struct osmo_strbuf *sb, size_t n_chars); |
| 340 | /* Convenience macro. struct osmo_strbuf are typically static to a function scope. Avoid having to type '&', same as |
| 341 | * with all the other OSMO_STRBUF_* API. */ |
| 342 | #define OSMO_STRBUF_DROP_TAIL(STRBUF, N_CHARS) osmo_strbuf_drop_tail(&(STRBUF), N_CHARS) |
| 343 | |
| 344 | void osmo_strbuf_added_tail(struct osmo_strbuf *sb, size_t n_chars); |
| 345 | /* Convenience macro. struct osmo_strbuf are typically static to a function scope. Avoid having to type '&', same as |
| 346 | * with all the other OSMO_STRBUF_* API. */ |
| 347 | #define OSMO_STRBUF_ADDED_TAIL(STRBUF, N_CHARS) osmo_strbuf_added_tail(&(STRBUF), N_CHARS) |
| 348 | |
Neels Hofmeyr | d79ccc6 | 2019-03-07 23:08:40 +0100 | [diff] [blame] | 349 | bool osmo_str_startswith(const char *str, const char *startswith_str); |
| 350 | |
Neels Hofmeyr | 87c3afb | 2020-09-30 21:47:47 +0000 | [diff] [blame] | 351 | int osmo_float_str_to_int(int64_t *val, const char *str, unsigned int precision); |
| 352 | int osmo_int_to_float_str_buf(char *buf, size_t buflen, int64_t val, unsigned int precision); |
| 353 | char *osmo_int_to_float_str_c(void *ctx, int64_t val, unsigned int precision); |
| 354 | |
Neels Hofmeyr | 4777334 | 2021-09-05 18:48:31 +0200 | [diff] [blame] | 355 | int osmo_str_to_int64(int64_t *result, const char *str, int base, int64_t min_val, int64_t max_val); |
| 356 | int osmo_str_to_int(int *result, const char *str, int base, int min_val, int max_val); |
| 357 | |
Neels Hofmeyr | 823073a | 2019-10-28 04:58:04 +0100 | [diff] [blame] | 358 | /*! Translate a buffer function to a talloc context function. |
| 359 | * This is the full function body of a char *foo_name_c(void *ctx, val...) function, implemented by an |
| 360 | * int foo_name_buf(buf, buflen, val...) function: |
| 361 | * |
| 362 | * char *foo_name_c(void *ctx, example_t arg) |
| 363 | * { |
| 364 | * OSMO_NAME_C_IMPL(ctx, 64, "ERROR", foo_name_buf, arg) |
| 365 | * } |
| 366 | * |
| 367 | * Return a talloc'd string containing the result of the given foo_name_buf() function, or ON_ERROR on error in the called |
| 368 | * foo_name_buf() function. |
| 369 | * |
| 370 | * If ON_ERROR is NULL, the function returns NULL on error rc from FUNC_BUF. Take care: returning NULL in printf() like |
| 371 | * formats (LOGP()) makes the program crash. If ON_ERROR is non-NULL, it must be a string constant, which is not |
| 372 | * returned directly, but written to an allocated string buffer first. |
| 373 | * |
| 374 | * \param[in] INITIAL_BUFSIZE Which size to first talloc from ctx -- a larger size makes a reallocation less likely, a |
| 375 | * smaller size allocates less unused bytes, zero allocates once but still runs the string composition twice. |
| 376 | * \param[in] ON_ERROR String constant to copy on error rc returned by FUNC_BUF, or NULL to return NULL. |
Neels Hofmeyr | dacac99 | 2020-05-26 02:44:21 +0200 | [diff] [blame] | 377 | * \param[in] FUNC_BUF Name of a function with signature int foo_buf(char *buf, size_t buflen, ...). |
| 378 | * The function must return the strlen() that it would write to a sufficiently large buffer or |
| 379 | * negative on error, like snprintf(). |
Neels Hofmeyr | 823073a | 2019-10-28 04:58:04 +0100 | [diff] [blame] | 380 | * \param[in] FUNC_BUF_ARGS Additional arguments to pass to FUNC_BUF after the buf and buflen. |
| 381 | */ |
| 382 | #define OSMO_NAME_C_IMPL(CTX, INITIAL_BUFSIZE, ON_ERROR, FUNC_BUF, FUNC_BUF_ARGS...) \ |
| 383 | size_t _len = INITIAL_BUFSIZE; \ |
| 384 | int _needed; \ |
| 385 | char *_str = NULL; \ |
| 386 | if ((INITIAL_BUFSIZE) > 0) { \ |
| 387 | _str = (char*)talloc_named_const(CTX, _len, __func__); \ |
| 388 | OSMO_ASSERT(_str); \ |
| 389 | } \ |
| 390 | _needed = FUNC_BUF(_str, _len, ## FUNC_BUF_ARGS); \ |
| 391 | if (_needed < 0) \ |
| 392 | goto OSMO_NAME_C_on_error; \ |
Harald Welte | 7d6166a | 2022-01-09 11:57:01 +0100 | [diff] [blame] | 393 | if ((unsigned int) _needed < _len) \ |
Neels Hofmeyr | 823073a | 2019-10-28 04:58:04 +0100 | [diff] [blame] | 394 | return _str; \ |
| 395 | _len = _needed + 1; \ |
| 396 | if (_str) \ |
| 397 | talloc_free(_str); \ |
| 398 | _str = (char*)talloc_named_const(CTX, _len, __func__); \ |
| 399 | OSMO_ASSERT(_str); \ |
| 400 | _needed = FUNC_BUF(_str, _len, ## FUNC_BUF_ARGS); \ |
| 401 | if (_needed < 0) \ |
| 402 | goto OSMO_NAME_C_on_error; \ |
| 403 | return _str; \ |
| 404 | OSMO_NAME_C_on_error: \ |
| 405 | /* Re-using and re-sizing above allocated buf ends up in very complex code. Just free and strdup. */ \ |
| 406 | if (_str) \ |
| 407 | talloc_free(_str); \ |
| 408 | if (!(ON_ERROR)) \ |
| 409 | return NULL; \ |
| 410 | _str = talloc_strdup(CTX, ON_ERROR); \ |
| 411 | OSMO_ASSERT(_str); \ |
| 412 | talloc_set_name_const(_str, __func__); \ |
| 413 | return _str; |
| 414 | |
Sylvain Munaut | dca7d2c | 2012-04-18 21:53:23 +0200 | [diff] [blame] | 415 | /*! @} */ |