libosmocore  1.6.0
Osmocom core library
utils.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <stdbool.h>
4 #include <stdint.h>
5 #include <stdio.h>
6 #include <string.h>
7 
9 #include <osmocom/core/talloc.h>
10 #include <osmocom/core/panic.h>
11 #include <osmocom/core/defs.h>
12 
18 #ifndef ARRAY_SIZE
19 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
20 #endif
21 
22 #define OSMO_MAX(a, b) ((a) >= (b) ? (a) : (b))
23 
24 #define OSMO_MIN(a, b) ((a) >= (b) ? (b) : (a))
25 
26 #define OSMO_CMP(a, b) ((a) < (b)? -1 : ((a) > (b)? 1 : 0))
27 
30 #define OSMO_STRINGIFY(x) #x
31 
32 #define OSMO_STRINGIFY_VAL(x) OSMO_STRINGIFY(x)
33 
34 #define OSMO_VALUE_STRING(x) { x, #x }
35 
36 #define OSMO_BYTES_FOR_BITS(BITS) ((BITS + 8 - 1) / 8)
37 
39 #define OSMO_STRLCPY_ARRAY(array, src) osmo_strlcpy(array, src, sizeof(array))
40 
42 struct value_string {
43  int value;
44  const char *str;
45 };
46 
47 const char *get_value_string(const struct value_string *vs, uint32_t val);
48 const char *get_value_string_or_null(const struct value_string *vs,
49  uint32_t val);
50 
51 int get_string_value(const struct value_string *vs, const char *str);
52 
53 char osmo_bcd2char(uint8_t bcd);
54 /* only works for numbers in ASCII */
55 uint8_t osmo_char2bcd(char c);
56 
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);
59 
60 int osmo_hexparse(const char *str, uint8_t *b, int max_len);
61 
62 char *osmo_ubit_dump_buf(char *buf, size_t buf_len, const uint8_t *bits, unsigned int len);
63 char *osmo_ubit_dump(const uint8_t *bits, unsigned int len);
64 char *osmo_hexdump(const unsigned char *buf, int len);
65 char *osmo_hexdump_c(const void *ctx, const unsigned char *buf, int len);
66 char *osmo_hexdump_nospc(const unsigned char *buf, int len);
67 char *osmo_hexdump_nospc_c(const void *ctx, const unsigned char *buf, int len);
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);
70 
71 char *osmo_osmo_hexdump_nospc(const unsigned char *buf, int len) __attribute__((__deprecated__));
72 
73 #define osmo_static_assert(exp, name) typedef int dummy##name [(exp) ? 1 : -1] __attribute__((__unused__));
74 
75 void osmo_str2lower(char *out, const char *in)
76  OSMO_DEPRECATED("Use osmo_str_tolower() or osmo_str_tolower_buf() instead,"
77  " to properly check target memory bounds");
78 void osmo_str2upper(char *out, const char *in)
79  OSMO_DEPRECATED("Use osmo_str_toupper() or osmo_str_toupper_buf() instead,"
80  " to properly check target memory bounds");
81 
82 size_t osmo_str_tolower_buf(char *dest, size_t dest_len, const char *src);
83 const char *osmo_str_tolower(const char *src);
84 char *osmo_str_tolower_c(const void *ctx, const char *src);
85 
86 size_t osmo_str_toupper_buf(char *dest, size_t dest_len, const char *src);
87 const char *osmo_str_toupper(const char *src);
88 char *osmo_str_toupper_c(const void *ctx, const char *src);
89 
90 #define OSMO_SNPRINTF_RET(ret, rem, offset, len) \
91 do { \
92  len += ret; \
93  if (ret > rem) \
94  ret = rem; \
95  offset += ret; \
96  rem -= ret; \
97 } while (0)
98 
104 #define OSMO_ASSERT(exp) \
105  if (!(exp)) { \
106  osmo_panic("Assert failed %s %s:%d\n", #exp, __FILE__, __LINE__); \
107  }
108 
113 static inline void osmo_talloc_replace_string(void *ctx, char **dst, const char *newstr)
114 {
115  if (*dst)
116  talloc_free(*dst);
117  *dst = talloc_strdup(ctx, newstr);
118 }
119 
120 void osmo_talloc_replace_string_fmt(void *ctx, char **dst, const char *fmt, ...);
121 
132 #define osmo_talloc_asprintf(ctx, dest, fmt, args ...) \
133  do { \
134  if (!dest) \
135  dest = talloc_asprintf(ctx, fmt, ## args); \
136  else \
137  dest = talloc_asprintf_append((char*)dest, fmt, ## args); \
138  } while (0)
139 
140 int osmo_constant_time_cmp(const uint8_t *exp, const uint8_t *rel, const int count);
141 uint64_t osmo_decode_big_endian(const uint8_t *data, size_t data_len);
142 uint8_t *osmo_encode_big_endian(uint64_t value, size_t data_len);
143 
144 size_t osmo_strlcpy(char *dst, const char *src, size_t siz);
145 const char *osmo_strnchr(const char *str, size_t str_size, char c);
146 
147 bool osmo_is_hexstr(const char *str, int min_digits, int max_digits,
148  bool require_even);
149 
150 bool osmo_identifier_valid(const char *str);
151 bool osmo_separated_identifiers_valid(const char *str, const char *sep_chars);
152 void osmo_identifier_sanitize_buf(char *str, const char *sep_chars, char replace_with);
153 
154 size_t osmo_escape_cstr_buf(char *buf, size_t bufsize, const char *str, int in_len);
155 char *osmo_escape_cstr_c(void *ctx, const char *str, int in_len);
156 size_t osmo_quote_cstr_buf(char *buf, size_t bufsize, const char *str, int in_len);
157 char *osmo_quote_cstr_c(void *ctx, const char *str, int in_len);
158 
159 const char *osmo_escape_str(const char *str, int len);
160 char *osmo_escape_str_buf2(char *buf, size_t bufsize, const char *str, int in_len);
161 const char *osmo_escape_str_buf(const char *str, int in_len, char *buf, size_t bufsize);
162 char *osmo_escape_str_c(const void *ctx, const char *str, int in_len);
163 const char *osmo_quote_str(const char *str, int in_len);
164 char *osmo_quote_str_buf2(char *buf, size_t bufsize, const char *str, int in_len);
165 const char *osmo_quote_str_buf(const char *str, int in_len, char *buf, size_t bufsize);
166 char *osmo_quote_str_c(const void *ctx, const char *str, int in_len);
167 
168 int osmo_print_n(char *buf, size_t bufsize, const char *str, size_t n);
169 
170 uint32_t osmo_isqrt32(uint32_t x);
171 
172 char osmo_luhn(const char* in, int in_len);
173 
175 struct osmo_strbuf {
177  char *buf;
179  size_t len;
181  char *pos;
185  size_t chars_needed;
186 };
187 
224 #define OSMO_STRBUF_APPEND(STRBUF, func, args...) do { \
225  if (!(STRBUF).pos) \
226  (STRBUF).pos = (STRBUF).buf; \
227  size_t _sb_remain = (STRBUF).buf ? (STRBUF).len - ((STRBUF).pos - (STRBUF).buf) : 0; \
228  int _sb_l = func((STRBUF).pos, _sb_remain, ##args); \
229  if (_sb_l < 0 || (size_t)_sb_l > _sb_remain) \
230  (STRBUF).pos = (STRBUF).buf + (STRBUF).len; \
231  else if ((STRBUF).pos) \
232  (STRBUF).pos += _sb_l; \
233  if (_sb_l > 0) \
234  (STRBUF).chars_needed += _sb_l; \
235  } while(0)
236 
256 #define OSMO_STRBUF_PRINTF(STRBUF, fmt, args...) \
257  OSMO_STRBUF_APPEND(STRBUF, snprintf, fmt, ##args)
258 
267 #define OSMO_STRBUF_APPEND_NOLEN(STRBUF, func, args...) do { \
268  if (!(STRBUF).pos) \
269  (STRBUF).pos = (STRBUF).buf; \
270  size_t _sb_remain = (STRBUF).buf ? (STRBUF).len - ((STRBUF).pos - (STRBUF).buf) : 0; \
271  if (_sb_remain) { \
272  func((STRBUF).pos, _sb_remain, ##args); \
273  } \
274  size_t _sb_l = (STRBUF).pos ? strnlen((STRBUF).pos, _sb_remain) : 0; \
275  if (_sb_l > _sb_remain) \
276  (STRBUF).pos = (STRBUF).buf + (STRBUF).len; \
277  else if ((STRBUF).pos) \
278  (STRBUF).pos += _sb_l; \
279  (STRBUF).chars_needed += _sb_l; \
280  } while(0)
281 
282 bool osmo_str_startswith(const char *str, const char *startswith_str);
283 
284 int osmo_float_str_to_int(int64_t *val, const char *str, unsigned int precision);
285 int osmo_int_to_float_str_buf(char *buf, size_t buflen, int64_t val, unsigned int precision);
286 char *osmo_int_to_float_str_c(void *ctx, int64_t val, unsigned int precision);
287 
288 int osmo_str_to_int64(int64_t *result, const char *str, int base, int64_t min_val, int64_t max_val);
289 int osmo_str_to_int(int *result, const char *str, int base, int min_val, int max_val);
290 
315 #define OSMO_NAME_C_IMPL(CTX, INITIAL_BUFSIZE, ON_ERROR, FUNC_BUF, FUNC_BUF_ARGS...) \
316  size_t _len = INITIAL_BUFSIZE; \
317  int _needed; \
318  char *_str = NULL; \
319  if ((INITIAL_BUFSIZE) > 0) { \
320  _str = (char*)talloc_named_const(CTX, _len, __func__); \
321  OSMO_ASSERT(_str); \
322  } \
323  _needed = FUNC_BUF(_str, _len, ## FUNC_BUF_ARGS); \
324  if (_needed < 0) \
325  goto OSMO_NAME_C_on_error; \
326  if (_needed < _len) \
327  return _str; \
328  _len = _needed + 1; \
329  if (_str) \
330  talloc_free(_str); \
331  _str = (char*)talloc_named_const(CTX, _len, __func__); \
332  OSMO_ASSERT(_str); \
333  _needed = FUNC_BUF(_str, _len, ## FUNC_BUF_ARGS); \
334  if (_needed < 0) \
335  goto OSMO_NAME_C_on_error; \
336  return _str; \
337 OSMO_NAME_C_on_error: \
338  /* Re-using and re-sizing above allocated buf ends up in very complex code. Just free and strdup. */ \
339  if (_str) \
340  talloc_free(_str); \
341  if (!(ON_ERROR)) \
342  return NULL; \
343  _str = talloc_strdup(CTX, ON_ERROR); \
344  OSMO_ASSERT(_str); \
345  talloc_set_name_const(_str, __func__); \
346  return _str;
347 
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.
Definition: utils.c:615
osmo_char2bcd
uint8_t osmo_char2bcd(char c)
Convert number in ASCII to BCD value.
Definition: utils.c:126
osmo_str2lower
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:473
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.
Definition: utils.c:345
OSMO_DEPRECATED
#define OSMO_DEPRECATED(text)
Set the deprecated attribute with a message.
Definition: defs.h:41
get_value_string
const char * get_value_string(const struct value_string *vs, uint32_t val)
get human-readable string for given value
Definition: utils.c:58
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.
Definition: utils.c:986
len
static size_t len(const char *str)
n
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
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.
Definition: utils.c:899
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_identif...
Definition: utils.c:687
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.
Definition: utils.c:956
osmo_escape_str
const char * osmo_escape_str(const char *str, int len)
Return the string with all non-printable characters escaped.
Definition: utils.c:847
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).
Definition: utils.c:1501
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.
Definition: utils.c:528
__attribute__
struct gsm48_classmark3 __attribute__
fls64 - find last set bit in a 64-bit word @x: the word to search
Definition: log2.h:61
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.
Definition: utils.c:645
backtrace.h
get_string_value
int get_string_value(const struct value_string *vs, const char *str)
get numeric value for given human-readable string
Definition: utils.c:97
osmo_talloc_replace_string
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
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 o...
Definition: utils.c:1093
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
Definition: utils.c:74
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.
Definition: utils.c:968
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:569
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.
Definition: utils.c:929
osmo_strbuf::chars_needed
size_t chars_needed
After all OSMO_STRBUF_APPEND operations, reflects the total number of characters that would be writte...
Definition: utils.h:185
data
uint8_t data[0]
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 co...
Definition: utils.c:710
osmo_hexdump_c
char * osmo_hexdump_c(const void *ctx, const unsigned char *buf, int len)
Convert binary sequence to hexadecimal ASCII string.
Definition: utils.c:411
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.
Definition: utils.c:380
value_string::str
const char * str
human-readable string
Definition: utils.h:44
value_string::value
int value
numeric value
Definition: utils.h:43
osmo_strbuf::pos
char * pos
Current writing position in buf (end of the string written so far).
Definition: utils.h:181
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.
Definition: utils.c:1055
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).
Definition: utils.c:1343
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.
Definition: utils.c:1397
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.
Definition: utils.c:152
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.
Definition: utils.c:303
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 preci...
Definition: utils.c:1218
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 con...
Definition: utils.c:913
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().
Definition: utils.c:727
osmo_identifier_valid
bool osmo_identifier_valid(const char *str)
Determine if a given identifier is valid, i.e.
Definition: utils.c:675
osmo_bcd2char
char osmo_bcd2char(uint8_t bcd)
Convert BCD-encoded digit into printable character.
Definition: utils.c:114
talloc.h
osmo_hexparse
int osmo_hexparse(const char *str, uint8_t *b, int max_len)
Parse a string containing hexadecimal digits.
Definition: utils.c:244
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.
Definition: utils.c:1416
osmo_isqrt32
uint32_t osmo_isqrt32(uint32_t x)
perform an integer square root operation on unsigned 32bit integer.
Definition: utils.c:1008
osmo_str_toupper
const char * osmo_str_toupper(const char *src)
Convert a string to uppercase, using a static buffer.
Definition: utils.c:1139
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.
Definition: utils.c:549
osmo_strbuf::buf
char * buf
Point to the start of a string buffer.
Definition: utils.h:177
value_string
A mapping between human-readable string and numeric value.
Definition: utils.h:42
osmo_str_tolower
const char * osmo_str_tolower(const char *src)
Convert a string to lowercase, using a static buffer.
Definition: utils.c:1081
osmo_luhn
char osmo_luhn(const char *in, int in_len)
Calculate the Luhn checksum (as used for IMEIs).
Definition: utils.c:1166
osmo_str_startswith
bool osmo_str_startswith(const char *str, const char *startswith_str)
Compare start of a string.
Definition: utils.c:1197
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.
Definition: utils.c:835
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.
Definition: utils.c:940
osmo_strbuf
State for OSMO_STRBUF_APPEND() and OSMO_STRBUF_PRINTF().
Definition: utils.h:175
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 buff...
Definition: utils.c:998
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.
Definition: utils.c:589
panic.h
defs.h
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.
Definition: utils.c:447
osmo_strbuf::len
size_t len
Total sizeof() the buffer buf points at.
Definition: utils.h:179
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.
Definition: utils.c:1113
osmo_osmo_hexdump_nospc
char * osmo_osmo_hexdump_nospc(const unsigned char *buf, int len) __attribute__((__deprecated__))
osmo_hexdump_nospc
char * osmo_hexdump_nospc(const unsigned char *buf, int len)
Convert binary sequence to hexadecimal ASCII string.
Definition: utils.c:432
osmo_hexdump
char * osmo_hexdump(const unsigned char *buf, int len)
Convert binary sequence to hexadecimal ASCII string.
Definition: utils.c:396
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 o...
Definition: utils.c:1151
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.
Definition: utils.c:195
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.
Definition: utils.c:857
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.
Definition: utils.c:506
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.
Definition: utils.c:1475
osmo_str2upper
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:486