libosmocore  0.10.2
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 
13 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
14 
15 #define OSMO_MAX(a, b) ((a) >= (b) ? (a) : (b))
16 
17 #define OSMO_MIN(a, b) ((a) >= (b) ? (b) : (a))
18 
19 #define OSMO_STRINGIFY(x) #x
20 
21 #define OSMO_VALUE_STRING(x) { x, #x }
22 
23 #define OSMO_BYTES_FOR_BITS(BITS) ((BITS + 8 - 1) / 8)
24 
25 #include <stdbool.h>
26 #include <stdint.h>
27 #include <stdio.h>
28 
30 struct value_string {
31  unsigned int value;
32  const char *str;
33 };
34 
35 const char *get_value_string(const struct value_string *vs, uint32_t val);
36 const char *get_value_string_or_null(const struct value_string *vs,
37  uint32_t val);
38 
39 int get_string_value(const struct value_string *vs, const char *str);
40 
41 char osmo_bcd2char(uint8_t bcd);
42 /* only works for numbers in ascci */
43 uint8_t osmo_char2bcd(char c);
44 
45 int osmo_hexparse(const char *str, uint8_t *b, int max_len);
46 
47 char *osmo_ubit_dump(const uint8_t *bits, unsigned int len);
48 char *osmo_hexdump(const unsigned char *buf, int len);
49 char *osmo_hexdump_nospc(const unsigned char *buf, int len);
50 char *osmo_osmo_hexdump_nospc(const unsigned char *buf, int len) __attribute__((__deprecated__));
51 
52 #define osmo_static_assert(exp, name) typedef int dummy##name [(exp) ? 1 : -1] __attribute__((__unused__));
53 
54 void osmo_str2lower(char *out, const char *in);
55 void osmo_str2upper(char *out, const char *in);
56 
57 #define OSMO_SNPRINTF_RET(ret, rem, offset, len) \
58 do { \
59  len += ret; \
60  if (ret > rem) \
61  ret = rem; \
62  offset += ret; \
63  rem -= ret; \
64 } while (0)
65 
71 #define OSMO_ASSERT(exp) \
72  if (!(exp)) { \
73  fprintf(stderr, "Assert failed %s %s:%d\n", #exp, __BASE_FILE__, __LINE__); \
74  osmo_generate_backtrace(); \
75  abort(); \
76  }
77 
82 static inline void osmo_talloc_replace_string(void *ctx, char **dst, const char *newstr)
83 {
84  if (*dst)
85  talloc_free(*dst);
86  *dst = talloc_strdup(ctx, newstr);
87 }
88 
99 #define osmo_talloc_asprintf(ctx, dest, fmt, args ...) \
100  do { \
101  if (!dest) \
102  dest = talloc_asprintf(ctx, fmt, ## args); \
103  else \
104  dest = talloc_asprintf_append((char*)dest, fmt, ## args); \
105  } while (0)
106 
107 int osmo_constant_time_cmp(const uint8_t *exp, const uint8_t *rel, const int count);
108 uint64_t osmo_decode_big_endian(const uint8_t *data, size_t data_len);
109 uint8_t *osmo_encode_big_endian(uint64_t value, size_t data_len);
110 
111 size_t osmo_strlcpy(char *dst, const char *src, size_t siz);
112 
113 bool osmo_is_hexstr(const char *str, int min_digits, int max_digits,
114  bool require_even);
115 
116 bool osmo_identifier_valid(const char *str);
117 
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:403
void osmo_str2lower(char *out, const char *in)
Convert an entire string to lower case.
Definition: utils.c:286
int get_string_value(const struct value_string *vs, const char *str)
get numeric value for given human-readable string
Definition: utils.c:89
const char * get_value_string(const struct value_string *vs, uint32_t val)
get human-readable string for given value
Definition: utils.c:53
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:69
struct rb_root __attribute__
Definition: conv_acc_generic.c:138
unsigned int value
numeric value
Definition: utils.h:31
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:341
uint8_t osmo_char2bcd(char c)
Convert number in ASCII to BCD value.
Definition: utils.c:118
A mapping between human-readable string and numeric value.
Definition: utils.h:30
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:82
size_t osmo_strlcpy(char *dst, const char *src, size_t siz)
Copy a C-string into a sized buffer.
Definition: utils.c:380
Convenience wrapper.
const char * str
human-readable string
Definition: utils.h:32
char * osmo_ubit_dump(const uint8_t *bits, unsigned int len)
Convert a sequence of unpacked bits to ASCII string.
Definition: utils.c:209
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:362
bool osmo_identifier_valid(const char *str)
Determine if a given identifier is valid, i.e.
Definition: utils.c:430
char osmo_bcd2char(uint8_t bcd)
Convert BCD-encoded digit into printable character.
Definition: utils.c:106
int osmo_hexparse(const char *str, uint8_t *b, int max_len)
Parse a string containing hexadecimal digits.
Definition: utils.c:136
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:266
char * osmo_hexdump(const unsigned char *buf, int len)
Convert binary sequence to hexadecimal ASCII string.
Definition: utils.c:250
void osmo_str2upper(char *out, const char *in)
Convert an entire string to upper case.
Definition: utils.c:299
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:319