blob: 0b54c880bcabeb69ba7b5fafc0aaf2b60dcd9e93 [file] [log] [blame]
Sylvain Munaut12ba7782014-06-16 10:13:40 +02001#pragma once
Harald Welteec8b4502010-02-20 20:34:29 +01002
Neels Hofmeyr4335bad2017-10-07 04:39:14 +02003#include <stdbool.h>
Vadim Yanitskiy55944302018-09-05 02:58:03 +07004#include <stdint.h>
5#include <stdio.h>
Neels Hofmeyr4335bad2017-10-07 04:39:14 +02006
Harald Welteb8add362013-07-06 23:49:41 +02007#include <osmocom/core/backtrace.h>
Harald Weltef3c7e852014-08-20 22:50:47 +02008#include <osmocom/core/talloc.h>
Harald Welte459a1802018-06-28 09:24:17 +02009#include <osmocom/core/panic.h>
Neels Hofmeyr7c749892018-09-07 03:01:38 +020010#include <osmocom/core/defs.h>
Harald Welteb8add362013-07-06 23:49:41 +020011
Harald Welte8598f182011-08-17 14:19:27 +020012/*! \defgroup utils General-purpose utility functions
13 * @{
Neels Hofmeyr17518fe2017-06-20 04:35:06 +020014 * \file utils.h */
Harald Welte8598f182011-08-17 14:19:27 +020015
Neels Hofmeyr87e45502017-06-20 00:17:59 +020016/*! Determine number of elements in an array of static size */
Harald Welteec8b4502010-02-20 20:34:29 +010017#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
Neels Hofmeyr87e45502017-06-20 00:17:59 +020018/*! Return the maximum of two specified values */
Holger Hans Peter Freyther08b28622012-08-15 17:02:59 +020019#define OSMO_MAX(a, b) ((a) >= (b) ? (a) : (b))
Neels Hofmeyr87e45502017-06-20 00:17:59 +020020/*! Return the minimum of two specified values */
Holger Hans Peter Freyther08b28622012-08-15 17:02:59 +020021#define OSMO_MIN(a, b) ((a) >= (b) ? (b) : (a))
Neels Hofmeyrdc75b112017-12-09 05:51:42 +010022/*! Stringify the name of a macro x, e.g. an FSM event name.
23 * Note: if nested within another preprocessor macro, this will
24 * stringify the value of x instead of its name. */
Neels Hofmeyr18080962016-12-16 13:43:54 +010025#define OSMO_STRINGIFY(x) #x
Neels Hofmeyr84ea2e02017-12-09 05:53:18 +010026/*! Stringify the value of a macro x, e.g. a port number. */
27#define OSMO_STRINGIFY_VAL(x) OSMO_STRINGIFY(x)
Neels Hofmeyr87e45502017-06-20 00:17:59 +020028/*! Make a value_string entry from an enum value name */
Neels Hofmeyr8a5d60b2017-03-09 23:01:37 +010029#define OSMO_VALUE_STRING(x) { x, #x }
Neels Hofmeyr87e45502017-06-20 00:17:59 +020030/*! Number of bytes necessary to store given BITS */
Maxdd75bac2017-06-13 15:07:01 +020031#define OSMO_BYTES_FOR_BITS(BITS) ((BITS + 8 - 1) / 8)
Harald Welteec8b4502010-02-20 20:34:29 +010032
Max18c014d2018-01-30 14:33:01 +010033/*! Copy a C-string into a sized buffer using sizeof to detect buffer's size */
34#define OSMO_STRLCPY_ARRAY(array, src) osmo_strlcpy(array, src, sizeof(array))
35
Neels Hofmeyr87e45502017-06-20 00:17:59 +020036/*! A mapping between human-readable string and numeric value */
Harald Welted284cd92010-03-01 21:58:31 +010037struct value_string {
Neels Hofmeyr87e45502017-06-20 00:17:59 +020038 unsigned int value; /*!< numeric value */
39 const char *str; /*!< human-readable string */
Harald Welted284cd92010-03-01 21:58:31 +010040};
41
42const char *get_value_string(const struct value_string *vs, uint32_t val);
Neels Hofmeyr8d6dcd92016-06-06 18:05:23 +020043const char *get_value_string_or_null(const struct value_string *vs,
44 uint32_t val);
Harald Weltebd598e32011-08-16 23:26:52 +020045
Harald Welted284cd92010-03-01 21:58:31 +010046int get_string_value(const struct value_string *vs, const char *str);
47
Pablo Neira Ayuso87f7b252011-05-07 12:43:08 +020048char osmo_bcd2char(uint8_t bcd);
Harald Weltea73e2f92010-03-04 10:50:32 +010049/* only works for numbers in ascci */
Pablo Neira Ayuso87f7b252011-05-07 12:43:08 +020050uint8_t osmo_char2bcd(char c);
Harald Welted284cd92010-03-01 21:58:31 +010051
Pablo Neira Ayuso87f7b252011-05-07 12:43:08 +020052int osmo_hexparse(const char *str, uint8_t *b, int max_len);
Harald Weltebd598e32011-08-16 23:26:52 +020053
Pablo Neira Ayuso87f7b252011-05-07 12:43:08 +020054char *osmo_ubit_dump(const uint8_t *bits, unsigned int len);
Harald Welte8598f182011-08-17 14:19:27 +020055char *osmo_hexdump(const unsigned char *buf, int len);
Sylvain Munautff23d242011-11-10 23:03:18 +010056char *osmo_hexdump_nospc(const unsigned char *buf, int len);
Sylvain Munaut4cfbae82011-11-13 23:04:00 +010057char *osmo_osmo_hexdump_nospc(const unsigned char *buf, int len) __attribute__((__deprecated__));
Harald Welte3eba9912010-07-30 10:37:29 +020058
Holger Hans Peter Freyther0f40ae12015-04-13 09:21:05 +020059#define osmo_static_assert(exp, name) typedef int dummy##name [(exp) ? 1 : -1] __attribute__((__unused__));
Holger Hans Peter Freyther52c07ca2011-01-16 17:37:27 +010060
Neels Hofmeyr7c749892018-09-07 03:01:38 +020061void osmo_str2lower(char *out, const char *in)
62 OSMO_DEPRECATED("Use osmo_str_tolower() or osmo_str_tolower_buf() instead,"
63 " to properly check target memory bounds");
64void osmo_str2upper(char *out, const char *in)
65 OSMO_DEPRECATED("Use osmo_str_toupper() or osmo_str_toupper_buf() instead,"
66 " to properly check target memory bounds");
67
68size_t osmo_str_tolower_buf(char *dest, size_t dest_len, const char *src);
69const char *osmo_str_tolower(const char *src);
70
71size_t osmo_str_toupper_buf(char *dest, size_t dest_len, const char *src);
72const char *osmo_str_toupper(const char *src);
Harald Welte28222962011-02-18 20:37:04 +010073
Pablo Neira Ayuso3abad6a2011-03-28 19:24:22 +020074#define OSMO_SNPRINTF_RET(ret, rem, offset, len) \
75do { \
76 len += ret; \
77 if (ret > rem) \
78 ret = rem; \
79 offset += ret; \
80 rem -= ret; \
81} while (0)
82
Harald Welte2d2e2cc2016-04-25 12:11:20 +020083/*! Helper macro to terminate when an assertion failes
84 * \param[in] exp Predicate to verify
85 * This function will generate a backtrace and terminate the program if
86 * the predicate evaluates to false (0).
87 */
Katerina Barone-Adesi008e53b2013-03-03 10:36:52 +000088#define OSMO_ASSERT(exp) \
89 if (!(exp)) { \
Neels Hofmeyr983dcb92018-08-20 12:33:22 +020090 osmo_panic("Assert failed %s %s:%d\n", #exp, __FILE__, __LINE__); \
Katerina Barone-Adesi008e53b2013-03-03 10:36:52 +000091 }
92
Harald Welte2d2e2cc2016-04-25 12:11:20 +020093/*! duplicate a string using talloc and release its prior content (if any)
94 * \param[in] ctx Talloc context to use for allocation
95 * \param[out] dst pointer to string, will be updated with ptr to new string
96 * \param[in] newstr String that will be copieed to newly allocated string */
Harald Welte881dcaf2016-07-28 08:02:48 +020097static inline void osmo_talloc_replace_string(void *ctx, char **dst, const char *newstr)
Harald Weltef3c7e852014-08-20 22:50:47 +020098{
99 if (*dst)
100 talloc_free(*dst);
101 *dst = talloc_strdup(ctx, newstr);
102}
Katerina Barone-Adesi008e53b2013-03-03 10:36:52 +0000103
Neels Hofmeyrabdd7a22017-10-19 02:40:01 +0200104/*! Append to a string and re-/allocate if necessary.
105 * \param[in] ctx Talloc context to use for initial allocation.
106 * \param[in,out] dest char* to re-/allocate and append to.
107 * \param[in] fmt printf-like string format.
108 * \param[in] args Arguments for fmt.
109 *
110 * \a dest may be passed in NULL, or a string previously allocated by talloc.
111 * If an existing string is passed in, it will remain associated with whichever
112 * ctx it was allocated before, regardless whether it matches \a ctx or not.
113 */
114#define osmo_talloc_asprintf(ctx, dest, fmt, args ...) \
115 do { \
116 if (!dest) \
117 dest = talloc_asprintf(ctx, fmt, ## args); \
118 else \
119 dest = talloc_asprintf_append((char*)dest, fmt, ## args); \
120 } while (0)
121
Harald Welte9709b2e2016-04-25 18:47:53 +0200122int osmo_constant_time_cmp(const uint8_t *exp, const uint8_t *rel, const int count);
123uint64_t osmo_decode_big_endian(const uint8_t *data, size_t data_len);
124uint8_t *osmo_encode_big_endian(uint64_t value, size_t data_len);
125
Harald Welteaeecc482016-11-26 10:41:40 +0100126size_t osmo_strlcpy(char *dst, const char *src, size_t siz);
127
Neels Hofmeyr4335bad2017-10-07 04:39:14 +0200128bool osmo_is_hexstr(const char *str, int min_digits, int max_digits,
129 bool require_even);
130
Harald Weltefebe83c2017-10-03 17:41:59 +0800131bool osmo_identifier_valid(const char *str);
Neels Hofmeyr937ddea2017-12-16 00:46:50 +0100132bool osmo_separated_identifiers_valid(const char *str, const char *sep_chars);
Harald Weltefebe83c2017-10-03 17:41:59 +0800133
Neels Hofmeyr9910bbc2017-12-16 00:54:52 +0100134const char *osmo_escape_str(const char *str, int len);
135const char *osmo_escape_str_buf(const char *str, int in_len, char *buf, size_t bufsize);
Neels Hofmeyr04eb56f2018-04-09 00:41:28 +0200136const char *osmo_quote_str(const char *str, int in_len);
137const char *osmo_quote_str_buf(const char *str, int in_len, char *buf, size_t bufsize);
Neels Hofmeyr9910bbc2017-12-16 00:54:52 +0100138
Harald Welte15a5f8d2018-06-06 16:58:17 +0200139uint32_t osmo_isqrt32(uint32_t x);
140
Sylvain Munautdca7d2c2012-04-18 21:53:23 +0200141/*! @} */