libosmocore  1.0.1.85-9f28
Osmocom core library
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
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 
8 #include <osmocom/core/talloc.h>
9 #include <osmocom/core/panic.h>
10 #include <osmocom/core/defs.h>
11 
17 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
18 
19 #define OSMO_MAX(a, b) ((a) >= (b) ? (a) : (b))
20 
21 #define OSMO_MIN(a, b) ((a) >= (b) ? (b) : (a))
22 
25 #define OSMO_STRINGIFY(x) #x
26 
27 #define OSMO_STRINGIFY_VAL(x) OSMO_STRINGIFY(x)
28 
29 #define OSMO_VALUE_STRING(x) { x, #x }
30 
31 #define OSMO_BYTES_FOR_BITS(BITS) ((BITS + 8 - 1) / 8)
32 
34 #define OSMO_STRLCPY_ARRAY(array, src) osmo_strlcpy(array, src, sizeof(array))
35 
37 struct value_string {
38  unsigned int value;
39  const char *str;
40 };
41 
42 const char *get_value_string(const struct value_string *vs, uint32_t val);
43 const char *get_value_string_or_null(const struct value_string *vs,
44  uint32_t val);
45 
46 int get_string_value(const struct value_string *vs, const char *str);
47 
48 char osmo_bcd2char(uint8_t bcd);
49 /* only works for numbers in ascci */
50 uint8_t osmo_char2bcd(char c);
51 
52 int osmo_bcd2str(char *dst, size_t dst_size, const uint8_t *bcd, int start_nibble, int end_nibble, bool allow_hex);
53 
54 int osmo_hexparse(const char *str, uint8_t *b, int max_len);
55 
56 char *osmo_ubit_dump(const uint8_t *bits, unsigned int len);
57 char *osmo_hexdump(const unsigned char *buf, int len);
58 char *osmo_hexdump_nospc(const unsigned char *buf, int len);
59 const char *osmo_hexdump_buf(char *out_buf, size_t out_buf_size, const unsigned char *buf, int len, const char *delim,
60  bool delim_after_last);
61 
62 char *osmo_osmo_hexdump_nospc(const unsigned char *buf, int len) __attribute__((__deprecated__));
63 
64 #define osmo_static_assert(exp, name) typedef int dummy##name [(exp) ? 1 : -1] __attribute__((__unused__));
65 
66 void osmo_str2lower(char *out, const char *in)
67  OSMO_DEPRECATED("Use osmo_str_tolower() or osmo_str_tolower_buf() instead,"
68  " to properly check target memory bounds");
69 void osmo_str2upper(char *out, const char *in)
70  OSMO_DEPRECATED("Use osmo_str_toupper() or osmo_str_toupper_buf() instead,"
71  " to properly check target memory bounds");
72 
73 size_t osmo_str_tolower_buf(char *dest, size_t dest_len, const char *src);
74 const char *osmo_str_tolower(const char *src);
75 
76 size_t osmo_str_toupper_buf(char *dest, size_t dest_len, const char *src);
77 const char *osmo_str_toupper(const char *src);
78 
79 #define OSMO_SNPRINTF_RET(ret, rem, offset, len) \
80 do { \
81  len += ret; \
82  if (ret > rem) \
83  ret = rem; \
84  offset += ret; \
85  rem -= ret; \
86 } while (0)
87 
93 #define OSMO_ASSERT(exp) \
94  if (!(exp)) { \
95  osmo_panic("Assert failed %s %s:%d\n", #exp, __FILE__, __LINE__); \
96  }
97 
102 static inline void osmo_talloc_replace_string(void *ctx, char **dst, const char *newstr)
103 {
104  if (*dst)
105  talloc_free(*dst);
106  *dst = talloc_strdup(ctx, newstr);
107 }
108 
119 #define osmo_talloc_asprintf(ctx, dest, fmt, args ...) \
120  do { \
121  if (!dest) \
122  dest = talloc_asprintf(ctx, fmt, ## args); \
123  else \
124  dest = talloc_asprintf_append((char*)dest, fmt, ## args); \
125  } while (0)
126 
127 int osmo_constant_time_cmp(const uint8_t *exp, const uint8_t *rel, const int count);
128 uint64_t osmo_decode_big_endian(const uint8_t *data, size_t data_len);
129 uint8_t *osmo_encode_big_endian(uint64_t value, size_t data_len);
130 
131 size_t osmo_strlcpy(char *dst, const char *src, size_t siz);
132 
133 bool osmo_is_hexstr(const char *str, int min_digits, int max_digits,
134  bool require_even);
135 
136 bool osmo_identifier_valid(const char *str);
137 bool osmo_separated_identifiers_valid(const char *str, const char *sep_chars);
138 
139 const char *osmo_escape_str(const char *str, int len);
140 const char *osmo_escape_str_buf(const char *str, int in_len, char *buf, size_t bufsize);
141 const char *osmo_quote_str(const char *str, int in_len);
142 const char *osmo_quote_str_buf(const char *str, int in_len, char *buf, size_t bufsize);
143 
144 uint32_t osmo_isqrt32(uint32_t x);
145 
146 const char osmo_luhn(const char* in, int in_len);
147 
149 struct osmo_strbuf {
151  char *buf;
153  size_t len;
155  char *pos;
159  size_t chars_needed;
160 };
161 
198 #define OSMO_STRBUF_APPEND(STRBUF, func, args...) do { \
199  if (!(STRBUF).pos) \
200  (STRBUF).pos = (STRBUF).buf; \
201  size_t remain = (STRBUF).buf ? (STRBUF).len - ((STRBUF).pos - (STRBUF).buf) : 0; \
202  int l = func((STRBUF).pos, remain, ##args); \
203  if (l < 0 || l > remain) \
204  (STRBUF).pos = (STRBUF).buf + (STRBUF).len; \
205  else \
206  (STRBUF).pos += l; \
207  if (l > 0) \
208  (STRBUF).chars_needed += l; \
209  } while(0)
210 
230 #define OSMO_STRBUF_PRINTF(STRBUF, fmt, args...) \
231  OSMO_STRBUF_APPEND(STRBUF, snprintf, fmt, ##args)
232 
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:477
#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:155
int get_string_value(const struct value_string *vs, const char *str)
get numeric value for given human-readable string
Definition: utils.c:94
const char * get_value_string(const struct value_string *vs, uint32_t val)
get human-readable string for given value
Definition: utils.c:55
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
const char * osmo_escape_str(const char *str, int len)
Return the string with all non-printable characters escaped.
Definition: utils.c:623
unsigned int value
numeric value
Definition: utils.h:38
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:414
uint8_t osmo_char2bcd(char c)
Convert number in ASCII to BCD value.
Definition: utils.c:123
State for OSMO_STRBUF_APPEND() and OSMO_STRBUF_PRINTF().
Definition: utils.h:149
A mapping between human-readable string and numeric value.
Definition: utils.h:37
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:102
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:453
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:667
bool osmo_separated_identifiers_valid(const char *str, const char *sep_chars)
Determine if a given identifier is valid, i.e.
Definition: utils.c:505
Convenience wrapper.
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.
Definition: utils.c:722
const char * str
human-readable string
Definition: utils.h:39
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:241
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:634
char * osmo_ubit_dump(const uint8_t *bits, unsigned int len)
Convert a sequence of unpacked bits to ASCII string.
Definition: utils.c:280
uint8_t data[0]
size_t chars_needed
After all OSMO_STRBUF_APPEND operations, reflects the total number of characters that would be writte...
Definition: utils.h:159
const char * osmo_str_toupper(const char *src)
Convert a string to uppercase, using a static buffer.
Definition: utils.c:791
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:548
void osmo_str2lower(char *out, const char *in) OSMO_DEPRECATED("Use osmo_str_tolower() or osmo_str_tolower_buf() instead
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:149
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:435
bool osmo_identifier_valid(const char *str)
Determine if a given identifier is valid, i.e.
Definition: utils.c:536
char osmo_bcd2char(uint8_t bcd)
Convert BCD-encoded digit into printable character.
Definition: utils.c:111
int osmo_hexparse(const char *str, uint8_t *b, int max_len)
Parse a string containing hexadecimal digits.
Definition: utils.c:182
char * buf
Point to the start of a string buffer.
Definition: utils.h:151
const char * osmo_str_tolower(const char *src)
Convert a string to lowercase, using a static buffer.
Definition: utils.c:748
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:765
uint32_t osmo_isqrt32(uint32_t x)
perform an integer square root operation on unsigned 32bit integer.
Definition: utils.c:675
static size_t len(const char *str)
const char osmo_luhn(const char *in, int in_len)
Calculate the Luhn checksum (as used for IMEIs).
Definition: utils.c:803
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:338
size_t len
Total sizeof() the buffer buf points at.
Definition: utils.h:153
char * osmo_hexdump(const unsigned char *buf, int len)
Convert binary sequence to hexadecimal ASCII string.
Definition: utils.c:321
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:392