Loading...
Searching...
No Matches
utils.c File Reference
General-purpose utility functions
`#include <stdbool.h>`
`#include <string.h>`
`#include <stdint.h>`
`#include <errno.h>`
`#include <stdio.h>`
`#include <inttypes.h>`
`#include <limits.h>`
`#include <osmocom/core/utils.h>`
`#include <osmocom/core/bit64gen.h>`
`#include "../config.h"`
`#include <ctype.h>`

## Macros

#define | BACKSLASH_CASE(c, repr)

## Functions

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.
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_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.
char * | osmo_osmo_hexdump_nospc (const unsigned char *buf, int len) __attribute__((__deprecated__))
char | alias ("osmo_hexdump_nospc")))
void | osmo_str2lower (char *out, const char *in)
| Convert an entire string to lower case.
void | osmo_str2upper (char *out, const char *in)
| Convert an entire string to upper case.
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_separated_identifiers_valid (const char *str, const char *sep_chars)
| Determine if a given identifier is valid, i.e.
bool | osmo_identifier_valid (const char *str)
| 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().
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.
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().
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.
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 (const char *str, int in_len)
| Return the string with all non-printable characters escaped.
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.
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.
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.
const char * | osmo_quote_str (const char *str, int in_len)
| Like osmo_quote_str_buf() but returns the result in a static buffer.
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.
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.
uint32_t | osmo_isqrt32 (uint32_t x)
| perform an integer square root operation on unsigned 32bit integer.
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().
char | osmo_luhn (const char *in, int in_len)
| Calculate the Luhn checksum (as used for IMEIs).
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.
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).

## Variables

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 [] = "., {}[]()<>|~\\^`'\"?=;/+*&%$#!"

## Macro Definition Documentation

## BACKSLASH_CASE

#define BACKSLASH_CASE | ( |  | c,
|  |  | repr
| ) |  |

Value:
case c: \
OSMO_STRBUF_PRINTF(sb, "\\%c", repr); \
break
c
struct abis_rsl_common_hdr c
subsys
char subsys[16]
logging sub-system
Definition gsmtap.h:9