libosmocore 1.13.1.1-afb4 Osmocom core library

Data Structures | Macros | Functions | Variables
utils.h File Reference
General-purpose utility functions
`#include <stdbool.h>`
`#include <stdint.h>`
`#include <stdio.h>`
`#include <string.h>`
`#include <osmocom/core/backtrace.h>`
`#include <osmocom/core/talloc.h>`
`#include <osmocom/core/panic.h>`
`#include <osmocom/core/defs.h>`

Go to the source code of this file.

## Data Structures
struct | value_string
| A mapping between human-readable string and numeric value. More...
struct | osmo_strbuf
| State for OSMO_STRBUF_APPEND() and OSMO_STRBUF_PRINTF(). More...

## Macros
#define | ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
| Determine number of elements in an array of static size. More...
#define | OSMO_MAX(a, b) ((a) >= (b) ? (a) : (b))
| Return the maximum of two specified values. More...
#define | OSMO_MIN(a, b) ((a) >= (b) ? (b) : (a))
| Return the minimum of two specified values. More...
#define | OSMO_CMP(a, b) ((a) < (b)? -1 : ((a) > (b)? 1 : 0))
| Return a typical cmp result for comparable entities a and b. More...
#define | OSMO_STRINGIFY(x) #x
| Stringify the name of a macro x, e.g. More...
#define | OSMO_STRINGIFY_VAL(x) OSMO_STRINGIFY(x)
| Stringify the value of a macro x, e.g. More...
#define | OSMO_VALUE_STRING(x) { x, #x }
| Make a value_string entry from an enum value name. More...
#define | OSMO_BYTES_FOR_BITS(BITS) (((BITS) + 7) / 8)
| Number of bytes necessary to store given BITS. More...
#define | OSMO_STRLCPY_ARRAY(array, src) osmo_strlcpy(array, src, sizeof(array))
| Copy a C-string into a sized buffer using sizeof to detect buffer's size. More...
#define | OSMO_LIKELY(exp) exp
| Branch prediction optimizations. More...
#define | OSMO_UNLIKELY(exp) exp
#define | osmo_static_assert(exp, name) typedef int dummy##name [(exp) ? 1 : -1] __attribute__((__unused__));
#define | OSMO_SNPRINTF_RET(ret, rem, offset, len)
#define | OSMO_ASSERT(exp)
| Helper macro to terminate when an assertion fails. More...
#define | osmo_talloc_asprintf(ctx, dest, fmt, args ...)
| Append to a string and re-/allocate if necessary. More...
#define | OSMO_MOD_FLR(x, y) (((x) > 0 && (y) < 0) || ((x) < 0 && (y) > 0) ? (x) % (y) + (y) : (x) % (y))
| Floored Modulo (See also: Daan Leijen, Division and Modulus for Computer Scientists). More...
#define | OSMO_MOD_EUC(x, y) ((x) % (y) < 0 ? (y) > 0 ? (x) % (y) + (y) : (x) % (y) - (y) : (x) % (y))
| Euclidean Modulo (See also: Daan Leijen, Division and Modulus for Computer Scientists). More...
#define | OSMO_STRBUF_APPEND(STRBUF, func, args...)
| Append a string to a buffer, as printed by an snprintf()-like function and with similar bounds checking. More...
#define | OSMO_STRBUF_PRINTF(STRBUF, fmt, args...) OSMO_STRBUF_APPEND(STRBUF, snprintf, fmt, ##args)
| Shortcut for OSMO_STRBUF_APPEND() invocation using snprintf(). More...
#define | OSMO_STRBUF_REMAIN(STRBUF) _osmo_strbuf_remain(&(STRBUF))
| Return remaining space for characters and terminating nul in the given struct osmo_strbuf. More...
#define | OSMO_STRBUF_CHAR_COUNT(STRBUF) _osmo_strbuf_char_count(&(STRBUF))
| Return number of actual characters contained in struct osmo_strbuf (without terminating nul). More...
#define | OSMO_STRBUF_APPEND_NOLEN(STRBUF, func, args...)
| Like OSMO_STRBUF_APPEND(), but for function signatures that return the char* buffer instead of a length. More...
#define | OSMO_STRBUF_DROP_TAIL(STRBUF, N_CHARS) osmo_strbuf_drop_tail(&(STRBUF), N_CHARS)
#define | OSMO_STRBUF_ADDED_TAIL(STRBUF, N_CHARS) osmo_strbuf_added_tail(&(STRBUF), N_CHARS)
#define | OSMO_NAME_C_IMPL(CTX, INITIAL_BUFSIZE, ON_ERROR, FUNC_BUF, FUNC_BUF_ARGS...)
| Translate a buffer function to a talloc context function. More...

## Functions
const char * | get_value_string (const struct value_string *vs, uint32_t val)
| get human-readable string for given value More...
const char * | get_value_string_or_null (const struct value_string *vs, uint32_t val)
| get human-readable string or NULL for given value More...
int | get_string_value (const struct value_string *vs, const char *str)
| get numeric value for given human-readable string More...
char | osmo_bcd2char (uint8_t bcd)
| Convert BCD-encoded digit into printable character. More...
uint8_t | osmo_char2bcd (char c)
| Convert number in ASCII to BCD value. More...
int | osmo_bcd2str (char *dst, size_t dst_size, const uint8_t *bcd, int start_nibble, int end_nibble, bool allow_hex)
| Convert BCD to string. More...
int | osmo_str2bcd (uint8_t *dst, size_t dst_size, const char *digits, int start_nibble, int end_nibble, bool allow_hex)
| Convert string to BCD. More...
int | osmo_hexparse (const char *str, uint8_t *b, unsigned int max_len)
| Parse a string containing hexadecimal digits. More...
char * | osmo_ubit_dump_buf (char *buf, size_t buf_len, const uint8_t *bits, unsigned int len)
| Convert a sequence of unpacked bits to ASCII string, in user-supplied buffer. More...
char * | osmo_ubit_dump (const uint8_t *bits, unsigned int len)
| Convert a sequence of unpacked bits to ASCII string, in static buffer. More...
char * | osmo_hexdump (const unsigned char *buf, int len)
| Convert binary sequence to hexadecimal ASCII string. More...
char * | osmo_hexdump_c (const void *ctx, const unsigned char *buf, int len)
| Convert binary sequence to hexadecimal ASCII string. More...
char * | osmo_hexdump_nospc (const unsigned char *buf, int len)
| Convert binary sequence to hexadecimal ASCII string. More...
char * | osmo_hexdump_nospc_c (const void *ctx, const unsigned char *buf, int len)
| Convert binary sequence to hexadecimal ASCII string. More...
const char * | osmo_hexdump_buf (char *out_buf, size_t out_buf_size, const unsigned char *buf, int len, const char *delim, bool delim_after_last)
| Convert binary sequence to hexadecimal ASCII string. More...
char * | osmo_osmo_hexdump_nospc (const unsigned char *buf, int len) __attribute__((__deprecated__))
void | osmo_str2lower (char *out, const char *in) OSMO_DEPRECATED("Use osmo_str_tolower() or osmo_str_tolower_buf() instead
void | osmo_str2upper (char *out, const char *in) OSMO_DEPRECATED("Use osmo_str_toupper() or osmo_str_toupper_buf() instead
size_t | osmo_str_tolower_buf (char *dest, size_t dest_len, const char *src)
| Convert a string to lowercase, while checking buffer size boundaries. More...
const char * | osmo_str_tolower (const char *src)
| Convert a string to lowercase, using a static buffer. More...
char * | osmo_str_tolower_c (const void *ctx, const char *src)
| Convert a string to lowercase, dynamically allocating the output from given talloc context See also osmo_str_tolower_buf(). More...
size_t | osmo_str_toupper_buf (char *dest, size_t dest_len, const char *src)
| Convert a string to uppercase, while checking buffer size boundaries. More...
const char * | osmo_str_toupper (const char *src)
| Convert a string to uppercase, using a static buffer. More...
char * | osmo_str_toupper_c (const void *ctx, const char *src)
| Convert a string to uppercase, dynamically allocating the output from given talloc context See also osmo_str_tolower_buf(). More...
static void | osmo_talloc_replace_string (void *ctx, char **dst, const char *newstr)
| duplicate a string using talloc and release its prior content (if any) More...
void | osmo_talloc_replace_string_fmt (void *ctx, char **dst, const char *fmt,...)
| Replace a string using talloc and release its prior content (if any). More...
int | osmo_constant_time_cmp (const uint8_t *exp, const uint8_t *rel, const int count)
| Wishful thinking to generate a constant time compare. More...
uint64_t | osmo_decode_big_endian (const uint8_t *data, size_t data_len)
| Generic retrieval of 1..8 bytes as big-endian uint64_t. More...
uint8_t * | osmo_encode_big_endian (uint64_t value, size_t data_len)
| Generic big-endian encoding of big endian number up to 64bit. More...
size_t | osmo_strlcpy (char *dst, const char *src, size_t siz)
| Copy a C-string into a sized buffer. More...
const char * | osmo_strnchr (const char *str, size_t str_size, char c)
| Find first occurence of a char in a size limited string. More...
bool | osmo_is_hexstr (const char *str, int min_digits, int max_digits, bool require_even)
| Validate that a given string is a hex string within given size limits. More...
bool | osmo_identifier_valid (const char *str)
| Determine if a given identifier is valid, i.e. More...
bool | osmo_separated_identifiers_valid (const char *str, const char *sep_chars)
| Determine if a given identifier is valid, i.e. More...
void | osmo_identifier_sanitize_buf (char *str, const char *sep_chars, char replace_with)
| Replace characters in the given string buffer so that it is guaranteed to pass osmo_separated_identifiers_valid(). More...
size_t | osmo_escape_cstr_buf (char *buf, size_t bufsize, const char *str, int in_len)
| Return the string with all non-printable characters escaped. More...
char * | osmo_escape_cstr_c (void *ctx, const char *str, int in_len)
| Return the string with all non-printable characters escaped, in dynamically-allocated buffer. More...
size_t | osmo_quote_cstr_buf (char *buf, size_t bufsize, const char *str, int in_len)
| Like osmo_escape_str_buf2(), but returns double-quotes around a string, or "NULL" for a NULL string. More...
char * | osmo_quote_cstr_c (void *ctx, const char *str, int in_len)
| Return the string quoted and with all non-printable characters escaped, in dynamically-allocated buffer. More...
const char * | osmo_escape_str (const char *str, int in_len)
| Return the string with all non-printable characters escaped. More...
int | osmo_escape_str_buf3 (char *buf, size_t bufsize, const char *str, int in_len)
| Return the string with all non-printable characters escaped. More...
char * | osmo_escape_str_buf2 (char *buf, size_t bufsize, const char *str, int in_len)
| Return the string with all non-printable characters escaped. More...
const char * | osmo_escape_str_buf (const char *str, int in_len, char *buf, size_t bufsize)
| Like osmo_escape_str_buf2, but with unusual ordering of arguments, and may sometimes return string constants instead of writing to buf for error cases or empty input. More...
char * | osmo_escape_str_c (const void *ctx, const char *str, int in_len)
| Return the string with all non-printable characters escaped, in dynamically-allocated buffer. More...
const char * | osmo_quote_str (const char *str, int in_len)
| Like osmo_quote_str_buf() but returns the result in a static buffer. More...
int | osmo_quote_str_buf3 (char *buf, size_t bufsize, const char *str, int in_len)
| Like osmo_escape_str_buf3(), but returns double-quotes around a string, or "NULL" for a NULL string. More...
char * | osmo_quote_str_buf2 (char *buf, size_t bufsize, const char *str, int in_len)
| Like osmo_escape_str_buf2(), but returns double-quotes around a string, or "NULL" for a NULL string. More...
const char * | osmo_quote_str_buf (const char *str, int in_len, char *buf, size_t bufsize)
| Like osmo_quote_str_buf2, but with unusual ordering of arguments, and may sometimes return string constants instead of writing to buf for error cases or empty input. More...
char * | osmo_quote_str_c (const void *ctx, const char *str, int in_len)
| Like osmo_quote_str_buf() but returns the result in a dynamically-allocated buffer. More...
int | osmo_print_n (char *buf, size_t bufsize, const char *str, size_t n)
| Copy N characters to a buffer with a function signature useful for OSMO_STRBUF_APPEND(). More...
uint32_t | osmo_isqrt32 (uint32_t x)
| perform an integer square root operation on unsigned 32bit integer. More...
char | osmo_luhn (const char *in, int in_len)
| Calculate the Luhn checksum (as used for IMEIs). More...
static size_t | _osmo_strbuf_remain (const struct osmo_strbuf *sb)
| Get remaining space for characters and terminating nul in the given struct osmo_strbuf. More...
static size_t | _osmo_strbuf_char_count (const struct osmo_strbuf *sb)
| Get number of actual characters (without terminating nul) in the given struct osmo_strbuf. More...
void | osmo_strbuf_drop_tail (struct osmo_strbuf *sb, size_t n_chars)
| Remove up to N chars from the end of an osmo_strbuf. More...
void | osmo_strbuf_added_tail (struct osmo_strbuf *sb, size_t n_chars)
| Let osmo_strbuf know that n_chars characters (excluding nul) were written to the end of the buffer. More...
bool | osmo_str_startswith (const char *str, const char *startswith_str)
| Compare start of a string. More...
int | osmo_float_str_to_int (int64_t *val, const char *str, unsigned int precision)
| Convert a string of a floating point number to a signed int, with a decimal factor (fixed-point precision). More...
int | osmo_int_to_float_str_buf (char *buf, size_t buflen, int64_t val, unsigned int precision)
| Convert an integer to a floating point string using a decimal quotient (fixed-point precision). More...
char * | osmo_int_to_float_str_c (void *ctx, int64_t val, unsigned int precision)
| Convert an integer with a factor of a million to a floating point string. More...
int | osmo_str_to_int64 (int64_t *result, const char *str, int base, int64_t min_val, int64_t max_val)
| Convert a string of a number to int64_t, including all common strtoll() validity checks. More...
int | osmo_str_to_int (int *result, const char *str, int base, int min_val, int max_val)
| Convert a string of a number to int, including all common strtoll() validity checks. More...

## Variables
void to properly check target memory | bounds