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 | 4335bad | 2017-10-07 04:39:14 +0200 | [diff] [blame] | 6 | |
Harald Welte | b8add36 | 2013-07-06 23:49:41 +0200 | [diff] [blame] | 7 | #include <osmocom/core/backtrace.h> |
Harald Welte | f3c7e85 | 2014-08-20 22:50:47 +0200 | [diff] [blame] | 8 | #include <osmocom/core/talloc.h> |
Harald Welte | 459a180 | 2018-06-28 09:24:17 +0200 | [diff] [blame] | 9 | #include <osmocom/core/panic.h> |
Neels Hofmeyr | 7c74989 | 2018-09-07 03:01:38 +0200 | [diff] [blame] | 10 | #include <osmocom/core/defs.h> |
Harald Welte | b8add36 | 2013-07-06 23:49:41 +0200 | [diff] [blame] | 11 | |
Harald Welte | 8598f18 | 2011-08-17 14:19:27 +0200 | [diff] [blame] | 12 | /*! \defgroup utils General-purpose utility functions |
| 13 | * @{ |
Neels Hofmeyr | 17518fe | 2017-06-20 04:35:06 +0200 | [diff] [blame] | 14 | * \file utils.h */ |
Harald Welte | 8598f18 | 2011-08-17 14:19:27 +0200 | [diff] [blame] | 15 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 16 | /*! Determine number of elements in an array of static size */ |
Kévin Redon | d1e220f | 2019-06-13 13:41:00 +0200 | [diff] [blame] | 17 | #ifndef ARRAY_SIZE |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 18 | #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) |
Kévin Redon | d1e220f | 2019-06-13 13:41:00 +0200 | [diff] [blame] | 19 | #endif |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 20 | /*! Return the maximum of two specified values */ |
Holger Hans Peter Freyther | 08b2862 | 2012-08-15 17:02:59 +0200 | [diff] [blame] | 21 | #define OSMO_MAX(a, b) ((a) >= (b) ? (a) : (b)) |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 22 | /*! Return the minimum of two specified values */ |
Holger Hans Peter Freyther | 08b2862 | 2012-08-15 17:02:59 +0200 | [diff] [blame] | 23 | #define OSMO_MIN(a, b) ((a) >= (b) ? (b) : (a)) |
Neels Hofmeyr | dc75b11 | 2017-12-09 05:51:42 +0100 | [diff] [blame] | 24 | /*! Stringify the name of a macro x, e.g. an FSM event name. |
| 25 | * Note: if nested within another preprocessor macro, this will |
| 26 | * stringify the value of x instead of its name. */ |
Neels Hofmeyr | 1808096 | 2016-12-16 13:43:54 +0100 | [diff] [blame] | 27 | #define OSMO_STRINGIFY(x) #x |
Neels Hofmeyr | 84ea2e0 | 2017-12-09 05:53:18 +0100 | [diff] [blame] | 28 | /*! Stringify the value of a macro x, e.g. a port number. */ |
| 29 | #define OSMO_STRINGIFY_VAL(x) OSMO_STRINGIFY(x) |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 30 | /*! Make a value_string entry from an enum value name */ |
Neels Hofmeyr | 8a5d60b | 2017-03-09 23:01:37 +0100 | [diff] [blame] | 31 | #define OSMO_VALUE_STRING(x) { x, #x } |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 32 | /*! Number of bytes necessary to store given BITS */ |
Max | dd75bac | 2017-06-13 15:07:01 +0200 | [diff] [blame] | 33 | #define OSMO_BYTES_FOR_BITS(BITS) ((BITS + 8 - 1) / 8) |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 34 | |
Max | 18c014d | 2018-01-30 14:33:01 +0100 | [diff] [blame] | 35 | /*! Copy a C-string into a sized buffer using sizeof to detect buffer's size */ |
| 36 | #define OSMO_STRLCPY_ARRAY(array, src) osmo_strlcpy(array, src, sizeof(array)) |
| 37 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 38 | /*! A mapping between human-readable string and numeric value */ |
Harald Welte | d284cd9 | 2010-03-01 21:58:31 +0100 | [diff] [blame] | 39 | struct value_string { |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 40 | unsigned int value; /*!< numeric value */ |
| 41 | const char *str; /*!< human-readable string */ |
Harald Welte | d284cd9 | 2010-03-01 21:58:31 +0100 | [diff] [blame] | 42 | }; |
| 43 | |
| 44 | 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] | 45 | const char *get_value_string_or_null(const struct value_string *vs, |
| 46 | uint32_t val); |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame] | 47 | |
Harald Welte | d284cd9 | 2010-03-01 21:58:31 +0100 | [diff] [blame] | 48 | int get_string_value(const struct value_string *vs, const char *str); |
| 49 | |
Pablo Neira Ayuso | 87f7b25 | 2011-05-07 12:43:08 +0200 | [diff] [blame] | 50 | char osmo_bcd2char(uint8_t bcd); |
Kévin Redon | 77021c7 | 2019-06-13 18:12:33 +0200 | [diff] [blame] | 51 | /* only works for numbers in ASCII */ |
Pablo Neira Ayuso | 87f7b25 | 2011-05-07 12:43:08 +0200 | [diff] [blame] | 52 | uint8_t osmo_char2bcd(char c); |
Harald Welte | d284cd9 | 2010-03-01 21:58:31 +0100 | [diff] [blame] | 53 | |
Neels Hofmeyr | 7079e69 | 2018-12-05 21:02:36 +0100 | [diff] [blame] | 54 | int osmo_bcd2str(char *dst, size_t dst_size, const uint8_t *bcd, int start_nibble, int end_nibble, bool allow_hex); |
| 55 | |
Pablo Neira Ayuso | 87f7b25 | 2011-05-07 12:43:08 +0200 | [diff] [blame] | 56 | int osmo_hexparse(const char *str, uint8_t *b, int max_len); |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame] | 57 | |
Harald Welte | 4a62eda | 2019-03-18 18:27:00 +0100 | [diff] [blame] | 58 | 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] | 59 | char *osmo_ubit_dump(const uint8_t *bits, unsigned int len); |
Harald Welte | 8598f18 | 2011-08-17 14:19:27 +0200 | [diff] [blame] | 60 | char *osmo_hexdump(const unsigned char *buf, int len); |
Harald Welte | 179f357 | 2019-03-18 18:38:47 +0100 | [diff] [blame] | 61 | 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] | 62 | char *osmo_hexdump_nospc(const unsigned char *buf, int len); |
Harald Welte | 179f357 | 2019-03-18 18:38:47 +0100 | [diff] [blame] | 63 | 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] | 64 | const char *osmo_hexdump_buf(char *out_buf, size_t out_buf_size, const unsigned char *buf, int len, const char *delim, |
| 65 | bool delim_after_last); |
| 66 | |
Sylvain Munaut | 4cfbae8 | 2011-11-13 23:04:00 +0100 | [diff] [blame] | 67 | 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] | 68 | |
Holger Hans Peter Freyther | 0f40ae1 | 2015-04-13 09:21:05 +0200 | [diff] [blame] | 69 | #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] | 70 | |
Neels Hofmeyr | 7c74989 | 2018-09-07 03:01:38 +0200 | [diff] [blame] | 71 | void osmo_str2lower(char *out, const char *in) |
| 72 | OSMO_DEPRECATED("Use osmo_str_tolower() or osmo_str_tolower_buf() instead," |
| 73 | " to properly check target memory bounds"); |
| 74 | void osmo_str2upper(char *out, const char *in) |
| 75 | OSMO_DEPRECATED("Use osmo_str_toupper() or osmo_str_toupper_buf() instead," |
| 76 | " to properly check target memory bounds"); |
| 77 | |
| 78 | size_t osmo_str_tolower_buf(char *dest, size_t dest_len, const char *src); |
| 79 | const char *osmo_str_tolower(const char *src); |
Harald Welte | 179f357 | 2019-03-18 18:38:47 +0100 | [diff] [blame] | 80 | char *osmo_str_tolower_c(const void *ctx, const char *src); |
Neels Hofmeyr | 7c74989 | 2018-09-07 03:01:38 +0200 | [diff] [blame] | 81 | |
| 82 | size_t osmo_str_toupper_buf(char *dest, size_t dest_len, const char *src); |
| 83 | const char *osmo_str_toupper(const char *src); |
Harald Welte | 179f357 | 2019-03-18 18:38:47 +0100 | [diff] [blame] | 84 | char *osmo_str_toupper_c(const void *ctx, const char *src); |
Harald Welte | 2822296 | 2011-02-18 20:37:04 +0100 | [diff] [blame] | 85 | |
Pablo Neira Ayuso | 3abad6a | 2011-03-28 19:24:22 +0200 | [diff] [blame] | 86 | #define OSMO_SNPRINTF_RET(ret, rem, offset, len) \ |
| 87 | do { \ |
| 88 | len += ret; \ |
| 89 | if (ret > rem) \ |
| 90 | ret = rem; \ |
| 91 | offset += ret; \ |
| 92 | rem -= ret; \ |
| 93 | } while (0) |
| 94 | |
Kévin Redon | 77021c7 | 2019-06-13 18:12:33 +0200 | [diff] [blame] | 95 | /*! Helper macro to terminate when an assertion fails |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 96 | * \param[in] exp Predicate to verify |
| 97 | * This function will generate a backtrace and terminate the program if |
| 98 | * the predicate evaluates to false (0). |
| 99 | */ |
Katerina Barone-Adesi | 008e53b | 2013-03-03 10:36:52 +0000 | [diff] [blame] | 100 | #define OSMO_ASSERT(exp) \ |
Vadim Yanitskiy | e242548 | 2019-07-09 13:56:24 +0700 | [diff] [blame] | 101 | if (!(exp)) { \ |
Neels Hofmeyr | 983dcb9 | 2018-08-20 12:33:22 +0200 | [diff] [blame] | 102 | osmo_panic("Assert failed %s %s:%d\n", #exp, __FILE__, __LINE__); \ |
Vadim Yanitskiy | e242548 | 2019-07-09 13:56:24 +0700 | [diff] [blame] | 103 | } |
Katerina Barone-Adesi | 008e53b | 2013-03-03 10:36:52 +0000 | [diff] [blame] | 104 | |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 105 | /*! duplicate a string using talloc and release its prior content (if any) |
| 106 | * \param[in] ctx Talloc context to use for allocation |
| 107 | * \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] | 108 | * \param[in] newstr String that will be copied to newly allocated string */ |
Harald Welte | 881dcaf | 2016-07-28 08:02:48 +0200 | [diff] [blame] | 109 | 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] | 110 | { |
| 111 | if (*dst) |
| 112 | talloc_free(*dst); |
| 113 | *dst = talloc_strdup(ctx, newstr); |
| 114 | } |
Katerina Barone-Adesi | 008e53b | 2013-03-03 10:36:52 +0000 | [diff] [blame] | 115 | |
Neels Hofmeyr | abdd7a2 | 2017-10-19 02:40:01 +0200 | [diff] [blame] | 116 | /*! Append to a string and re-/allocate if necessary. |
| 117 | * \param[in] ctx Talloc context to use for initial allocation. |
| 118 | * \param[in,out] dest char* to re-/allocate and append to. |
| 119 | * \param[in] fmt printf-like string format. |
| 120 | * \param[in] args Arguments for fmt. |
| 121 | * |
| 122 | * \a dest may be passed in NULL, or a string previously allocated by talloc. |
| 123 | * If an existing string is passed in, it will remain associated with whichever |
| 124 | * ctx it was allocated before, regardless whether it matches \a ctx or not. |
| 125 | */ |
| 126 | #define osmo_talloc_asprintf(ctx, dest, fmt, args ...) \ |
| 127 | do { \ |
| 128 | if (!dest) \ |
| 129 | dest = talloc_asprintf(ctx, fmt, ## args); \ |
| 130 | else \ |
| 131 | dest = talloc_asprintf_append((char*)dest, fmt, ## args); \ |
| 132 | } while (0) |
| 133 | |
Harald Welte | 9709b2e | 2016-04-25 18:47:53 +0200 | [diff] [blame] | 134 | int osmo_constant_time_cmp(const uint8_t *exp, const uint8_t *rel, const int count); |
| 135 | uint64_t osmo_decode_big_endian(const uint8_t *data, size_t data_len); |
| 136 | uint8_t *osmo_encode_big_endian(uint64_t value, size_t data_len); |
| 137 | |
Harald Welte | aeecc48 | 2016-11-26 10:41:40 +0100 | [diff] [blame] | 138 | size_t osmo_strlcpy(char *dst, const char *src, size_t siz); |
| 139 | |
Neels Hofmeyr | 4335bad | 2017-10-07 04:39:14 +0200 | [diff] [blame] | 140 | bool osmo_is_hexstr(const char *str, int min_digits, int max_digits, |
| 141 | bool require_even); |
| 142 | |
Harald Welte | febe83c | 2017-10-03 17:41:59 +0800 | [diff] [blame] | 143 | bool osmo_identifier_valid(const char *str); |
Neels Hofmeyr | 937ddea | 2017-12-16 00:46:50 +0100 | [diff] [blame] | 144 | bool osmo_separated_identifiers_valid(const char *str, const char *sep_chars); |
Neels Hofmeyr | b480b74 | 2019-04-11 06:58:44 +0200 | [diff] [blame] | 145 | 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] | 146 | |
Neels Hofmeyr | 9910bbc | 2017-12-16 00:54:52 +0100 | [diff] [blame] | 147 | const char *osmo_escape_str(const char *str, int len); |
Neels Hofmeyr | ecef7ec | 2019-03-05 16:42:50 +0100 | [diff] [blame] | 148 | char *osmo_escape_str_buf2(char *buf, size_t bufsize, const char *str, int in_len); |
| 149 | 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] | 150 | 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] | 151 | const char *osmo_quote_str(const char *str, int in_len); |
Neels Hofmeyr | ecef7ec | 2019-03-05 16:42:50 +0100 | [diff] [blame] | 152 | char *osmo_quote_str_buf2(char *buf, size_t bufsize, const char *str, int in_len); |
| 153 | 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] | 154 | 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] | 155 | |
Neels Hofmeyr | ecef7ec | 2019-03-05 16:42:50 +0100 | [diff] [blame] | 156 | int osmo_print_n(char *buf, size_t bufsize, const char *str, size_t n); |
| 157 | |
Harald Welte | 15a5f8d | 2018-06-06 16:58:17 +0200 | [diff] [blame] | 158 | uint32_t osmo_isqrt32(uint32_t x); |
| 159 | |
Vadim Yanitskiy | d9fc604 | 2019-06-12 15:49:03 +0700 | [diff] [blame] | 160 | char osmo_luhn(const char* in, int in_len); |
Oliver Smith | 894be2d | 2019-01-11 13:13:37 +0100 | [diff] [blame] | 161 | |
Neels Hofmeyr | 2cbe25f | 2019-02-11 20:32:06 +0100 | [diff] [blame] | 162 | /*! State for OSMO_STRBUF_APPEND() and OSMO_STRBUF_PRINTF(). See there for examples. */ |
| 163 | struct osmo_strbuf { |
| 164 | /*! Point to the start of a string buffer. */ |
| 165 | char *buf; |
| 166 | /*! Total sizeof() the buffer buf points at. */ |
| 167 | size_t len; |
| 168 | /*! Current writing position in buf (end of the string written so far). */ |
| 169 | char *pos; |
| 170 | /*! After all OSMO_STRBUF_APPEND operations, reflects the total number of characters that would be written had |
| 171 | * buf been large enough. Like snprintf()'s return value, this does not include the terminating nul character. |
| 172 | * Hence, to allocate an adequately sized buffer, add 1 to this number. */ |
| 173 | size_t chars_needed; |
| 174 | }; |
| 175 | |
| 176 | /*! Append a string to a buffer, as printed by an snprintf()-like function and with similar bounds checking. |
| 177 | * Make sure to never write past the end of the buffer, and collect the total size that would be needed. |
| 178 | * |
| 179 | * // an example function implementation to append: write N spaces. |
| 180 | * int print_spaces(char *dst, size_t dst_len, int n) |
| 181 | * { |
| 182 | * int i; |
| 183 | * if (n < 0) |
| 184 | * return -EINVAL; |
| 185 | * for (i = 0; i < n && i < dst_len; i++) |
| 186 | * dst[i] = ' '; |
| 187 | * if (dst_len) |
| 188 | * dst[OSMO_MIN(dst_len - 1, n)] = '\0'; |
| 189 | * // return the n that we would have liked to write if space were available: |
| 190 | * return n; |
| 191 | * } |
| 192 | * |
| 193 | * // append above spaces as well as an snprintf() |
| 194 | * void strbuf_example() |
| 195 | * { |
| 196 | * char buf[23]; |
| 197 | * struct osmo_strbuf sb = { .buf = buf, .len = sizeof(buf) }; |
| 198 | * |
| 199 | * OSMO_STRBUF_APPEND(sb, print_spaces, 5); |
| 200 | * OSMO_STRBUF_APPEND(sb, snprintf, "The answer is %d but what is the question?", 42); |
| 201 | * OSMO_STRBUF_APPEND(sb, print_spaces, 423423); |
| 202 | * |
| 203 | * printf("%s\n", buf); |
| 204 | * printf("would have needed %zu bytes\n", sb.chars_needed); |
| 205 | * } |
| 206 | * |
| 207 | * \param[inout] STRBUF A struct osmo_strbuf instance. |
| 208 | * \param[in] func A function with a signature of int func(char *dst, size_t dst_len [, args]) with semantics like |
| 209 | * snprintf(). |
| 210 | * \param[in] args Arguments passed to func, if any. |
| 211 | */ |
| 212 | #define OSMO_STRBUF_APPEND(STRBUF, func, args...) do { \ |
| 213 | if (!(STRBUF).pos) \ |
| 214 | (STRBUF).pos = (STRBUF).buf; \ |
Neels Hofmeyr | 8531d66 | 2019-04-11 07:16:02 +0200 | [diff] [blame] | 215 | size_t _sb_remain = (STRBUF).buf ? (STRBUF).len - ((STRBUF).pos - (STRBUF).buf) : 0; \ |
| 216 | int _sb_l = func((STRBUF).pos, _sb_remain, ##args); \ |
| 217 | if (_sb_l < 0 || _sb_l > _sb_remain) \ |
Neels Hofmeyr | 2cbe25f | 2019-02-11 20:32:06 +0100 | [diff] [blame] | 218 | (STRBUF).pos = (STRBUF).buf + (STRBUF).len; \ |
Neels Hofmeyr | 8531d66 | 2019-04-11 07:16:02 +0200 | [diff] [blame] | 219 | else if ((STRBUF).pos) \ |
| 220 | (STRBUF).pos += _sb_l; \ |
| 221 | if (_sb_l > 0) \ |
| 222 | (STRBUF).chars_needed += _sb_l; \ |
Neels Hofmeyr | 2cbe25f | 2019-02-11 20:32:06 +0100 | [diff] [blame] | 223 | } while(0) |
| 224 | |
| 225 | /*! Shortcut for OSMO_STRBUF_APPEND() invocation using snprintf(). |
| 226 | * |
| 227 | * int strbuf_example2(char *buf, size_t buflen) |
| 228 | * { |
| 229 | * int i; |
| 230 | * struct osmo_strbuf sb = { .buf = buf, .len = buflen }; |
| 231 | * |
| 232 | * OSMO_STRBUF_PRINTF(sb, "T minus"); |
| 233 | * for (i = 10; i; i--) |
| 234 | * OSMO_STRBUF_PRINTF(sb, " %d", i); |
| 235 | * OSMO_STRBUF_PRINTF(sb, " ... Lift off!"); |
| 236 | * |
| 237 | * return sb.chars_needed; |
| 238 | * } |
| 239 | * |
| 240 | * \param[inout] STRBUF A struct osmo_strbuf instance. |
| 241 | * \param[in] fmt Format string passed to snprintf. |
| 242 | * \param[in] args Additional arguments passed to snprintf, if any. |
| 243 | */ |
| 244 | #define OSMO_STRBUF_PRINTF(STRBUF, fmt, args...) \ |
| 245 | OSMO_STRBUF_APPEND(STRBUF, snprintf, fmt, ##args) |
| 246 | |
Neels Hofmeyr | 8531d66 | 2019-04-11 07:16:02 +0200 | [diff] [blame] | 247 | /*! Like OSMO_STRBUF_APPEND(), but for function signatures that return the char* buffer instead of a length. |
| 248 | * When using this function, the final STRBUF.chars_needed may not reflect the actual number of characters needed, since |
| 249 | * that number cannot be obtained from this kind of function signature. |
| 250 | * \param[inout] STRBUF A struct osmo_strbuf instance. |
| 251 | * \param[in] func A function with a signature of char *func(char *dst, size_t dst_len [, args]) where |
| 252 | * the returned string is always written to dst. |
| 253 | * \param[in] args Arguments passed to func, if any. |
| 254 | */ |
| 255 | #define OSMO_STRBUF_APPEND_NOLEN(STRBUF, func, args...) do { \ |
| 256 | if (!(STRBUF).pos) \ |
| 257 | (STRBUF).pos = (STRBUF).buf; \ |
| 258 | size_t _sb_remain = (STRBUF).buf ? (STRBUF).len - ((STRBUF).pos - (STRBUF).buf) : 0; \ |
| 259 | if (_sb_remain) { \ |
| 260 | func((STRBUF).pos, _sb_remain, ##args); \ |
| 261 | } \ |
| 262 | size_t _sb_l = (STRBUF).pos ? strnlen((STRBUF).pos, _sb_remain) : 0; \ |
| 263 | if (_sb_l > _sb_remain) \ |
| 264 | (STRBUF).pos = (STRBUF).buf + (STRBUF).len; \ |
| 265 | else if ((STRBUF).pos) \ |
| 266 | (STRBUF).pos += _sb_l; \ |
| 267 | (STRBUF).chars_needed += _sb_l; \ |
| 268 | } while(0) |
| 269 | |
Neels Hofmeyr | d79ccc6 | 2019-03-07 23:08:40 +0100 | [diff] [blame] | 270 | bool osmo_str_startswith(const char *str, const char *startswith_str); |
| 271 | |
Sylvain Munaut | dca7d2c | 2012-04-18 21:53:23 +0200 | [diff] [blame] | 272 | /*! @} */ |