2014-06-16 08:13:40 +00:00
|
|
|
#pragma once
|
2010-02-20 19:34:29 +00:00
|
|
|
|
2017-10-07 02:39:14 +00:00
|
|
|
#include <stdbool.h>
|
|
|
|
|
2013-07-06 21:49:41 +00:00
|
|
|
#include <osmocom/core/backtrace.h>
|
2014-08-20 20:50:47 +00:00
|
|
|
#include <osmocom/core/talloc.h>
|
2013-07-06 21:49:41 +00:00
|
|
|
|
2011-08-17 12:19:27 +00:00
|
|
|
/*! \defgroup utils General-purpose utility functions
|
|
|
|
* @{
|
2017-06-20 02:35:06 +00:00
|
|
|
* \file utils.h */
|
2011-08-17 12:19:27 +00:00
|
|
|
|
2017-06-19 22:17:59 +00:00
|
|
|
/*! Determine number of elements in an array of static size */
|
2010-02-20 19:34:29 +00:00
|
|
|
#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
|
2017-06-19 22:17:59 +00:00
|
|
|
/*! Return the maximum of two specified values */
|
2012-08-15 15:02:59 +00:00
|
|
|
#define OSMO_MAX(a, b) ((a) >= (b) ? (a) : (b))
|
2017-06-19 22:17:59 +00:00
|
|
|
/*! Return the minimum of two specified values */
|
2012-08-15 15:02:59 +00:00
|
|
|
#define OSMO_MIN(a, b) ((a) >= (b) ? (b) : (a))
|
2017-06-19 22:17:59 +00:00
|
|
|
/*! Stringify the contents of a macro, e.g. a port number */
|
2016-12-16 12:43:54 +00:00
|
|
|
#define OSMO_STRINGIFY(x) #x
|
2017-06-19 22:17:59 +00:00
|
|
|
/*! Make a value_string entry from an enum value name */
|
2017-03-09 22:01:37 +00:00
|
|
|
#define OSMO_VALUE_STRING(x) { x, #x }
|
2017-06-19 22:17:59 +00:00
|
|
|
/*! Number of bytes necessary to store given BITS */
|
2017-06-13 13:07:01 +00:00
|
|
|
#define OSMO_BYTES_FOR_BITS(BITS) ((BITS + 8 - 1) / 8)
|
2010-02-20 19:34:29 +00:00
|
|
|
|
2010-03-01 20:58:31 +00:00
|
|
|
#include <stdint.h>
|
2017-01-15 16:59:21 +00:00
|
|
|
#include <stdio.h>
|
2010-03-01 20:58:31 +00:00
|
|
|
|
2017-06-19 22:17:59 +00:00
|
|
|
/*! A mapping between human-readable string and numeric value */
|
2010-03-01 20:58:31 +00:00
|
|
|
struct value_string {
|
2017-06-19 22:17:59 +00:00
|
|
|
unsigned int value; /*!< numeric value */
|
|
|
|
const char *str; /*!< human-readable string */
|
2010-03-01 20:58:31 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const char *get_value_string(const struct value_string *vs, uint32_t val);
|
2016-06-06 16:05:23 +00:00
|
|
|
const char *get_value_string_or_null(const struct value_string *vs,
|
|
|
|
uint32_t val);
|
2011-08-16 21:26:52 +00:00
|
|
|
|
2010-03-01 20:58:31 +00:00
|
|
|
int get_string_value(const struct value_string *vs, const char *str);
|
|
|
|
|
2011-05-07 10:43:08 +00:00
|
|
|
char osmo_bcd2char(uint8_t bcd);
|
2010-03-04 09:50:32 +00:00
|
|
|
/* only works for numbers in ascci */
|
2011-05-07 10:43:08 +00:00
|
|
|
uint8_t osmo_char2bcd(char c);
|
2010-03-01 20:58:31 +00:00
|
|
|
|
2011-05-07 10:43:08 +00:00
|
|
|
int osmo_hexparse(const char *str, uint8_t *b, int max_len);
|
2011-08-16 21:26:52 +00:00
|
|
|
|
2011-08-17 12:19:27 +00:00
|
|
|
char *osmo_ubit_dump(const uint8_t *bits, unsigned int len);
|
2011-05-07 10:43:08 +00:00
|
|
|
char *osmo_hexdump(const unsigned char *buf, int len);
|
2011-11-10 22:03:18 +00:00
|
|
|
char *osmo_hexdump_nospc(const unsigned char *buf, int len);
|
2011-11-13 22:04:00 +00:00
|
|
|
char *osmo_osmo_hexdump_nospc(const unsigned char *buf, int len) __attribute__((__deprecated__));
|
2011-08-16 21:26:52 +00:00
|
|
|
|
2015-04-13 07:21:05 +00:00
|
|
|
#define osmo_static_assert(exp, name) typedef int dummy##name [(exp) ? 1 : -1] __attribute__((__unused__));
|
2011-01-16 16:37:27 +00:00
|
|
|
|
2011-02-18 19:37:04 +00:00
|
|
|
void osmo_str2lower(char *out, const char *in);
|
|
|
|
void osmo_str2upper(char *out, const char *in);
|
|
|
|
|
2011-03-28 17:24:22 +00:00
|
|
|
#define OSMO_SNPRINTF_RET(ret, rem, offset, len) \
|
|
|
|
do { \
|
|
|
|
len += ret; \
|
|
|
|
if (ret > rem) \
|
|
|
|
ret = rem; \
|
|
|
|
offset += ret; \
|
|
|
|
rem -= ret; \
|
|
|
|
} while (0)
|
|
|
|
|
2016-04-25 10:11:20 +00:00
|
|
|
/*! Helper macro to terminate when an assertion failes
|
|
|
|
* \param[in] exp Predicate to verify
|
|
|
|
* This function will generate a backtrace and terminate the program if
|
|
|
|
* the predicate evaluates to false (0).
|
|
|
|
*/
|
2013-03-03 10:36:52 +00:00
|
|
|
#define OSMO_ASSERT(exp) \
|
|
|
|
if (!(exp)) { \
|
2016-05-10 13:23:06 +00:00
|
|
|
fprintf(stderr, "Assert failed %s %s:%d\n", #exp, __BASE_FILE__, __LINE__); \
|
2013-03-12 09:23:52 +00:00
|
|
|
osmo_generate_backtrace(); \
|
2013-03-03 10:36:52 +00:00
|
|
|
abort(); \
|
|
|
|
}
|
|
|
|
|
2016-04-25 10:11:20 +00:00
|
|
|
/*! duplicate a string using talloc and release its prior content (if any)
|
|
|
|
* \param[in] ctx Talloc context to use for allocation
|
|
|
|
* \param[out] dst pointer to string, will be updated with ptr to new string
|
|
|
|
* \param[in] newstr String that will be copieed to newly allocated string */
|
2016-07-28 06:02:48 +00:00
|
|
|
static inline void osmo_talloc_replace_string(void *ctx, char **dst, const char *newstr)
|
2014-08-20 20:50:47 +00:00
|
|
|
{
|
|
|
|
if (*dst)
|
|
|
|
talloc_free(*dst);
|
|
|
|
*dst = talloc_strdup(ctx, newstr);
|
|
|
|
}
|
2013-03-03 10:36:52 +00:00
|
|
|
|
2016-04-25 16:47:53 +00:00
|
|
|
int osmo_constant_time_cmp(const uint8_t *exp, const uint8_t *rel, const int count);
|
|
|
|
uint64_t osmo_decode_big_endian(const uint8_t *data, size_t data_len);
|
|
|
|
uint8_t *osmo_encode_big_endian(uint64_t value, size_t data_len);
|
|
|
|
|
2016-11-26 09:41:40 +00:00
|
|
|
size_t osmo_strlcpy(char *dst, const char *src, size_t siz);
|
|
|
|
|
2017-10-07 02:39:14 +00:00
|
|
|
bool osmo_is_hexstr(const char *str, int min_digits, int max_digits,
|
|
|
|
bool require_even);
|
|
|
|
|
2012-04-18 19:53:23 +00:00
|
|
|
/*! @} */
|