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>
|
2018-09-04 19:58:03 +00:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdio.h>
|
2017-10-07 02:39:14 +00:00
|
|
|
|
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>
|
2018-06-28 07:24:17 +00:00
|
|
|
#include <osmocom/core/panic.h>
|
2018-09-07 01:01:38 +00:00
|
|
|
#include <osmocom/core/defs.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-12-09 04:51:42 +00:00
|
|
|
/*! Stringify the name of a macro x, e.g. an FSM event name.
|
|
|
|
* Note: if nested within another preprocessor macro, this will
|
|
|
|
* stringify the value of x instead of its name. */
|
2016-12-16 12:43:54 +00:00
|
|
|
#define OSMO_STRINGIFY(x) #x
|
2017-12-09 04:53:18 +00:00
|
|
|
/*! Stringify the value of a macro x, e.g. a port number. */
|
|
|
|
#define OSMO_STRINGIFY_VAL(x) OSMO_STRINGIFY(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
|
|
|
|
2018-01-30 13:33:01 +00:00
|
|
|
/*! Copy a C-string into a sized buffer using sizeof to detect buffer's size */
|
|
|
|
#define OSMO_STRLCPY_ARRAY(array, src) osmo_strlcpy(array, src, sizeof(array))
|
|
|
|
|
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
|
|
|
|
2018-12-05 20:02:36 +00:00
|
|
|
int osmo_bcd2str(char *dst, size_t dst_size, const uint8_t *bcd, int start_nibble, int end_nibble, bool allow_hex);
|
|
|
|
|
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
|
|
|
|
2018-09-07 01:01:38 +00:00
|
|
|
void osmo_str2lower(char *out, const char *in)
|
|
|
|
OSMO_DEPRECATED("Use osmo_str_tolower() or osmo_str_tolower_buf() instead,"
|
|
|
|
" to properly check target memory bounds");
|
|
|
|
void osmo_str2upper(char *out, const char *in)
|
|
|
|
OSMO_DEPRECATED("Use osmo_str_toupper() or osmo_str_toupper_buf() instead,"
|
|
|
|
" to properly check target memory bounds");
|
|
|
|
|
|
|
|
size_t osmo_str_tolower_buf(char *dest, size_t dest_len, const char *src);
|
|
|
|
const char *osmo_str_tolower(const char *src);
|
|
|
|
|
|
|
|
size_t osmo_str_toupper_buf(char *dest, size_t dest_len, const char *src);
|
|
|
|
const char *osmo_str_toupper(const char *src);
|
2011-02-18 19:37:04 +00:00
|
|
|
|
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)) { \
|
2018-08-20 10:33:22 +00:00
|
|
|
osmo_panic("Assert failed %s %s:%d\n", #exp, __FILE__, __LINE__); \
|
2013-03-03 10:36:52 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2017-10-19 00:40:01 +00:00
|
|
|
/*! Append to a string and re-/allocate if necessary.
|
|
|
|
* \param[in] ctx Talloc context to use for initial allocation.
|
|
|
|
* \param[in,out] dest char* to re-/allocate and append to.
|
|
|
|
* \param[in] fmt printf-like string format.
|
|
|
|
* \param[in] args Arguments for fmt.
|
|
|
|
*
|
|
|
|
* \a dest may be passed in NULL, or a string previously allocated by talloc.
|
|
|
|
* If an existing string is passed in, it will remain associated with whichever
|
|
|
|
* ctx it was allocated before, regardless whether it matches \a ctx or not.
|
|
|
|
*/
|
|
|
|
#define osmo_talloc_asprintf(ctx, dest, fmt, args ...) \
|
|
|
|
do { \
|
|
|
|
if (!dest) \
|
|
|
|
dest = talloc_asprintf(ctx, fmt, ## args); \
|
|
|
|
else \
|
|
|
|
dest = talloc_asprintf_append((char*)dest, fmt, ## args); \
|
|
|
|
} while (0)
|
|
|
|
|
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);
|
|
|
|
|
2017-10-03 09:41:59 +00:00
|
|
|
bool osmo_identifier_valid(const char *str);
|
2017-12-15 23:46:50 +00:00
|
|
|
bool osmo_separated_identifiers_valid(const char *str, const char *sep_chars);
|
2017-10-03 09:41:59 +00:00
|
|
|
|
2017-12-15 23:54:52 +00:00
|
|
|
const char *osmo_escape_str(const char *str, int len);
|
|
|
|
const char *osmo_escape_str_buf(const char *str, int in_len, char *buf, size_t bufsize);
|
add osmo_quote_str(),osmo_quote_str_buf() and test
Rationale: with osmo_escape_str(), you get the escaped contents of the string,
but not so graceful handling of NULL strings. The caller needs to quote it, and
for NULL strings not quote it.
osmo_quote_str() is like osmo_escape_str() but always quotes a non-NULL string,
and for a NULL string returns a literal NULL, i.e. it should (tm) give the
exact C representation of a string.
That's useful in testing, to show exactly what char* situation we have, without
jumping through hoops like
if (str)
printf("\"%s\"", osmo_escape_str(str, -1));
else
printf("NULL");
Copy the unit test for osmo_escape_str() and adjust. To indicate that the
double quotes are returned by osmo_quote_str(), use single quotes in the test
printf()s.
I considered allowing to pick the quoting characters by further arguments, but
that complicates things: we'd need to escape the quoting characters. Just
hardcode double quotes like C.
Change-Id: I6f1b3709b32c23fc52f70ad9ecc9439c62b02a12
2018-04-08 22:41:28 +00:00
|
|
|
const char *osmo_quote_str(const char *str, int in_len);
|
|
|
|
const char *osmo_quote_str_buf(const char *str, int in_len, char *buf, size_t bufsize);
|
2017-12-15 23:54:52 +00:00
|
|
|
|
2018-06-06 14:58:17 +00:00
|
|
|
uint32_t osmo_isqrt32(uint32_t x);
|
|
|
|
|
2019-01-11 12:13:37 +00:00
|
|
|
const char osmo_luhn(const char* in, int in_len);
|
|
|
|
|
2012-04-18 19:53:23 +00:00
|
|
|
/*! @} */
|