Loading...
Searching...
No Matches
General-purpose utility functions

## Files

file | defs.h
| General definitions that are meant to be included from header files.
file | panic.h
file | utils.h
file | macaddr.c
| MAC address utility routines.
file | panic.c
| Routines for panic handling.
file | plugin.c
| Routines for loading and managing shared library plug-ins.
file | strrb.c
| Ringbuffer implementation, tailored for logging.
file | utils.c

## 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 | OSMO_GNUC_PREREQ(maj, min)
| Check for gcc and version.
#define | OSMO_DEPRECATED(text)
| Set the deprecated attribute with a message.
#define | OSMO_DEPRECATED_OUTSIDE_LIBOSMOCORE OSMO_DEPRECATED("For internal use inside libosmocore only.")
#define | OSMO_DEPRECATED_OUTSIDE(text)
#define | ARRAY_SIZE(x)
| Determine number of elements in an array of static size.
#define | OSMO_MAX(a, b)
| Return the maximum of two specified values.
#define | OSMO_MIN(a, b)
| Return the minimum of two specified values.
#define | OSMO_CMP(a, b)
| Return a typical cmp result for comparable entities a and b.
#define | OSMO_STRINGIFY(x)
| Stringify the name of a macro x, e.g.
#define | OSMO_STRINGIFY_VAL(x)
| Stringify the value of a macro x, e.g.
#define | OSMO_VALUE_STRING(x)
| Make a value_string entry from an enum value name.
#define | OSMO_BYTES_FOR_BITS(BITS)
| Number of bytes necessary to store given BITS.
#define | OSMO_STRLCPY_ARRAY(array, src)
| Copy a C-string into a sized buffer using sizeof to detect buffer's size.
#define | OSMO_LIKELY(exp)
| Branch prediction optimizations.
#define | OSMO_UNLIKELY(exp)
#define | osmo_static_assert(exp, name)
#define | OSMO_SNPRINTF_RET(ret, rem, offset, len)
#define | OSMO_ASSERT(exp)
| Helper macro to terminate when an assertion fails.
#define | osmo_talloc_asprintf(ctx, dest, fmt, args ...)
| Append to a string and re-/allocate if necessary.
#define | OSMO_MOD_FLR(x, y)
| Floored Modulo (See also: Daan Leijen, Division and Modulus for Computer Scientists).
#define | OSMO_MOD_EUC(x, y)
| Euclidean Modulo (See also: Daan Leijen, Division and Modulus for Computer Scientists).
#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.
#define | OSMO_STRBUF_PRINTF(STRBUF, fmt, args...)
| Shortcut for OSMO_STRBUF_APPEND() invocation using snprintf().
#define | OSMO_STRBUF_REMAIN(STRBUF)
| Return remaining space for characters and terminating nul in the given struct osmo_strbuf.
#define | OSMO_STRBUF_CHAR_COUNT(STRBUF)
| Return number of actual characters contained in struct osmo_strbuf (without terminating nul).
#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.
#define | OSMO_STRBUF_DROP_TAIL(STRBUF, N_CHARS)
#define | 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.

## Typedefs

typedef void(* | osmo_panic_handler_t) (const char *fmt, va_list args)
| panic handler callback function type

## Functions

void | osmo_panic (const char *fmt,...)
| Terminate the current program with a panic.
void | osmo_set_panic_handler (osmo_panic_handler_t h)
| Set the panic handler.
const char * | get_value_string (const struct value_string *vs, uint32_t val)
| get human-readable string for given value
const char * | get_value_string_or_null (const struct value_string *vs, uint32_t val)
| get human-readable string or NULL for given value
int | get_string_value (const struct value_string *vs, const char *str)
| get numeric value for given human-readable string
char | osmo_bcd2char (uint8_t bcd)
| Convert BCD-encoded digit into printable character.
uint8_t | osmo_char2bcd (char c)
| Convert number in ASCII to BCD value.
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.
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.
int | osmo_hexparse (const char *str, uint8_t *b, unsigned int max_len)
| Parse a string containing hexadecimal digits.
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.
char * | osmo_ubit_dump (const uint8_t *bits, unsigned int len)
| Convert a sequence of unpacked bits to ASCII string, in static buffer.
char * | osmo_hexdump (const unsigned char *buf, int len)
| Convert binary sequence to hexadecimal ASCII string.
char * | osmo_hexdump_c (const void *ctx, const unsigned char *buf, int len)
| Convert binary sequence to hexadecimal ASCII string.
char * | osmo_hexdump_nospc (const unsigned char *buf, int len)
| Convert binary sequence to hexadecimal ASCII string.
char * | osmo_hexdump_nospc_c (const void *ctx, const unsigned char *buf, int len)
| Convert binary sequence to hexadecimal ASCII string.
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.
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.
const char * | osmo_str_tolower (const char *src)
| Convert a string to lowercase, using a static buffer.
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().
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.
const char * | osmo_str_toupper (const char *src)
| Convert a string to uppercase, using a static buffer.
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().
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)
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).
int | osmo_constant_time_cmp (const uint8_t *exp, const uint8_t *rel, const int count)
| Wishful thinking to generate a constant time compare.
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.
uint8_t * | osmo_encode_big_endian (uint64_t value, size_t data_len)
| Generic big-endian encoding of big endian number up to 64bit.
size_t | osmo_strlcpy (char *dst, const char *src, size_t siz)
| Copy a C-string into a sized buffer.
const char * | osmo_strnchr (const char *str, size_t str_size, char c)
| Find first occurence of a char in a size limited string.
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.
bool | osmo_identifier_valid (const char *str)
| Determine if a given identifier is valid, i.e.
bool | osmo_separated_identifiers_valid (const char *str, const char *sep_chars)
| Determine if a given identifier is valid, i.e.
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().
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.
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.
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.
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.
const char * | osmo_escape_str (const char *str, int in_len)
| Return the string with all non-printable characters escaped.
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.
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.
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.
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.
const char * | osmo_quote_str (const char *str, int in_len)
| Like osmo_quote_str_buf() but returns the result in a static buffer.
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.
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.
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.
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.
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().
uint32_t | osmo_isqrt32 (uint32_t x)
| perform an integer square root operation on unsigned 32bit integer.
char | osmo_luhn (const char *in, int in_len)
| Calculate the Luhn checksum (as used for IMEIs).
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.
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.
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.
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.
bool | osmo_str_startswith (const char *str, const char *startswith_str)
| Compare start of a string.
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).
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).
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.
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.
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.
int | osmo_macaddr_parse (uint8_t *out, const char *in)
| Parse a MAC address from human-readable notation This function parses an ethernet MAC address in the commonly-used hex/colon notation (00:00:00:00:00:00) and generates the binary representation from it.
int | osmo_get_macaddr (uint8_t *mac_out, const char *dev_name)
| Obtain the MAC address of a given network device.
static void | osmo_panic_default (const char *fmt, va_list args)
int | osmo_plugin_load_all (const char *directory)
struct osmo_strrb * | osmo_strrb_create (void *talloc_ctx, size_t rb_size)
| Create an empty, initialized osmo_strrb.
bool | osmo_strrb_is_empty (const struct osmo_strrb *rb)
| Check if an osmo_strrb is empty.
const char * | osmo_strrb_get_nth (const struct osmo_strrb *rb, unsigned int string_index)
| Return a pointer to the Nth string in the osmo_strrb.
bool | _osmo_strrb_is_bufindex_valid (const struct osmo_strrb *rb, unsigned int bufi)
size_t | osmo_strrb_elements (const struct osmo_strrb *rb)
| Count the number of log messages in an osmo_strrb.
int | osmo_strrb_add (struct osmo_strrb *rb, const char *data)
| Add a string to the osmo_strrb.
char | alias ("osmo_hexdump_nospc")))
static int | _osmo_escape_str_buf (char *buf, size_t bufsize, const char *str, int in_len, bool legacy_format)
| Return the string with all non-printable characters escaped.
static size_t | _osmo_quote_str_buf (char *buf, size_t bufsize, const char *str, int in_len, bool legacy_format)
| Return a quoted and escaped representation of the string.

## Variables

void to properly check target memory | bounds
static osmo_panic_handler_t | osmo_panic_handler = (void*)0
static __thread char | namebuf [255]
static __thread char | capsbuf [128]
static __thread char | hexd_buff [4096]
static const char | hex_chars [] = "0123456789abcdef"
static const char | osmo_identifier_illegal_chars [] = "., {}[]()<>|~\\^`'\"?=;/+*&%$#!"

## Detailed Description

various utility routines

## Macro Definition Documentation

## ARRAY_SIZE

#define ARRAY_SIZE | ( |  | x | ) |

Value:
(sizeof(x) / sizeof((x)[0]))
x
uint32_t x
Definition jhash.h:0

Determine number of elements in an array of static size.

Referenced by dispatch_rx_msg(), log_init(), msgb_hexdump_buf(), osmo_encode_big_endian(), osmo_environment_filter(), osmo_sercomm_change_speed(), osmo_sercomm_drv_pull(), osmo_sercomm_init(), osmo_sercomm_register_rx_cb(), and osmo_system_nowait2().

## OSMO_ASSERT

#define OSMO_ASSERT | ( |  | exp | ) |

Value:
do { \
if (OSMO_UNLIKELY(!(exp))) { \
osmo_panic("Assert failed %s %s:%d\n", #exp, __FILE__, __LINE__); \
} \
} while (0); /* some code invokes OSMO_ASSERT() without the semicolon */
OSMO_UNLIKELY
#define OSMO_UNLIKELY(exp)
Definition utils.h:47

Helper macro to terminate when an assertion fails.

Parameters

  [in] | exp | Predicate to verify This function will generate a backtrace and terminate the program if the predicate evaluates to false (0).

Referenced by __attribute__(), __attribute__(), _file_output_stream(), _file_raw_output(), _log_target_file_setup_iofd(), _log_target_file_setup_talloc_pool(), _osmo_fsm_inst_dispatch(), _osmo_tdef_fsm_inst_state_chg(), assert_loginfo(), iofd_handle_recv(), iofd_handle_segmented_read(), iofd_msgb_alloc2(), iofd_poll_ofd_cb_recvmsg_sendmsg(), iofd_prepare_handle_segmentation(), iofd_send_completion_cb(), iofd_txqueue_dequeue(), log_init(), log_parse_category_mask(), log_target_create_file(), log_target_file_destroy(), log_target_file_reopen(), mangle_identifier_ifneeded(), map_subsys(), msgb_alloc_headroom(), msgb_alloc_headroom_c(), msgb_l1len(), msgb_l2len(), msgb_l3len(), msgb_l4len(), msgb_printf(), netdev_netns_ctx_alloc(), netdev_netns_ctx_get(), netdev_netns_ctx_put(), osmo_conv_encode_init(), osmo_encode_big_endian(), osmo_environment_append(), osmo_environment_filter(), osmo_float_str_to_int(), osmo_fsm_defer_free(), osmo_iofd_init(), osmo_iofd_notify_connected(), osmo_iofd_sendmsg_msgb(), osmo_iofd_sendto_msgb(), osmo_iofd_set_alloc_info(), osmo_iofd_set_ioops(), osmo_iofd_write_msgb(), osmo_load16be_ext(), osmo_load16be_ext_2(), osmo_load16le_ext(), osmo_load32be_ext(), osmo_load32be_ext_2(), osmo_load32le_ext(), osmo_load64be_ext(), osmo_load64be_ext_2(), osmo_load64le_ext(), osmo_soft_uart_alloc(), osmo_soft_uart_tx_ubits(), osmo_stat_item_group_alloc(), osmo_stats_timer_cb(), osmo_store16be_ext(), osmo_store16le_ext(), osmo_store32be_ext(), osmo_store32le_ext(), osmo_store64be_ext(), osmo_store64le_ext(), osmo_tdef_get(), osmo_tdef_get_state_timeout(), osmo_tundev_send(), rate_ctr_group_desc_mangle(), rate_ctr_timer_cb(), suart_rx_bit(), suart_rx_ch(), and suart_tx_bit().

## OSMO_BYTES_FOR_BITS

#define OSMO_BYTES_FOR_BITS | ( |  | BITS | ) |

Value:
(((BITS) + 7) / 8)

Number of bytes necessary to store given BITS.

## OSMO_CMP

#define OSMO_CMP | ( |  | a,
|  |  | b )

Value:
((a) < (b)? -1 : ((a) > (b)? 1 : 0))

Return a typical cmp result for comparable entities a and b.

## OSMO_DEPRECATED

#define OSMO_DEPRECATED | ( |  | text | ) |

Set the deprecated attribute with a message.

## OSMO_DEPRECATED_OUTSIDE

#define OSMO_DEPRECATED_OUTSIDE | ( |  | text | ) |

Value:
OSMO_DEPRECATED(text)
OSMO_DEPRECATED
#define OSMO_DEPRECATED(text)
Set the deprecated attribute with a message.
Definition defs.h:41

## OSMO_DEPRECATED_OUTSIDE_LIBOSMOCORE

#define OSMO_DEPRECATED_OUTSIDE_LIBOSMOCORE OSMO_DEPRECATED("For internal use inside libosmocore only.")

## OSMO_GNUC_PREREQ

#define OSMO_GNUC_PREREQ | ( |  | maj,
|  |  | min )

Value:
0

Check for gcc and version.

Note
  Albeit glibc provides a features.h file that contains a similar definition (__GNUC_PREREQ), this definition has been copied from there to have it available with other libraries, too.

Returns
  != 0 iff gcc is used and it's version is at least maj.min.

## OSMO_LIKELY

#define OSMO_LIKELY | ( |  | exp | ) |

Value:
exp

Branch prediction optimizations.

Referenced by iofd_handle_send_completion(), iofd_prepare_handle_segmentation(), msgb_alloc_headroom(), and msgb_alloc_headroom_c().

## OSMO_MAX

#define OSMO_MAX | ( |  | a,
|  |  | b )

Value:
((a) >= (b) ? (a) : (b))

Return the maximum of two specified values.

Referenced by _log_target_file_setup_iofd(), log_cache_update(), log_cache_update_all(), osmo_bcd2str(), and osmo_stat_item_set().

## OSMO_MIN

#define OSMO_MIN | ( |  | a,
|  |  | b )

Value:
((a) >= (b) ? (b) : (a))

Return the minimum of two specified values.

Referenced by _log_target_file_setup_iofd(), _osmo_strbuf_char_count(), iofd_handle_send_completion(), iofd_prepare_handle_segmentation(), log_cache_update(), log_cache_update_all(), osmo_stat_item_set(), osmo_strbuf_added_tail(), osmo_strbuf_drop_tail(), and osmo_strlcpy().

## OSMO_MOD_EUC

#define OSMO_MOD_EUC | ( |  | x,
|  |  | y )

Value:
((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).

Parameters

  [in] | x | dividend.
  [in] | y | divisor.

Returns
  remainder of x divided by y.

## OSMO_MOD_FLR

#define OSMO_MOD_FLR | ( |  | x,
|  |  | y )

Value:
(((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).

Parameters

  [in] | x | dividend.
  [in] | y | divisor.

Returns
  remainder of x divided by y.

## OSMO_NAME_C_IMPL

#define OSMO_NAME_C_IMPL | ( |  | CTX,
|  |  | INITIAL_BUFSIZE,
|  |  | ON_ERROR,
|  |  | FUNC_BUF,
|  |  | FUNC_BUF_ARGS... )

Translate a buffer function to a talloc context function.

This is the full function body of a char *foo_name_c(void *ctx, val...) function, implemented by an int foo_name_buf(buf, buflen, val...) function:

char *foo_name_c(void *ctx, example_t arg) { OSMO_NAME_C_IMPL(ctx, 64, "ERROR", foo_name_buf, arg) }

Return a talloc'd string containing the result of the given foo_name_buf() function, or ON_ERROR on error in the called foo_name_buf() function.

If ON_ERROR is NULL, the function returns NULL on error rc from FUNC_BUF. Take care: returning NULL in printf() like formats (LOGP()) makes the program crash. If ON_ERROR is non-NULL, it must be a string constant, which is not returned directly, but written to an allocated string buffer first.

Parameters

  [in] | INITIAL_BUFSIZE | Which size to first talloc from ctx – a larger size makes a reallocation less likely, a smaller size allocates less unused bytes, zero allocates once but still runs the string composition twice.
  [in] | ON_ERROR | String constant to copy on error rc returned by FUNC_BUF, or NULL to return NULL.
  [in] | FUNC_BUF | Name of a function with signature int foo_buf(char *buf, size_t buflen, ...). The function must return the strlen() that it would write to a sufficiently large buffer or negative on error, like snprintf().
  [in] | FUNC_BUF_ARGS | Additional arguments to pass to FUNC_BUF after the buf and buflen.

Referenced by osmo_escape_cstr_c(), osmo_escape_str_c(), osmo_int_to_float_str_c(), osmo_quote_cstr_c(), osmo_quote_str_c(), and osmo_use_count_to_str_c().

## OSMO_SNPRINTF_RET

#define OSMO_SNPRINTF_RET | ( |  | ret,
|  |  | rem,
|  |  | offset,
|  |  | len )

Value:
do { \
len += ret; \
if (ret > rem) \
ret = rem; \
offset += ret; \
rem -= ret; \
} while (0)
len
uint8_t len

Referenced by osmo_tdef_range_str_buf().

## osmo_static_assert

#define osmo_static_assert | ( |  | exp,
|  |  | name )

Value:
typedef int dummy##name [(exp) ? 1 : -1] __attribute__((__unused__));
__attribute__
enum gsm0808_assignment_requirement __attribute__
Definition log2.h:61
name
char name[32]
source file name
Definition gsmtap.h:11

## OSMO_STRBUF_ADDED_TAIL

#define OSMO_STRBUF_ADDED_TAIL | ( |  | STRBUF,
|  |  | N_CHARS )

Value:
osmo_strbuf_added_tail(&(STRBUF), N_CHARS)
osmo_strbuf_added_tail
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.
Definition utils.c:1248

Referenced by log_output_buf().

## OSMO_STRBUF_APPEND

#define OSMO_STRBUF_APPEND | ( |  | STRBUF,
|  |  | func,
|  |  | args... )

Value:
do { \
if (!(STRBUF).pos) \
(STRBUF).pos = (STRBUF).buf; \
size_t _sb_remain = OSMO_STRBUF_REMAIN(STRBUF); \
int _sb_l = func((STRBUF).pos, _sb_remain, ##args); \
if (_sb_l < 0 || (size_t)_sb_l > _sb_remain) \
(STRBUF).pos = (STRBUF).buf + (STRBUF).len; \
else if ((STRBUF).pos) \
(STRBUF).pos += _sb_l; \
if (_sb_l > 0) \
(STRBUF).chars_needed += _sb_l; \
} while(0)
OSMO_STRBUF_REMAIN
#define OSMO_STRBUF_REMAIN(STRBUF)
Return remaining space for characters and terminating nul in the given struct osmo_strbuf.
Definition utils.h:297

Append a string to a buffer, as printed by an snprintf()-like function and with similar bounds checking.

Make sure to never write past the end of the buffer, and collect the total size that would be needed.

    // an example function implementation to append: write N spaces.
    int print_spaces(char *dst, size_t dst_len, int n)
    {
            int i;
            if (n < 0)
                    return -EINVAL;
            for (i = 0; i < n && i < dst_len; i++)
                    dst[i] = ' ';
            if (dst_len)
                    dst[OSMO_MIN(dst_len - 1, n)] = '\0';
            // return the n that we would have liked to write if space were available:
            return n;
    }

    // append above spaces as well as an snprintf()
    void strbuf_example()
    {
            char buf[23];
            struct osmo_strbuf sb = { .buf = buf, .len = sizeof(buf) };

            OSMO_STRBUF_APPEND(sb, print_spaces, 5);
            OSMO_STRBUF_APPEND(sb, snprintf, "The answer is %d but what is the question?", 42);
            OSMO_STRBUF_APPEND(sb, print_spaces, 423423);

            printf("%s\n", buf);
            printf("would have needed %zu bytes\n", sb.chars_needed);
    }

Parameters

  [in,out] | STRBUF | A struct osmo_strbuf instance.
  [in] | func | A function with a signature of int func(char *dst, size_t dst_len [, args]) with semantics like snprintf().
  [in] | args | Arguments passed to func, if any.

Referenced by _osmo_escape_str_buf(), _osmo_quote_str_buf(), and log_output_buf().

## OSMO_STRBUF_APPEND_NOLEN

#define OSMO_STRBUF_APPEND_NOLEN | ( |  | STRBUF,
|  |  | func,
|  |  | args... )

Value:
do { \
if (!(STRBUF).pos) \
(STRBUF).pos = (STRBUF).buf; \
size_t _sb_remain = OSMO_STRBUF_REMAIN(STRBUF); \
if (_sb_remain) { \
func((STRBUF).pos, _sb_remain, ##args); \
} \
size_t _sb_l = (STRBUF).pos ? strnlen((STRBUF).pos, _sb_remain) : 0; \
if (_sb_l > _sb_remain) \
(STRBUF).pos = (STRBUF).buf + (STRBUF).len; \
else if ((STRBUF).pos) \
(STRBUF).pos += _sb_l; \
(STRBUF).chars_needed += _sb_l; \
} while(0)

Like OSMO_STRBUF_APPEND(), but for function signatures that return the char* buffer instead of a length.

When using this function, the final STRBUF.chars_needed may not reflect the actual number of characters needed, since that number cannot be obtained from this kind of function signature.

Parameters

  [in,out] | STRBUF | A struct osmo_strbuf instance.
  [in] | func | A function with a signature of char *func(char *dst, size_t dst_len [, args]) where the returned string is always written to dst.
  [in] | args | Arguments passed to func, if any.

## OSMO_STRBUF_CHAR_COUNT

#define OSMO_STRBUF_CHAR_COUNT | ( |  | STRBUF | ) |

Value:
_osmo_strbuf_char_count(&(STRBUF))
_osmo_strbuf_char_count
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.
Definition utils.h:303

Return number of actual characters contained in struct osmo_strbuf (without terminating nul).

Referenced by log_output_buf(), and osmo_strbuf_drop_tail().

## OSMO_STRBUF_DROP_TAIL

#define OSMO_STRBUF_DROP_TAIL | ( |  | STRBUF,
|  |  | N_CHARS )

Value:
osmo_strbuf_drop_tail(&(STRBUF), N_CHARS)
osmo_strbuf_drop_tail
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.
Definition utils.c:1228

Referenced by log_output_buf().

## OSMO_STRBUF_PRINTF

#define OSMO_STRBUF_PRINTF | ( |  | STRBUF,
|  |  | fmt,
|  |  | args... )

Value:
OSMO_STRBUF_APPEND(STRBUF, snprintf, fmt, ##args)
OSMO_STRBUF_APPEND
#define OSMO_STRBUF_APPEND(STRBUF, func, args...)
Append a string to a buffer, as printed by an snprintf()-like function and with similar bounds checki...
Definition utils.h:249

Shortcut for OSMO_STRBUF_APPEND() invocation using snprintf().

int strbuf_example2(char *buf, size_t buflen) { int i; struct osmo_strbuf sb = { .buf = buf, .len = buflen };

OSMO_STRBUF_PRINTF(sb, "T minus"); for (i = 10; i; i–) OSMO_STRBUF_PRINTF(sb, " %d", i); OSMO_STRBUF_PRINTF(sb, " ... Lift off!");

return sb.chars_needed; }

Parameters

  [in,out] | STRBUF | A struct osmo_strbuf instance.
  [in] | fmt | Format string passed to snprintf.
  [in] | args | Additional arguments passed to snprintf, if any.

Referenced by _osmo_escape_str_buf(), _osmo_quote_str_buf(), log_output_buf(), osmo_int_to_float_str_buf(), and osmo_use_count_to_str_buf().

## OSMO_STRBUF_REMAIN

#define OSMO_STRBUF_REMAIN | ( |  | STRBUF | ) |

Value:
_osmo_strbuf_remain(&(STRBUF))
_osmo_strbuf_remain
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.
Definition utils.h:287

Return remaining space for characters and terminating nul in the given struct osmo_strbuf.

Referenced by log_output_buf(), and osmo_strbuf_added_tail().

## OSMO_STRINGIFY

#define OSMO_STRINGIFY | ( |  | x | ) |

Value:
#x

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.

## OSMO_STRINGIFY_VAL

#define OSMO_STRINGIFY_VAL | ( |  | x | ) |

Value:
OSMO_STRINGIFY(x)
OSMO_STRINGIFY
#define OSMO_STRINGIFY(x)
Stringify the name of a macro x, e.g.
Definition utils.h:30

Stringify the value of a macro x, e.g.

a port number.

## OSMO_STRLCPY_ARRAY

#define OSMO_STRLCPY_ARRAY | ( |  | array,
|  |  | src )

Value:
osmo_strlcpy(array, src, sizeof(array))
osmo_strlcpy
size_t osmo_strlcpy(char *dst, const char *src, size_t siz)
Copy a C-string into a sized buffer.
Definition utils.c:575

Copy a C-string into a sized buffer using sizeof to detect buffer's size.

Referenced by _gsmtap_raw_output().

## osmo_talloc_asprintf

#define osmo_talloc_asprintf | ( |  | ctx,
|  |  | dest,
|  |  | fmt,
|  |  | args ... )

Value:
do { \
if (!dest) \
dest = talloc_asprintf(ctx, fmt, ## args); \
else \
dest = talloc_asprintf_append((char*)dest, fmt, ## args); \
} while (0)

Append to a string and re-/allocate if necessary.

Parameters

  [in] | ctx | Talloc context to use for initial allocation.
  [in,out] | dest | char* to re-/allocate and append to.
  [in] | fmt | printf-like string format.
  [in] | args | Arguments for fmt.

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 ctx or not.

## OSMO_UNLIKELY

#define OSMO_UNLIKELY | ( |  | exp | ) |

Value:
exp

Referenced by _osmo_strbuf_char_count(), _osmo_strbuf_remain(), iofd_handle_segmented_read(), iofd_handle_send_completion(), iofd_txqueue_dequeue(), log_check_level(), msgb_get(), msgb_pull(), msgb_push(), msgb_put(), msgb_trim(), osmo_iofd_sendmsg_msgb(), osmo_iofd_sendto_msgb(), osmo_iofd_write_msgb(), osmo_soft_uart_tx_ubits(), and osmo_vlogp().

## OSMO_VALUE_STRING

#define OSMO_VALUE_STRING | ( |  | x | ) |

Value:
{ x, #x }

Make a value_string entry from an enum value name.

## Typedef Documentation

## osmo_panic_handler_t

typedef void(* osmo_panic_handler_t) (const char *fmt, va_list args)

panic handler callback function type

## Function Documentation

## _osmo_escape_str_buf()

int _osmo_escape_str_buf | ( | char * | buf,
|  | size_t | bufsize,
|  | const char * | str,
|  | int | in_len,
|  | bool | legacy_format )

static

Return the string with all non-printable characters escaped.

This internal function is the implementation for all osmo_escape_str* and osmo_quote_str* API versions. It provides both the legacy (non C compatible) escaping, as well as C compatible string constant syntax, and it provides a return value of characters-needed, to allow producing un-truncated strings in all cases.

Parameters

  [out] | buf | string buffer to write escaped characters to.
  [in] | bufsize | sizeof(buf).
  [in] | str | A string that may contain any characters.
  [in] | in_len | Pass -1 to print until nul char, or >= 0 to force a length (also past nul chars).
  [in] | legacy_format | If false, return C compatible string constants ("\x0f"), if true the legacy escaping format ("\15"). The legacy format also escapes as "\a\b\f\v", while the non-legacy format also escapes those as "\xNN" sequences.

Returns
  Number of characters that would be written if bufsize were large enough excluding '\0' (like snprintf()).

References BACKSLASH_CASE, osmo_strbuf::buf, osmo_strbuf::chars_needed, osmo_print_n(), OSMO_STRBUF_APPEND, and OSMO_STRBUF_PRINTF.

Referenced by _osmo_quote_str_buf(), osmo_escape_cstr_buf(), osmo_escape_cstr_c(), osmo_escape_str_buf2(), osmo_escape_str_buf3(), and osmo_escape_str_c().

## _osmo_quote_str_buf()

size_t _osmo_quote_str_buf | ( | char * | buf,
|  | size_t | bufsize,
|  | const char * | str,
|  | int | in_len,
|  | bool | legacy_format )

static

Return a quoted and escaped representation of the string.

This internal function is the implementation for all osmo_quote_str* API versions. It provides both the legacy (non C compatible) escaping, as well as C compatible string constant syntax, and it provides a return value of characters-needed, to allow producing un-truncated strings in all cases.

Parameters

  [out] | buf | string buffer to write escaped characters to.
  [in] | bufsize | sizeof(buf).
  [in] | str | A string that may contain any characters.
  [in] | in_len | Pass -1 to print until nul char, or >= 0 to force a length (also past nul chars).
  [in] | legacy_format | If false, return C compatible string constants ("\x0f"), if true the legacy escaping format ("\15"). The legacy format also escapes as "\a\b\f\v", while the non-legacy format also escapes those as "\xNN" sequences.

Returns
  Number of characters that would be written if bufsize were large enough excluding '\0' (like snprintf()).

References _osmo_escape_str_buf(), osmo_strbuf::buf, osmo_strbuf::chars_needed, OSMO_STRBUF_APPEND, and OSMO_STRBUF_PRINTF.

Referenced by osmo_quote_cstr_buf(), osmo_quote_cstr_c(), osmo_quote_str(), osmo_quote_str_buf(), osmo_quote_str_buf2(), osmo_quote_str_buf3(), and osmo_quote_str_c().

## _osmo_strbuf_char_count()

size_t _osmo_strbuf_char_count | ( | const struct osmo_strbuf * | sb | ) |

inline static

Get number of actual characters (without terminating nul) in the given struct osmo_strbuf.

Parameters

  [in] | sb | the string buffer to get the number of characters for.

Returns
  number of actual characters (without terminating nul).

References osmo_strbuf::buf, osmo_strbuf::len, OSMO_MIN, OSMO_UNLIKELY, and osmo_strbuf::pos.

## _osmo_strbuf_remain()

size_t _osmo_strbuf_remain | ( | const struct osmo_strbuf * | sb | ) |

inline static

Get remaining space for characters and terminating nul in the given struct osmo_strbuf.

Parameters

  [in] | sb | the string buffer to get the remaining space for.

Returns
  remaining space in the given struct osmo_strbuf.

References osmo_strbuf::buf, osmo_strbuf::len, OSMO_UNLIKELY, and osmo_strbuf::pos.

## _osmo_strrb_is_bufindex_valid()

bool _osmo_strrb_is_bufindex_valid | ( | const struct osmo_strrb * | rb,
|  | unsigned int | bufi )

References osmo_strrb::end, osmo_strrb_is_empty(), osmo_strrb::size, and osmo_strrb::start.

Referenced by osmo_strrb_get_nth().

## alias()

char alias | ( | "osmo_hexdump_nospc" |  | ) |

## get_string_value()

int get_string_value | ( | const struct value_string * | vs,
|  | const char * | str )

get numeric value for given human-readable string

Parameters

  [in] | vs | Array of value_string tuples
  [in] | str | human-readable string

Returns
  numeric value (>0) or negative numer in case of error

References value_string::value.

Referenced by log_parse_level().

## get_value_string()

const char * get_value_string | ( | const struct value_string * | vs,
|  | uint32_t | val )

get human-readable string for given value

Parameters

  [in] | vs | Array of value_string tuples
  [in] | val | Value to be converted

Returns
  pointer to human-readable string

If val is found in vs, the array's string entry is returned. Otherwise, an "unknown" string containing the actual value is composed in a static buffer that is reused across invocations.

References get_value_string_or_null(), and namebuf.

Referenced by level_color(), log_level_str(), osmo_fsm_event_name(), osmo_fsm_term_cause_name(), osmo_io_backend_name(), osmo_iofd_mode_name(), osmo_prim_operation_name(), and osmo_tdef_unit_name().

## get_value_string_or_null()

const char * get_value_string_or_null | ( | const struct value_string * | vs,
|  | uint32_t | val )

get human-readable string or NULL for given value

Parameters

  [in] | vs | Array of value_string tuples
  [in] | val | Value to be converted

Returns
  pointer to human-readable string or NULL if val is not found

References value_string::str.

Referenced by get_value_string(), and level_color().

## osmo_bcd2char()

char osmo_bcd2char | ( | uint8_t | bcd | ) |

Convert BCD-encoded digit into printable character.

Parameters

  [in] | bcd | A single BCD-encoded digit

Returns
  single printable character

Referenced by osmo_bcd2str().

## osmo_bcd2str()

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.

The given nibble offsets are interpreted in BCD order, i.e. nibble 0 is bcd[0] & 0xf, nibble 1 is bcd[0] >> 4, nibble 3 is bcd[1] & 0xf, etc..

Parameters

  [out] | dst | Output string buffer, is always nul terminated when dst_size > 0.
  [in] | dst_size | sizeof() the output string buffer.
  [in] | bcd | Binary coded data buffer.
  [in] | start_nibble | Offset to start from, in nibbles, typically 1 to skip the first nibble.
  [in] | end_nibble | Offset to stop before, in nibbles, e.g. sizeof(bcd)*2 - (bcd[0] & GSM_MI_ODD? 0:1).
  [in] | allow_hex | If false, return error if there are digits other than 0-9. If true, return those as [A-F].

Returns
  The strlen that would be written if the output buffer is large enough, excluding nul byte (like snprintf()), or -EINVAL if allow_hex is false and a digit > 9 is encountered. On -EINVAL, the conversion is still completed as if allow_hex were passed as true. Return -ENOMEM if dst is NULL or dst_size is zero. If end_nibble <= start_nibble, write an empty string to dst and return 0.

References osmo_bcd2char(), and OSMO_MAX.

## osmo_char2bcd()

uint8_t osmo_char2bcd | ( | char | c | ) |

Convert number in ASCII to BCD value.

Parameters

  [in] | c | ASCII character

Returns
  BCD encoded value of character

## osmo_constant_time_cmp()

int osmo_constant_time_cmp | ( | const uint8_t * | exp,
|  | const uint8_t * | rel,
|  | const int | count )

Wishful thinking to generate a constant time compare.

Parameters

  [in] | exp | Expected data
  [in] | rel | Comparison value
  [in] | count | Number of bytes to compare

Returns
  1 in case exp equals rel; zero otherwise

Compare count bytes of exp to rel. Return 0 if they are identical, 1 otherwise. Do not return a mismatch on the first mismatching byte, but always compare all bytes, regardless. The idea is that the amount of matching bytes cannot be inferred from the time the comparison took.

References x.

## osmo_decode_big_endian()

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.

Parameters

  [in] | data | Input data as byte-array
  [in] | data_len | Length of data in octets

Returns
  uint64_t of data interpreted as big-endian

This is like osmo_load64be_ext, except that if data_len is less than sizeof(uint64_t), the data is interpreted as the least significant bytes (osmo_load64be_ext loads them as the most significant bytes into the returned uint64_t). In this way, any integer size up to 64 bits can be decoded conveniently by using sizeof(), without the need to call specific numbered functions (osmo_load16, 32, ...).

## osmo_encode_big_endian()

uint8_t * osmo_encode_big_endian | ( | uint64_t | value,
|  | size_t | data_len )

Generic big-endian encoding of big endian number up to 64bit.

Parameters

  [in] | value | unsigned integer value to be stored
  [in] | data_len | number of octets

Returns
  static buffer containing big-endian stored value

This is like osmo_store64be_ext, except that this returns a static buffer of the result (for convenience, but not threadsafe). If data_len is less than sizeof(uint64_t), only the least significant bytes of value are encoded.

References ARRAY_SIZE, OSMO_ASSERT, and osmo_store64be_ext().

## osmo_escape_cstr_buf()

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.

In contrast to osmo_escape_str_buf2(), this returns the needed buffer size suitable for OSMO_STRBUF_APPEND(), and this escapes characters in a way compatible with C string constant syntax.

Parameters

  [out] | buf | string buffer to write escaped characters to.
  [in] | bufsize | sizeof(buf).
  [in] | str | A string that may contain any characters.
  [in] | in_len | Pass -1 to print until nul char, or >= 0 to force a length (also past nul chars).

Returns
  Number of characters that would be written if bufsize were large enough excluding '\0' (like snprintf()).

References _osmo_escape_str_buf(), and osmo_strbuf::buf.

## osmo_escape_cstr_c()

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.

In contrast to osmo_escape_str_c(), this escapes characters in a way compatible with C string constant syntax, and allocates sufficient memory in all cases.

Parameters

  [in] | str | A string that may contain any characters.
  [in] | len | Pass -1 to print until nul char, or >= 0 to force a length.

Returns
  dynamically-allocated buffer, containing an escaped representation.

References _osmo_escape_str_buf(), and OSMO_NAME_C_IMPL.

## osmo_escape_str()

const char * osmo_escape_str | ( | const char * | str,
|  | int | in_len )

Return the string with all non-printable characters escaped.

Call osmo_escape_str_buf() with a static buffer.

Parameters

  [in] | str | A string that may contain any characters.
  [in] | len | Pass -1 to print until nul char, or >= 0 to force a length.

Returns
  buf containing an escaped representation, possibly truncated, or str itself.

References namebuf, and osmo_escape_str_buf().

## osmo_escape_str_buf()

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.

Most *_buf() functions have the buffer and size as first arguments, here the arguments are last. In particular, this function signature doesn't work with OSMO_STRBUF_APPEND_NOLEN().

Parameters

  [in] | str | A string that may contain any characters.
  [in] | len | Pass -1 to print until nul char, or >= 0 to force a length.
  [in,out] | buf | string buffer to write escaped characters to.
  [in] | bufsize | size of buf.

Returns
  buf containing an escaped representation, possibly truncated, or "(null)" if str == NULL, or "(error)" in case of errors.

References osmo_escape_str_buf2().

Referenced by osmo_escape_str().

## osmo_escape_str_buf2()

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.

Parameters

  [out] | buf | string buffer to write escaped characters to.
  [in] | bufsize | sizeof(buf).
  [in] | str | A string that may contain any characters.
  [in] | in_len | Pass -1 to print until nul char, or >= 0 to force a length (also past nul chars).

Returns
  The output buffer (buf).

References _osmo_escape_str_buf(), and osmo_strbuf::buf.

Referenced by osmo_escape_str_buf().

## osmo_escape_str_buf3()

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.

Parameters

  [out] | buf | string buffer to write escaped characters to.
  [in] | bufsize | sizeof(buf).
  [in] | str | A string that may contain any characters.
  [in] | in_len | Pass -1 to print until nul char, or >= 0 to force a length (also past nul chars).

Returns
  Number of characters that would be written if bufsize were large enough excluding '\0' (like snprintf()).

References _osmo_escape_str_buf(), and osmo_strbuf::buf.

## osmo_escape_str_c()

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.

Parameters

  [in] | str | A string that may contain any characters.
  [in] | len | Pass -1 to print until nul char, or >= 0 to force a length.

Returns
  dynamically-allocated output buffer, containing an escaped representation

References _osmo_escape_str_buf(), and OSMO_NAME_C_IMPL.

## osmo_float_str_to_int()

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).

For example, with precision=3, convert "-1.23" to -1230. In other words, the float value is multiplied by 10 to-the-power-of precision to obtain the returned integer. The usable range of digits is -INT64_MAX .. INT64_MAX – note, not INT64_MIN! The value of INT64_MIN is excluded to reduce implementation complexity. See also utils_test.c. The advantage over using sscanf("%f") is guaranteed precision: float or double types may apply rounding in the conversion result. osmo_float_str_to_int() and osmo_int_to_float_str_buf() guarantee true results when converting back and forth between string and int.

Parameters

  [out] | val | Returned integer value.
  [in] | str | String of a float, like '-12.345'.
  [in] | precision | Fixed-point precision, or *

Returns
  0 on success, negative on error.

References OSMO_ASSERT, and osmo_strlcpy().

## osmo_get_macaddr()

int osmo_get_macaddr | ( | uint8_t * | mac_out,
|  | const char * | dev_name )

Obtain the MAC address of a given network device.

Parameters

  [out] | mac_out | pointer to caller-allocated buffer of 6 bytes
  [in] | dev_name | string name of the network device

Returns
  0 in case of success; negative otherwise

## osmo_hexdump()

char * osmo_hexdump | ( | const unsigned char * | buf,
|  | int | len )

Convert binary sequence to hexadecimal ASCII string.

Parameters

  [in] | buf | pointer to sequence of bytes
  [in] | len | length of buf in number of bytes

Returns
  pointer to zero-terminated string

This function will print a sequence of bytes as hexadecimal numbers, adding one space character between each byte (e.g. "1a ef d9")

The maximum size of the output buffer is 4096 bytes, i.e. the maximum number of input bytes that can be printed in one call is 1365!

References hexd_buff, and osmo_hexdump_buf().

Referenced by _msgb_eq(), msgb_hexdump_buf(), msgb_hexdump_l1(), msgb_hexdump_l2(), msgb_hexdump_l3(), and msgb_hexdump_l4().

## osmo_hexdump_buf()

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.

Parameters

  [out] | out_buf | Output buffer to write the resulting string to.
  [in] | out_buf_size | sizeof(out_buf).
  [in] | buf | Input buffer, pointer to sequence of bytes.
  [in] | len | Length of input buf in number of bytes.
  [in] | delim | String to separate each byte; NULL or "" for no delim.
  [in] | delim_after_last | If true, end the string in delim (true: "1a:ef:d9:", false: "1a:ef:d9"); if out_buf has insufficient space, the string will always end in a delim.

Returns
  out_buf, containing a zero-terminated string, or "" (empty string) if out_buf == NULL or out_buf_size < 1.

This function will print a sequence of bytes as hexadecimal numbers, adding one delim between each byte (e.g. for delim passed as ":", return a string like "1a:ef:d9").

The delim_after_last argument exists to be able to exactly show the original osmo_hexdump() behavior, which always ends the string with a delimiter.

References hex_chars.

Referenced by osmo_hexdump(), osmo_hexdump_c(), osmo_hexdump_nospc(), and osmo_hexdump_nospc_c().

## osmo_hexdump_c()

char * osmo_hexdump_c | ( | const void * | ctx,
|  | const unsigned char * | buf,
|  | int | len )

Convert binary sequence to hexadecimal ASCII string.

Parameters

  [in] | ctx | talloc context from where to allocate the output string
  [in] | buf | pointer to sequence of bytes
  [in] | len | length of buf in number of bytes

Returns
  pointer to zero-terminated string

This function will print a sequence of bytes as hexadecimal numbers, adding one space character between each byte (e.g. "1a ef d9")

References hexd_buff, and osmo_hexdump_buf().

## osmo_hexdump_nospc()

char * osmo_hexdump_nospc | ( | const unsigned char * | buf,
|  | int | len )

Convert binary sequence to hexadecimal ASCII string.

Parameters

  [in] | buf | pointer to sequence of bytes
  [in] | len | length of buf in number of bytes

Returns
  pointer to zero-terminated string

This function will print a sequence of bytes as hexadecimal numbers, without any space character between each byte (e.g. "1aefd9")

The maximum size of the output buffer is 4096 bytes, i.e. the maximum number of input bytes that can be printed in one call is 2048!

References hexd_buff, and osmo_hexdump_buf().

## osmo_hexdump_nospc_c()

char * osmo_hexdump_nospc_c | ( | const void * | ctx,
|  | const unsigned char * | buf,
|  | int | len )

Convert binary sequence to hexadecimal ASCII string.

Parameters

  [in] | ctx | talloc context from where to allocate the output string
  [in] | buf | pointer to sequence of bytes
  [in] | len | length of buf in number of bytes

Returns
  pointer to zero-terminated string

This function will print a sequence of bytes as hexadecimal numbers, without any space character between each byte (e.g. "1aefd9")

References hexd_buff, and osmo_hexdump_buf().

## osmo_hexparse()

int osmo_hexparse | ( | const char * | str,
|  | uint8_t * | b,
|  | unsigned int | max_len )

Parse a string containing hexadecimal digits.

Parameters

  [in] | str | string containing ASCII encoded hexadecimal digits
  [out] | b | output buffer
  [in] | max_len | maximum space in output buffer

Returns
  number of parsed octets, or -1 on error

Referenced by bitvec_unhex().

## osmo_identifier_sanitize_buf()

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().

To guarantee passing osmo_separated_identifiers_valid(), replace_with must not itself be an illegal character. If in doubt, use '-'.

Parameters

  [in,out] | str | Identifier to sanitize, must be nul terminated and in a writable buffer.
  [in] | sep_chars | Additional characters that are to be replaced besides osmo_identifier_illegal_chars.
  [in] | replace_with | Replace any illegal characters with this character.

References osmo_identifier_illegal_chars.

Referenced by osmo_fsm_inst_update_id_f_sanitize().

## osmo_identifier_valid()

bool osmo_identifier_valid | ( | const char * | str | ) |

Determine if a given identifier is valid, i.e.

doesn't contain illegal chars

Parameters

  [in] | str | String to validate

Returns
  true in case str contains valid identifier, false otherwise

References osmo_separated_identifiers_valid().

Referenced by osmo_fsm_inst_update_id_f(), osmo_fsm_register(), and rate_ctrl_group_desc_validate().

## osmo_int_to_float_str_buf()

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).

For example, with precision = 3, convert -1230 to "-1.23". The usable range of digits is -INT64_MAX .. INT64_MAX – note, not INT64_MIN! The value of INT64_MIN is excluded to reduce implementation complexity. See also utils_test.c. The advantage over using printf("%.6g") is guaranteed precision: float or double types may apply rounding in the conversion result. osmo_float_str_to_int() and osmo_int_to_float_str_buf() guarantee true results when converting back and forth between string and int. The resulting string omits trailing zeros in the fractional part (like "%g" would) but never applies rounding.

Parameters

  [out] | buf | Buffer to write string to.
  [in] | buflen | sizeof(buf).
  [in] | val | Value to convert to float.

Returns
  number of chars that would be written, like snprintf().

References osmo_strbuf::buf, osmo_strbuf::chars_needed, and OSMO_STRBUF_PRINTF.

Referenced by osmo_int_to_float_str_c().

## osmo_int_to_float_str_c()

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.

For example, convert -1230000 to "-1.23".

Parameters

  [in] | ctx | Talloc ctx to allocate string buffer from.
  [in] | val | Value to convert to float.

Returns
  resulting string, dynamically allocated.

References osmo_int_to_float_str_buf(), and OSMO_NAME_C_IMPL.

## osmo_is_hexstr()

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.

Note that each hex digit amounts to a nibble, so if checking for a hex string to result in N bytes, pass amount of digits as 2*N.

Parameters

  str | A nul-terminated string to validate, or NULL.
  min_digits | least permitted amount of digits.
  max_digits | most permitted amount of digits.
  require_even | if true, require an even amount of digits.

Returns
  true when the hex_str contains only hexadecimal digits (no whitespace) and matches the requested length; also true when min_digits <= 0 and str is NULL.

## osmo_isqrt32()

uint32_t osmo_isqrt32 | ( | uint32_t | x | ) |

perform an integer square root operation on unsigned 32bit integer.

This implementation is taken from "Hacker's Delight" Figure 11-1 "Integer square root, Newton's method", which can also be found at http://www.hackersdelight.org/hdcodetxt/isqrt.c.txt

References x.

## osmo_luhn()

char osmo_luhn | ( | const char * | in,
|  | int | in_len )

Calculate the Luhn checksum (as used for IMEIs).

Parameters

  [in] | in | Input digits in ASCII string representation.
  [in] | in_len | Count of digits to use for the input (14 for IMEI).

Returns
  checksum char (e.g. '3'); negative on error

## osmo_macaddr_parse()

int osmo_macaddr_parse | ( | uint8_t * | out,
|  | const char * | in )

Parse a MAC address from human-readable notation This function parses an ethernet MAC address in the commonly-used hex/colon notation (00:00:00:00:00:00) and generates the binary representation from it.

Parameters

  [out] | out | pointer to caller-allocated buffer of 6 bytes
  [in] | in | pointer to input data as string with hex/colon notation

## osmo_osmo_hexdump_nospc()

char * osmo_osmo_hexdump_nospc | ( | const unsigned char * | buf,
|  | int | len )

## osmo_panic()

void osmo_panic | ( | const char * | fmt,
|  |  | ... )

extern

Terminate the current program with a panic.

You can call this function in case some severely unexpected situation is detected and the program is supposed to terminate in a way that reports the fact that it terminates.

The application can register a panic handler function using osmo_set_panic_handler. If it doesn't, a default panic handler function is called automatically.

The default function on most systems will generate a backtrace and then abort() the process.

References osmo_panic_default(), and osmo_panic_handler.

Referenced by bit_value_to_char(), and osmo_tdefs_reset().

## osmo_panic_default()

void osmo_panic_default | ( | const char * | fmt,
|  | va_list | args )

static

References osmo_generate_backtrace().

Referenced by osmo_panic().

## osmo_plugin_load_all()

int osmo_plugin_load_all | ( | const char * | directory | ) |

## osmo_print_n()

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().

Similarly to snprintf(), the result is always nul terminated (except if buf is NULL or bufsize is 0).

Parameters

  [out] | buf | Target buffer.
  [in] | bufsize | sizeof(buf).
  [in] | str | String to copy.
  [in] | n | Maximum number of non-nul characters to copy.

Returns
  Number of characters that would be written if bufsize were large enough excluding '\0' (like snprintf()).

Referenced by _osmo_escape_str_buf().

## osmo_quote_cstr_buf()

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.

This allows passing any char* value and get its C representation as string. The function signature is suitable for OSMO_STRBUF_APPEND_NOLEN(). In contrast to osmo_escape_str_buf2(), this returns the needed buffer size suitable for OSMO_STRBUF_APPEND(), and this escapes characters in a way compatible with C string constant syntax.

Parameters

  [out] | buf | string buffer to write escaped characters to.
  [in] | bufsize | sizeof(buf).
  [in] | str | A string that may contain any characters.
  [in] | in_len | Pass -1 to print until nul char, or >= 0 to force a length.

Returns
  Number of characters that would be written if bufsize were large enough excluding '\0' (like snprintf()).

References _osmo_quote_str_buf(), and osmo_strbuf::buf.

## osmo_quote_cstr_c()

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.

In contrast to osmo_quote_str_c(), this escapes characters in a way compatible with C string constant syntax, and allocates sufficient memory in all cases.

Parameters

  [in] | str | A string that may contain any characters.
  [in] | len | Pass -1 to print until nul char, or >= 0 to force a length.

Returns
  dynamically-allocated buffer, containing a quoted and escaped representation.

References _osmo_quote_str_buf(), and OSMO_NAME_C_IMPL.

## osmo_quote_str()

const char * osmo_quote_str | ( | const char * | str,
|  | int | in_len )

Like osmo_quote_str_buf() but returns the result in a static buffer.

The static buffer is shared with get_value_string() and osmo_escape_str().

Parameters

  [in] | str | A string that may contain any characters.
  [in] | in_len | Pass -1 to print until nul char, or >= 0 to force a length.

Returns
  static buffer containing a quoted and escaped representation, possibly truncated.

References _osmo_quote_str_buf(), and namebuf.

Referenced by osmo_fsm_inst_update_id_f().

## osmo_quote_str_buf()

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.

Most *_buf() functions have the buffer and size as first arguments, here the arguments are last. In particular, this function signature doesn't work with OSMO_STRBUF_APPEND_NOLEN().

Parameters

  [in] | str | A string that may contain any characters.
  [in] | in_len | Pass -1 to print until nul char, or >= 0 to force a length.

Returns
  buf containing a quoted and escaped representation, possibly truncated.

References _osmo_quote_str_buf(), and osmo_strbuf::buf.

## osmo_quote_str_buf2()

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.

This allows passing any char* value and get its C representation as string. The function signature is suitable for OSMO_STRBUF_APPEND_NOLEN().

Parameters

  [out] | buf | string buffer to write escaped characters to.
  [in] | bufsize | sizeof(buf).
  [in] | str | A string that may contain any characters.
  [in] | in_len | Pass -1 to print until nul char, or >= 0 to force a length.

Returns
  The output buffer (buf).

References _osmo_quote_str_buf(), and osmo_strbuf::buf.

## osmo_quote_str_buf3()

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.

This allows passing any char* value and get its C representation as string. The function signature is suitable for OSMO_STRBUF_APPEND_NOLEN().

Parameters

  [out] | buf | string buffer to write escaped characters to.
  [in] | bufsize | sizeof(buf).
  [in] | str | A string that may contain any characters.
  [in] | in_len | Pass -1 to print until nul char, or >= 0 to force a length.

Returns
  Number of characters that would be written if bufsize were large enough excluding '\0' (like snprintf()).

References _osmo_quote_str_buf(), and osmo_strbuf::buf.

## osmo_quote_str_c()

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.

Parameters

  [in] | str | A string that may contain any characters.
  [in] | in_len | Pass -1 to print until nul char, or >= 0 to force a length.

Returns
  dynamically-allocated buffer containing a quoted and escaped representation.

References _osmo_quote_str_buf(), and OSMO_NAME_C_IMPL.

## osmo_separated_identifiers_valid()

bool osmo_separated_identifiers_valid | ( | const char * | str,
|  | const char * | sep_chars )

Determine if a given identifier is valid, i.e.

doesn't contain illegal chars

Parameters

  [in] | str | String to validate
  [in] | sep_chars | Permitted separation characters between identifiers.

Returns
  true in case str contains only valid identifiers and sep_chars, false otherwise

References osmo_identifier_illegal_chars.

Referenced by osmo_identifier_valid().

## osmo_set_panic_handler()

void osmo_set_panic_handler | ( | osmo_panic_handler_t | h | ) |

extern

Set the panic handler.

Parameters

  [in] | h | New panic handler function

This changes the panic handling function from the currently active function to a new call-back function supplied by the caller.

References osmo_panic_handler.

## osmo_str2bcd()

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.

The given nibble offsets are interpreted in BCD order, i.e. nibble 0 is bcd[0] & 0x0f, nibble 1 is bcd[0] & 0xf0, nibble 3 is bcd[1] & 0x0f, etc..

Parameters

  [out] | dst | Output BCD buffer.
  [in] | dst_size | sizeof() the output string buffer.
  [in] | digits | String containing decimal or hexadecimal digits in upper or lower case.
  [in] | start_nibble | Offset to start from, in nibbles, typically 1 to skip the first (MI type) nibble.
  [in] | end_nibble | Negative to write all digits found in str, followed by 0xf nibbles to fill any started octet. If >= 0, stop before this offset in nibbles, e.g. to get default behavior, pass start_nibble + strlen(str) + ((start_nibble + strlen(str)) & 1? 1 : 0) + 1.
  [in] | allow_hex | If false, return error if there are hexadecimal digits (A-F). If true, write those to BCD.

Returns
  The buffer size in octets that is used to place all bcd digits (including the skipped nibbles from 'start_nibble' and rounded up to full octets); -EINVAL on invalid digits; -ENOMEM if dst is NULL, if dst_size is too small to contain all nibbles, or if start_nibble is negative.

## osmo_str2lower()

void osmo_str2lower | ( | char * | out,
|  | const char * | in )

## osmo_str2upper()

void osmo_str2upper | ( | char * | out,
|  | const char * | in )

## osmo_str_startswith()

bool osmo_str_startswith | ( | const char * | str,
|  | const char * | startswith_str )

Compare start of a string.

This is an optimisation of 'strstr(str, startswith_str) == str' because it doesn't search through the entire string.

Parameters

  str | (Longer) string to compare.
  startswith_str | (Shorter) string to compare with the start of str.

Returns
  true iff the first characters of str fully match startswith_str or startswith_str is empty.

## osmo_str_to_int()

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.

Same as osmo_str_to_int64() but using the plain int data type.

Parameters

  [out] | result | Buffer for the resulting integer number, or NULL if the caller is only interested in the validation result (returned rc).
  [in] | str | The string to convert.
  [in] | base | The integer base, i.e. 10 for decimal numbers or 16 for hexadecimal, as in strtoll().
  [in] | min_val | The smallest valid number expected in the string.
  [in] | max_val | The largest valid number expected in the string.

Returns
  0 on success, -EOVERFLOW if the number in the string exceeds int range, -ENOTSUPP if the base is not supported, -ERANGE if the converted number exceeds the range [min_val..max_val] but is still within int range, -E2BIG if surplus characters follow after the number, -EINVAL if the string does not contain a number. In case of -ERANGE and -E2BIG, the converted number is still accurately returned in result. In case of -EOVERFLOW, the returned value is clamped to INT_MIN..INT_MAX.

References osmo_str_to_int64().

## osmo_str_to_int64()

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.

It's not so trivial to call strtoll() and properly verify that the input string was indeed a valid number string.

Parameters

  [out] | result | Buffer for the resulting integer number, or NULL if the caller is only interested in the validation result (returned rc).
  [in] | str | The string to convert.
  [in] | base | The integer base, i.e. 10 for decimal numbers or 16 for hexadecimal, as in strtoll().
  [in] | min_val | The smallest valid number expected in the string.
  [in] | max_val | The largest valid number expected in the string.

Returns
  0 on success, -EOVERFLOW if the number in the string exceeds int64_t, -ENOTSUPP if the base is not supported, -ERANGE if the converted number exceeds the range [min_val..max_val] but is still within int64_t range, -E2BIG if surplus characters follow after the number, -EINVAL if the string does not contain a number. In case of -ERANGE and -E2BIG, the converted number is still accurately returned in result. In case of -EOVERFLOW, the returned value is clamped to INT64_MIN..INT64_MAX.

Referenced by osmo_str_to_int().

## osmo_str_tolower()

const char * osmo_str_tolower | ( | const char * | src | ) |

Convert a string to lowercase, using a static buffer.

The resulting string may be truncated if the internally used static buffer is shorter than src. The internal buffer is at least 128 bytes long, i.e. guaranteed to hold at least 127 characters and a terminating nul. The static buffer returned is shared with osmo_str_toupper(). See also osmo_str_tolower_buf().

Parameters

  [in] | src | String to convert to lowercase.

Returns
  Resulting lowercase string in a static buffer, always nul terminated.

References capsbuf, and osmo_str_tolower_buf().

## osmo_str_tolower_buf()

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.

The result written to dest is guaranteed to be nul terminated if dest_len > 0. If dest == src, the string is converted in-place, if necessary truncated at dest_len - 1 characters length as well as nul terminated. Note: similar osmo_str2lower(), but safe to use for src strings of arbitrary length.

Parameters

  [out] | dest | Target buffer to write lowercase string.
  [in] | dest_len | Maximum buffer size of dest (e.g. sizeof(dest)).
  [in] | src | String to convert to lowercase.

Returns
  Length of src, like osmo_strlcpy(), but if dest == src at most dest_len - 1.

References osmo_strlcpy().

Referenced by osmo_str_tolower(), and osmo_str_tolower_c().

## osmo_str_tolower_c()

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().

Parameters

  [in] | ctx | talloc context from where to allocate the output string
  [in] | src | String to convert to lowercase.

Returns
  Resulting lowercase string in a dynamically allocated buffer, always nul terminated.

References osmo_strbuf::buf, and osmo_str_tolower_buf().

## osmo_str_toupper()

const char * osmo_str_toupper | ( | const char * | src | ) |

Convert a string to uppercase, using a static buffer.

The resulting string may be truncated if the internally used static buffer is shorter than src. The internal buffer is at least 128 bytes long, i.e. guaranteed to hold at least 127 characters and a terminating nul. The static buffer returned is shared with osmo_str_tolower(). See also osmo_str_toupper_buf().

Parameters

  [in] | src | String to convert to uppercase.

Returns
  Resulting uppercase string in a static buffer, always nul terminated.

References capsbuf, and osmo_str_toupper_buf().

## osmo_str_toupper_buf()

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.

The result written to dest is guaranteed to be nul terminated if dest_len > 0. If dest == src, the string is converted in-place, if necessary truncated at dest_len - 1 characters length as well as nul terminated. Note: similar osmo_str2upper(), but safe to use for src strings of arbitrary length.

Parameters

  [out] | dest | Target buffer to write uppercase string.
  [in] | dest_len | Maximum buffer size of dest (e.g. sizeof(dest)).
  [in] | src | String to convert to uppercase.

Returns
  Length of src, like osmo_strlcpy(), but if dest == src at most dest_len - 1.

References osmo_strlcpy().

Referenced by osmo_str_toupper(), and osmo_str_toupper_c().

## osmo_str_toupper_c()

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().

Parameters

  [in] | ctx | talloc context from where to allocate the output string
  [in] | src | String to convert to uppercase.

Returns
  Resulting uppercase string in a dynamically allocated buffer, always nul terminated.

References osmo_strbuf::buf, and osmo_str_toupper_buf().

## osmo_strbuf_added_tail()

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.

If sb is nonempty, the n_chars are assumed to have been written to sb->pos. If sb is still empty and pos == NULL, the n_chars are assumed to have been written to the start of the buffer. Advance sb->pos and sb->chars_needed by at most n_chars, or up to sb->len - 1. Ensure nul termination.

References osmo_strbuf::buf, osmo_strbuf::chars_needed, osmo_strbuf::len, OSMO_MIN, OSMO_STRBUF_REMAIN, and osmo_strbuf::pos.

## osmo_strbuf_drop_tail()

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.

|–char-count—| - - chars_needed - - | |<------—drop-------—|

References osmo_strbuf::buf, osmo_strbuf::chars_needed, OSMO_MIN, OSMO_STRBUF_CHAR_COUNT, and osmo_strbuf::pos.

## osmo_strlcpy()

size_t osmo_strlcpy | ( | char * | dst,
|  | const char * | src,
|  | size_t | siz )

Copy a C-string into a sized buffer.

Parameters

  [in] | src | source string
  [out] | dst | destination string
  [in] | siz | size of the dst buffer

Returns
  length of src

Copy at most siz bytes from src to dst, ensuring that the result is NUL terminated. The NUL character is included in siz, i.e. passing the actual sizeof(*dst) is correct.

Note, a similar function that also limits the input buffer size is osmo_print_n().

References OSMO_MIN.

Referenced by osmo_float_str_to_int(), osmo_str_tolower_buf(), and osmo_str_toupper_buf().

## osmo_strnchr()

const char * osmo_strnchr | ( | const char * | str,
|  | size_t | str_size,
|  | char | c )

Find first occurence of a char in a size limited string.

Like strchr() but with a buffer size limit.

Parameters

  [in] | str | String buffer to examine.
  [in] | str_size | sizeof(str).
  [in] | c | Character to look for.

Returns
  Pointer to the matched char, or NULL if not found.

## osmo_strrb_add()

int osmo_strrb_add | ( | struct osmo_strrb * | rb,
|  | const char * | data )

Add a string to the osmo_strrb.

Parameters

  [in] | rb | The osmo_strrb to add to.
  [in] | data | The string to add.

Add a message to the osmo_strrb. Older messages will be overwritten as necessary.

Returns
  0 normally, 1 as a warning (ie, if data was truncated).

References osmo_strrb::buffer, osmo_strrb::end, RB_MAX_MESSAGE_SIZE, osmo_strrb::size, and osmo_strrb::start.

Referenced by _rb_output().

## osmo_strrb_create()

struct osmo_strrb * osmo_strrb_create | ( | void * | talloc_ctx,
|  | size_t | rb_size )

Create an empty, initialized osmo_strrb.

Parameters

  [in] | ctx | The talloc memory context which should own this.
  [in] | rb_size | The number of message slots the osmo_strrb can hold.

Returns
  A struct osmo_strrb* on success, NULL in case of error.

This function creates and initializes a ringbuffer. Note that the ringbuffer stores at most rb_size - 1 messages.

References osmo_strrb::buffer, RB_MAX_MESSAGE_SIZE, and osmo_strrb::size.

Referenced by log_target_create_rb().

## osmo_strrb_elements()

size_t osmo_strrb_elements | ( | const struct osmo_strrb * | rb | ) |

Count the number of log messages in an osmo_strrb.

Parameters

  [in] | rb | The osmo_strrb to count the elements of.

Returns
  The number of log messages in the osmo_strrb.

References osmo_strrb::end, osmo_strrb::size, and osmo_strrb::start.

Referenced by log_target_rb_used_size().

## osmo_strrb_get_nth()

const char * osmo_strrb_get_nth | ( | const struct osmo_strrb * | rb,
|  | unsigned int | string_index )

Return a pointer to the Nth string in the osmo_strrb.

Parameters

  [in] | rb | The osmo_strrb to search.
  [in] | string_index | The index sought (N), zero-indexed.

Return a pointer to the Nth string in the osmo_strrb. Return NULL if there is no Nth string. Note that N is zero-indexed.

Returns
  A pointer to the target string on success, NULL in case of error.

References _osmo_strrb_is_bufindex_valid(), osmo_strrb::buffer, osmo_strrb::end, osmo_strrb::size, and osmo_strrb::start.

Referenced by log_target_rb_get().

## osmo_strrb_is_empty()

bool osmo_strrb_is_empty | ( | const struct osmo_strrb * | rb | ) |

Check if an osmo_strrb is empty.

Parameters

  [in] | rb | The osmo_strrb to check.

Returns
  True if the osmo_strrb is empty, false otherwise.

References osmo_strrb::end, and osmo_strrb::start.

Referenced by _osmo_strrb_is_bufindex_valid().

## osmo_talloc_replace_string()

void osmo_talloc_replace_string | ( | void * | ctx,
|  | char ** | dst,
|  | const char * | newstr )

inline static

duplicate a string using talloc and release its prior content (if any)

Parameters

  [in] | ctx | Talloc context to use for allocation
  [out] | dst | pointer to string, will be updated with ptr to new string
  [in] | newstr | String that will be copied to newly allocated string

Referenced by osmo_iofd_set_name(), osmo_netdev_register(), osmo_netdev_set_netns_name(), osmo_soft_uart_set_name(), osmo_stat_item_group_set_name(), osmo_tundev_set_dev_name(), osmo_tundev_set_netns_name(), rate_ctr_group_set_name(), and tundev_dev_name_chg_cb().

## osmo_talloc_replace_string_fmt()

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).

This is a format string capable equivalent of osmo_talloc_replace_string().

Parameters

  [in] | ctx | Talloc context to use for allocation.
  [out] | dst | Pointer to string, will be updated with ptr to new string.
  [in] | fmt | Format string that will be copied to newly allocated string.

References name.

## osmo_ubit_dump()

char * osmo_ubit_dump | ( | const uint8_t * | bits,
|  | unsigned int | len )

Convert a sequence of unpacked bits to ASCII string, in static buffer.

Parameters

  [in] | bits | A sequence of unpacked bits
  [in] | len | Length of bits

Returns
  string representation in static buffer.

References hexd_buff, and osmo_ubit_dump_buf().

## osmo_ubit_dump_buf()

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.

Parameters

  [out] | buf | caller-provided output string buffer
  [out] | buf_len | size of buf in bytes
  [in] | bits | A sequence of unpacked bits
  [in] | len | Length of bits

Returns
  The output buffer (buf).

Referenced by osmo_ubit_dump().

## Variable Documentation

## bounds

void to properly check target memory bounds

## capsbuf

__thread char capsbuf[128]

static

Referenced by osmo_str_tolower(), and osmo_str_toupper().

## hex_chars

const char hex_chars[] = "0123456789abcdef"

static

Referenced by osmo_hexdump_buf().

## hexd_buff

__thread char hexd_buff[4096]

static

Referenced by osmo_hexdump(), osmo_hexdump_c(), osmo_hexdump_nospc(), osmo_hexdump_nospc_c(), and osmo_ubit_dump().

## namebuf

__thread char namebuf[255]

static

Referenced by get_value_string(), osmo_escape_str(), and osmo_quote_str().

## osmo_identifier_illegal_chars

const char osmo_identifier_illegal_chars[] = "., {}[]()<>|~\\^`'\"?=;/+*&%$#!"

static

Referenced by osmo_identifier_sanitize_buf(), and osmo_separated_identifiers_valid().

## osmo_panic_handler

osmo_panic_handler_t osmo_panic_handler = (void*)0

static

Referenced by osmo_panic(), and osmo_set_panic_handler().