libosmocore  0.12.0
Osmocom core library
utils.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <stdbool.h>
4 
6 #include <osmocom/core/talloc.h>
7 #include <osmocom/core/panic.h>
8 
14 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
15 
16 #define OSMO_MAX(a, b) ((a) >= (b) ? (a) : (b))
17 
18 #define OSMO_MIN(a, b) ((a) >= (b) ? (b) : (a))
19 
22 #define OSMO_STRINGIFY(x) #x
23 
24 #define OSMO_STRINGIFY_VAL(x) OSMO_STRINGIFY(x)
25 
26 #define OSMO_VALUE_STRING(x) { x, #x }
27 
28 #define OSMO_BYTES_FOR_BITS(BITS) ((BITS + 8 - 1) / 8)
29 
31 #define OSMO_STRLCPY_ARRAY(array, src) osmo_strlcpy(array, src, sizeof(array))
32 
33 #include <stdbool.h>
34 #include <stdint.h>
35 #include <stdio.h>
36 
38 struct value_string {
39  unsigned int value;
40  const char *str;
41 };
42 
43 const char *get_value_string(const struct value_string *vs, uint32_t val);
44 const char *get_value_string_or_null(const struct value_string *vs,
45  uint32_t val);
46 
47 int get_string_value(const struct value_string *vs, const char *str);
48 
49 char osmo_bcd2char(uint8_t bcd);
50 /* only works for numbers in ascci */
51 uint8_t osmo_char2bcd(char c);
52 
53 int osmo_hexparse(const char *str, uint8_t *b, int max_len);
54 
55 char *osmo_ubit_dump(const uint8_t *bits, unsigned int len);
56 char *osmo_hexdump(const unsigned char *buf, int len);
57 char *osmo_hexdump_nospc(const unsigned char *buf, int len);
58 char *osmo_osmo_hexdump_nospc(const unsigned char *buf, int len) __attribute__((__deprecated__));
59 
60 #define osmo_static_assert(exp, name) typedef int dummy##name [(exp) ? 1 : -1] __attribute__((__unused__));
61 
62 void osmo_str2lower(char *out, const char *in);
63 void osmo_str2upper(char *out, const char *in);
64 
65 #define OSMO_SNPRINTF_RET(ret, rem, offset, len) \
66 do { \
67  len += ret; \
68  if (ret > rem) \
69  ret = rem; \
70  offset += ret; \
71  rem -= ret; \
72 } while (0)
73 
79 #define OSMO_ASSERT(exp) \
80  if (!(exp)) { \
81  osmo_panic("Assert failed %s %s:%d\n", #exp, __BASE_FILE__, __LINE__); \
82  }
83 
88 static inline void osmo_talloc_replace_string(void *ctx, char **dst, const char *newstr)
89 {
90  if (*dst)
91  talloc_free(*dst);
92  *dst = talloc_strdup(ctx, newstr);
93 }
94 
105 #define osmo_talloc_asprintf(ctx, dest, fmt, args ...) \
106  do { \
107  if (!dest) \
108  dest = talloc_asprintf(ctx, fmt, ## args); \
109  else \
110  dest = talloc_asprintf_append((char*)dest, fmt, ## args); \
111  } while (0)
112 
113 int osmo_constant_time_cmp(const uint8_t *exp, const uint8_t *rel, const int count);
114 uint64_t osmo_decode_big_endian(const uint8_t *data, size_t data_len);
115 uint8_t *osmo_encode_big_endian(uint64_t value, size_t data_len);
116 
117 size_t osmo_strlcpy(char *dst, const char *src, size_t siz);
118 
119 bool osmo_is_hexstr(const char *str, int min_digits, int max_digits,
120  bool require_even);
121 
122 bool osmo_identifier_valid(const char *str);
123 bool osmo_separated_identifiers_valid(const char *str, const char *sep_chars);
124 
125 const char *osmo_escape_str(const char *str, int len);
126 const char *osmo_escape_str_buf(const char *str, int in_len, char *buf, size_t bufsize);
127 const char *osmo_quote_str(const char *str, int in_len);
128 const char *osmo_quote_str_buf(const char *str, int in_len, char *buf, size_t bufsize);
129 
130 uint32_t osmo_isqrt32(uint32_t x);
131 
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:406
osmo_char2bcd
uint8_t osmo_char2bcd(char c)
Convert number in ASCII to BCD value.
Definition: utils.c:120
osmo_str2lower
void osmo_str2lower(char *out, const char *in)
Convert an entire string to lower case.
Definition: utils.c:288
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:55
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:552
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:343
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:434
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:91
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:88
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:71
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:382
osmo_quote_str
const char * osmo_quote_str(const char *str, int in_len)
Definition: utils.c:590
value_string::value
unsigned int value
numeric value
Definition: utils.h:39
osmo_escape_str_buf
const char * osmo_escape_str_buf(const char *str, int in_len, char *buf, size_t bufsize)
Return the string with all non-printable characters escaped.
Definition: utils.c:477
osmo_ubit_dump
char * osmo_ubit_dump(const uint8_t *bits, unsigned int len)
Convert a sequence of unpacked bits to ASCII string.
Definition: utils.c:211
value_string::str
const char * str
human-readable string
Definition: utils.h:40
__attribute__
struct rb_root __attribute__
Definition: conv_acc_generic.c:140
osmo_quote_str_buf
const char * osmo_quote_str_buf(const char *str, int in_len, char *buf, size_t bufsize)
Like osmo_escape_str(), but returns double-quotes around a string, or "NULL" for a NULL string.
Definition: utils.c:563
osmo_identifier_valid
bool osmo_identifier_valid(const char *str)
Determine if a given identifier is valid, i.e.
Definition: utils.c:465
osmo_bcd2char
char osmo_bcd2char(uint8_t bcd)
Convert BCD-encoded digit into printable character.
Definition: utils.c:108
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:138
osmo_isqrt32
uint32_t osmo_isqrt32(uint32_t x)
perform an integer square root operation on unsigned 32bit integer.
Definition: utils.c:598
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:364
value_string
A mapping between human-readable string and numeric value.
Definition: utils.h:38
panic.h
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:268
osmo_hexdump
char * osmo_hexdump(const unsigned char *buf, int len)
Convert binary sequence to hexadecimal ASCII string.
Definition: utils.c:252
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:321
osmo_str2upper
void osmo_str2upper(char *out, const char *in)
Convert an entire string to upper case.
Definition: utils.c:301