blob: 41bbc2799d4ad1cccd343bf44321a20005fb4d62 [file] [log] [blame]
Sylvain Munaut12ba7782014-06-16 10:13:40 +02001#pragma once
Harald Welteec8b4502010-02-20 20:34:29 +01002
Harald Welteb8add362013-07-06 23:49:41 +02003#include <osmocom/core/backtrace.h>
Harald Weltef3c7e852014-08-20 22:50:47 +02004#include <osmocom/core/talloc.h>
Harald Welteb8add362013-07-06 23:49:41 +02005
Harald Welte8598f182011-08-17 14:19:27 +02006/*! \defgroup utils General-purpose utility functions
7 * @{
Harald Weltebd598e32011-08-16 23:26:52 +02008 */
9
Harald Welte8598f182011-08-17 14:19:27 +020010/*! \file utils.h */
11
Harald Weltebd598e32011-08-16 23:26:52 +020012/*! \brief Determine number of elements in an array of static size */
Harald Welteec8b4502010-02-20 20:34:29 +010013#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
Harald Weltebd598e32011-08-16 23:26:52 +020014/*! \brief Return the maximum of two specified values */
Holger Hans Peter Freyther08b28622012-08-15 17:02:59 +020015#define OSMO_MAX(a, b) ((a) >= (b) ? (a) : (b))
Harald Weltebd598e32011-08-16 23:26:52 +020016/*! \brief Return the minimum of two specified values */
Holger Hans Peter Freyther08b28622012-08-15 17:02:59 +020017#define OSMO_MIN(a, b) ((a) >= (b) ? (b) : (a))
Neels Hofmeyr18080962016-12-16 13:43:54 +010018/*! \brief Stringify the contents of a macro, e.g. a port number */
19#define OSMO_STRINGIFY(x) #x
20/*! \brief Make a value_string entry from an enum value name */
21#define OSMO_VALUE_STRING(x) { x, OSMO_STRINGIFY(x) }
Harald Welteec8b4502010-02-20 20:34:29 +010022
Harald Welted284cd92010-03-01 21:58:31 +010023#include <stdint.h>
24
Harald Weltebd598e32011-08-16 23:26:52 +020025/*! \brief A mapping between human-readable string and numeric value */
Harald Welted284cd92010-03-01 21:58:31 +010026struct value_string {
Harald Weltebd598e32011-08-16 23:26:52 +020027 unsigned int value; /*!< \brief numeric value */
28 const char *str; /*!< \brief human-readable string */
Harald Welted284cd92010-03-01 21:58:31 +010029};
30
31const char *get_value_string(const struct value_string *vs, uint32_t val);
Neels Hofmeyr8d6dcd92016-06-06 18:05:23 +020032const char *get_value_string_or_null(const struct value_string *vs,
33 uint32_t val);
Harald Weltebd598e32011-08-16 23:26:52 +020034
Harald Welted284cd92010-03-01 21:58:31 +010035int get_string_value(const struct value_string *vs, const char *str);
36
Pablo Neira Ayuso87f7b252011-05-07 12:43:08 +020037char osmo_bcd2char(uint8_t bcd);
Harald Weltea73e2f92010-03-04 10:50:32 +010038/* only works for numbers in ascci */
Pablo Neira Ayuso87f7b252011-05-07 12:43:08 +020039uint8_t osmo_char2bcd(char c);
Harald Welted284cd92010-03-01 21:58:31 +010040
Pablo Neira Ayuso87f7b252011-05-07 12:43:08 +020041int osmo_hexparse(const char *str, uint8_t *b, int max_len);
Harald Weltebd598e32011-08-16 23:26:52 +020042
Pablo Neira Ayuso87f7b252011-05-07 12:43:08 +020043char *osmo_ubit_dump(const uint8_t *bits, unsigned int len);
Harald Welte8598f182011-08-17 14:19:27 +020044char *osmo_hexdump(const unsigned char *buf, int len);
Sylvain Munautff23d242011-11-10 23:03:18 +010045char *osmo_hexdump_nospc(const unsigned char *buf, int len);
Sylvain Munaut4cfbae82011-11-13 23:04:00 +010046char *osmo_osmo_hexdump_nospc(const unsigned char *buf, int len) __attribute__((__deprecated__));
Harald Welte3eba9912010-07-30 10:37:29 +020047
Holger Hans Peter Freyther0f40ae12015-04-13 09:21:05 +020048#define osmo_static_assert(exp, name) typedef int dummy##name [(exp) ? 1 : -1] __attribute__((__unused__));
Holger Hans Peter Freyther52c07ca2011-01-16 17:37:27 +010049
Harald Welte28222962011-02-18 20:37:04 +010050void osmo_str2lower(char *out, const char *in);
51void osmo_str2upper(char *out, const char *in);
52
Pablo Neira Ayuso3abad6a2011-03-28 19:24:22 +020053#define OSMO_SNPRINTF_RET(ret, rem, offset, len) \
54do { \
55 len += ret; \
56 if (ret > rem) \
57 ret = rem; \
58 offset += ret; \
59 rem -= ret; \
60} while (0)
61
Harald Welte2d2e2cc2016-04-25 12:11:20 +020062/*! Helper macro to terminate when an assertion failes
63 * \param[in] exp Predicate to verify
64 * This function will generate a backtrace and terminate the program if
65 * the predicate evaluates to false (0).
66 */
Katerina Barone-Adesi008e53b2013-03-03 10:36:52 +000067#define OSMO_ASSERT(exp) \
68 if (!(exp)) { \
Harald Welte3d792402016-05-10 15:23:06 +020069 fprintf(stderr, "Assert failed %s %s:%d\n", #exp, __BASE_FILE__, __LINE__); \
Katerina Barone-Adesi55cf0222013-03-12 10:23:52 +010070 osmo_generate_backtrace(); \
Katerina Barone-Adesi008e53b2013-03-03 10:36:52 +000071 abort(); \
72 }
73
Harald Welte2d2e2cc2016-04-25 12:11:20 +020074/*! duplicate a string using talloc and release its prior content (if any)
75 * \param[in] ctx Talloc context to use for allocation
76 * \param[out] dst pointer to string, will be updated with ptr to new string
77 * \param[in] newstr String that will be copieed to newly allocated string */
Harald Welte881dcaf2016-07-28 08:02:48 +020078static inline void osmo_talloc_replace_string(void *ctx, char **dst, const char *newstr)
Harald Weltef3c7e852014-08-20 22:50:47 +020079{
80 if (*dst)
81 talloc_free(*dst);
82 *dst = talloc_strdup(ctx, newstr);
83}
Katerina Barone-Adesi008e53b2013-03-03 10:36:52 +000084
Harald Welte9709b2e2016-04-25 18:47:53 +020085int osmo_constant_time_cmp(const uint8_t *exp, const uint8_t *rel, const int count);
86uint64_t osmo_decode_big_endian(const uint8_t *data, size_t data_len);
87uint8_t *osmo_encode_big_endian(uint64_t value, size_t data_len);
88
Harald Welteaeecc482016-11-26 10:41:40 +010089size_t osmo_strlcpy(char *dst, const char *src, size_t siz);
90
Sylvain Munautdca7d2c2012-04-18 21:53:23 +020091/*! @} */