19 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) 22 #define OSMO_MAX(a, b) ((a) >= (b) ? (a) : (b)) 24 #define OSMO_MIN(a, b) ((a) >= (b) ? (b) : (a)) 26 #define OSMO_CMP(a, b) ((a) < (b)? -1 : ((a) > (b)? 1 : 0)) 30 #define OSMO_STRINGIFY(x) #x 32 #define OSMO_STRINGIFY_VAL(x) OSMO_STRINGIFY(x) 34 #define OSMO_VALUE_STRING(x) { x, #x } 36 #define OSMO_BYTES_FOR_BITS(BITS) ((BITS + 8 - 1) / 8) 39 #define OSMO_STRLCPY_ARRAY(array, src) osmo_strlcpy(array, src, sizeof(array)) 57 int osmo_bcd2str(
char *dst,
size_t dst_size,
const uint8_t *bcd,
int start_nibble,
int end_nibble,
bool allow_hex);
58 int osmo_str2bcd(uint8_t *dst,
size_t dst_size,
const char *digits,
int start_nibble,
int end_nibble,
bool allow_hex);
68 const char *
osmo_hexdump_buf(
char *out_buf,
size_t out_buf_size,
const unsigned char *buf,
int len,
const char *delim,
69 bool delim_after_last);
73 #define osmo_static_assert(exp, name) typedef int dummy##name [(exp) ? 1 : -1] __attribute__((__unused__)); 76 OSMO_DEPRECATED(
"Use osmo_str_tolower() or osmo_str_tolower_buf() instead," 77 " to properly check target memory bounds");
79 OSMO_DEPRECATED(
"Use osmo_str_toupper() or osmo_str_toupper_buf() instead," 80 " to properly check target memory bounds");
90 #define OSMO_SNPRINTF_RET(ret, rem, offset, len) \ 104 #define OSMO_ASSERT(exp) \ 106 osmo_panic("Assert failed %s %s:%d\n", #exp, __FILE__, __LINE__); \ 117 *dst = talloc_strdup(ctx, newstr);
130 #define osmo_talloc_asprintf(ctx, dest, fmt, args ...) \ 133 dest = talloc_asprintf(ctx, fmt, ## args); \ 135 dest = talloc_asprintf_append((char*)dest, fmt, ## args); \ 142 size_t osmo_strlcpy(
char *dst,
const char *src,
size_t siz);
170 char osmo_luhn(
const char* in,
int in_len);
222 #define OSMO_STRBUF_APPEND(STRBUF, func, args...) do { \ 224 (STRBUF).pos = (STRBUF).buf; \ 225 size_t _sb_remain = (STRBUF).buf ? (STRBUF).len - ((STRBUF).pos - (STRBUF).buf) : 0; \ 226 int _sb_l = func((STRBUF).pos, _sb_remain, ##args); \ 227 if (_sb_l < 0 || _sb_l > _sb_remain) \ 228 (STRBUF).pos = (STRBUF).buf + (STRBUF).len; \ 229 else if ((STRBUF).pos) \ 230 (STRBUF).pos += _sb_l; \ 232 (STRBUF).chars_needed += _sb_l; \ 254 #define OSMO_STRBUF_PRINTF(STRBUF, fmt, args...) \ 255 OSMO_STRBUF_APPEND(STRBUF, snprintf, fmt, ##args) 265 #define OSMO_STRBUF_APPEND_NOLEN(STRBUF, func, args...) do { \ 267 (STRBUF).pos = (STRBUF).buf; \ 268 size_t _sb_remain = (STRBUF).buf ? (STRBUF).len - ((STRBUF).pos - (STRBUF).buf) : 0; \ 270 func((STRBUF).pos, _sb_remain, ##args); \ 272 size_t _sb_l = (STRBUF).pos ? strnlen((STRBUF).pos, _sb_remain) : 0; \ 273 if (_sb_l > _sb_remain) \ 274 (STRBUF).pos = (STRBUF).buf + (STRBUF).len; \ 275 else if ((STRBUF).pos) \ 276 (STRBUF).pos += _sb_l; \ 277 (STRBUF).chars_needed += _sb_l; \ 306 #define OSMO_NAME_C_IMPL(CTX, INITIAL_BUFSIZE, ON_ERROR, FUNC_BUF, FUNC_BUF_ARGS...) \ 307 size_t _len = INITIAL_BUFSIZE; \ 310 if ((INITIAL_BUFSIZE) > 0) { \ 311 _str = (char*)talloc_named_const(CTX, _len, __func__); \ 314 _needed = FUNC_BUF(_str, _len, ## FUNC_BUF_ARGS); \ 316 goto OSMO_NAME_C_on_error; \ 317 if (_needed < _len) \ 319 _len = _needed + 1; \ 322 _str = (char*)talloc_named_const(CTX, _len, __func__); \ 324 _needed = FUNC_BUF(_str, _len, ## FUNC_BUF_ARGS); \ 326 goto OSMO_NAME_C_on_error; \ 328 OSMO_NAME_C_on_error: \ 334 _str = talloc_strdup(CTX, ON_ERROR); \ 336 talloc_set_name_const(_str, __func__); \ 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...
Definition: utils.c:862
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.
Definition: utils.c:620
#define OSMO_DEPRECATED(text)
Set the deprecated attribute with a message.
Definition: defs.h:41
struct osmo_gsm48_classmark __attribute__
Definition: conv_acc_generic.c:140
char * pos
Current writing position in buf (end of the string written so far).
Definition: utils.h:179
void osmo_str2lower(char *out, const char *in) OSMO_DEPRECATED("Use osmo_str_tolower() or osmo_str_tolower_buf() instead
Convert an entire string to lower case.
Definition: utils.c:478
int get_string_value(const struct value_string *vs, const char *str)
get numeric value for given human-readable string
Definition: utils.c:96
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...
Definition: utils.c:904
const char * get_value_string(const struct value_string *vs, uint32_t val)
get human-readable string for given value
Definition: utils.c:57
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...
Definition: utils.c:991
const char * get_value_string_or_null(const struct value_string *vs, uint32_t val)
get human-readable string or NULL for given value
Definition: utils.c:73
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.
Definition: utils.c:961
const char * osmo_escape_str(const char *str, int len)
Return the string with all non-printable characters escaped.
Definition: utils.c:852
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_identif...
Definition: utils.c:692
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.
Definition: utils.c:533
uint8_t osmo_char2bcd(char c)
Convert number in ASCII to BCD value.
Definition: utils.c:125
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.
Definition: utils.c:344
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 o...
Definition: utils.c:1098
int value
numeric value
Definition: utils.h:43
State for OSMO_STRBUF_APPEND() and OSMO_STRBUF_PRINTF().
Definition: utils.h:173
A mapping between human-readable string and numeric value.
Definition: utils.h:42
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)
Definition: utils.h:113
General definitions that are meant to be included from header files.
size_t osmo_strlcpy(char *dst, const char *src, size_t siz)
Copy a C-string into a sized buffer.
Definition: utils.c:574
const char * osmo_quote_str(const char *str, int in_len)
Like osmo_quote_str_buf() but returns the result in a static buffer.
Definition: utils.c:934
bool osmo_separated_identifiers_valid(const char *str, const char *sep_chars)
Determine if a given identifier is valid, i.e.
Definition: utils.c:650
char * osmo_hexdump_c(const void *ctx, const unsigned char *buf, int len)
Convert binary sequence to hexadecimal ASCII string.
Definition: utils.c:413
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...
Definition: utils.c:973
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.
Definition: utils.c:1060
const char * str
human-readable string
Definition: utils.h:44
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.
Definition: utils.c:302
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 con...
Definition: utils.c:918
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().
Definition: utils.c:732
char * osmo_ubit_dump(const uint8_t *bits, unsigned int len)
Convert a sequence of unpacked bits to ASCII string, in static buffer.
Definition: utils.c:379
size_t chars_needed
After all OSMO_STRBUF_APPEND operations, reflects the total number of characters that would be writte...
Definition: utils.h:183
const char * osmo_str_toupper(const char *src)
Convert a string to uppercase, using a static buffer.
Definition: utils.c:1144
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 co...
Definition: utils.c:715
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.
Definition: utils.c:151
uint8_t * osmo_encode_big_endian(uint64_t value, size_t data_len)
Generic big-endian encoding of big endian number up to 64bit.
Definition: utils.c:554
write Write running configuration to or terminal n Write configuration to the copy running config startup Copy configuration n Copy running config to n Copy running config to startup write Write running configuration to or terminal n Write to terminal n
bool osmo_identifier_valid(const char *str)
Determine if a given identifier is valid, i.e.
Definition: utils.c:680
char osmo_bcd2char(uint8_t bcd)
Convert BCD-encoded digit into printable character.
Definition: utils.c:113
int osmo_hexparse(const char *str, uint8_t *b, int max_len)
Parse a string containing hexadecimal digits.
Definition: utils.c:243
char * buf
Point to the start of a string buffer.
Definition: utils.h:175
char osmo_luhn(const char *in, int in_len)
Calculate the Luhn checksum (as used for IMEIs).
Definition: utils.c:1171
const char * osmo_str_tolower(const char *src)
Convert a string to lowercase, using a static buffer.
Definition: utils.c:1086
bool osmo_str_startswith(const char *str, const char *startswith_str)
Compare start of a string.
Definition: utils.c:1202
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.
Definition: utils.c:1118
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.
Definition: utils.c:840
uint32_t osmo_isqrt32(uint32_t x)
perform an integer square root operation on unsigned 32bit integer.
Definition: utils.c:1013
const char * osmo_strnchr(const char *str, size_t str_size, char c)
Find first occurence of a char in a size limited string.
Definition: utils.c:594
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.
Definition: utils.c:945
static size_t len(const char *str)
char * osmo_osmo_hexdump_nospc(const unsigned char *buf, int len) __attribute__((__deprecated__))
char * osmo_hexdump_nospc(const unsigned char *buf, int len)
Convert binary sequence to hexadecimal ASCII string.
Definition: utils.c:434
size_t len
Total sizeof() the buffer buf points at.
Definition: utils.h:177
char * osmo_hexdump(const unsigned char *buf, int len)
Convert binary sequence to hexadecimal ASCII string.
Definition: utils.c:395
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 buff...
Definition: utils.c:1003
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.
Definition: utils.c:194
char * osmo_hexdump_nospc_c(const void *ctx, const unsigned char *buf, int len)
Convert binary sequence to hexadecimal ASCII string.
Definition: utils.c:452
void osmo_str2upper(char *out, const char *in) OSMO_DEPRECATED("Use osmo_str_toupper() or osmo_str_toupper_buf() instead
Convert an entire string to upper case.
Definition: utils.c:491
int osmo_constant_time_cmp(const uint8_t *exp, const uint8_t *rel, const int count)
Wishful thinking to generate a constant time compare.
Definition: utils.c:511
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 o...
Definition: utils.c:1156