blob: 13228608cce4f1e069a3ed9c3aa2e3bdcea877fc [file] [log] [blame]
Piotr Krysik9e2e8352018-02-27 12:16:25 +01001#pragma once
2
3#include <stdbool.h>
4
5//#include <osmocom/core/backtrace.h>
6//#include <osmocom/core/talloc.h>
7
8/*! \defgroup utils General-purpose utility functions
9 * @{
10 * \file utils.h */
11
12/*! Determine number of elements in an array of static size */
13#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
14/*! Return the maximum of two specified values */
15#define OSMO_MAX(a, b) ((a) >= (b) ? (a) : (b))
16/*! Return the minimum of two specified values */
17#define OSMO_MIN(a, b) ((a) >= (b) ? (b) : (a))
18/*! Stringify the name of a macro x, e.g. an FSM event name.
19 * Note: if nested within another preprocessor macro, this will
20 * stringify the value of x instead of its name. */
21#define OSMO_STRINGIFY(x) #x
22/*! Stringify the value of a macro x, e.g. a port number. */
23#define OSMO_STRINGIFY_VAL(x) OSMO_STRINGIFY(x)
24/*! Make a value_string entry from an enum value name */
25#define OSMO_VALUE_STRING(x) { x, #x }
26/*! Number of bytes necessary to store given BITS */
27#define OSMO_BYTES_FOR_BITS(BITS) ((BITS + 8 - 1) / 8)
28
29#include <stdbool.h>
30#include <stdint.h>
31#include <stdio.h>
32
Piotr Krysik9e2e8352018-02-27 12:16:25 +010033/*! A mapping between human-readable string and numeric value */
34struct value_string {
35 unsigned int value; /*!< numeric value */
36 const char *str; /*!< human-readable string */
37};
38
39//const char *get_value_string(const struct value_string *vs, uint32_t val);
40const char *get_value_string_or_null(const struct value_string *vs,
41 uint32_t val);
42
43int get_string_value(const struct value_string *vs, const char *str);
44
45char osmo_bcd2char(uint8_t bcd);
46/* only works for numbers in ascci */
47uint8_t osmo_char2bcd(char c);
48
49int osmo_hexparse(const char *str, uint8_t *b, int max_len);
50
51char *osmo_ubit_dump(const uint8_t *bits, unsigned int len);
52char *osmo_hexdump(const unsigned char *buf, int len);
53char *osmo_hexdump_nospc(const unsigned char *buf, int len);
54char *osmo_osmo_hexdump_nospc(const unsigned char *buf, int len) __attribute__((__deprecated__));
55
56#define osmo_static_assert(exp, name) typedef int dummy##name [(exp) ? 1 : -1] __attribute__((__unused__));
57
58void osmo_str2lower(char *out, const char *in);
59void osmo_str2upper(char *out, const char *in);
60
61#define OSMO_SNPRINTF_RET(ret, rem, offset, len) \
62do { \
63 len += ret; \
64 if (ret > rem) \
65 ret = rem; \
66 offset += ret; \
67 rem -= ret; \
68} while (0)
69
70/*! Helper macro to terminate when an assertion failes
71 * \param[in] exp Predicate to verify
72 * This function will generate a backtrace and terminate the program if
73 * the predicate evaluates to false (0).
74 */
75#define OSMO_ASSERT(exp) \
76 if (!(exp)) { \
77 fprintf(stderr, "Assert failed %s %s:%d\n", #exp, __BASE_FILE__, __LINE__); \
78 /*osmo_generate_backtrace(); \ */\
79 abort(); \
80 }
81
82/*! duplicate a string using talloc and release its prior content (if any)
83 * \param[in] ctx Talloc context to use for allocation
84 * \param[out] dst pointer to string, will be updated with ptr to new string
85 * \param[in] newstr String that will be copieed to newly allocated string */
86/*static inline void osmo_talloc_replace_string(void *ctx, char **dst, const char *newstr)*/
87/*{*/
88/* if (*dst)*/
89/* talloc_free(*dst);*/
90/* *dst = talloc_strdup(ctx, newstr);*/
91/*}*/
92
93/*! Append to a string and re-/allocate if necessary.
94 * \param[in] ctx Talloc context to use for initial allocation.
95 * \param[in,out] dest char* to re-/allocate and append to.
96 * \param[in] fmt printf-like string format.
97 * \param[in] args Arguments for fmt.
98 *
99 * \a dest may be passed in NULL, or a string previously allocated by talloc.
100 * If an existing string is passed in, it will remain associated with whichever
101 * ctx it was allocated before, regardless whether it matches \a ctx or not.
102 */
103/*#define osmo_talloc_asprintf(ctx, dest, fmt, args ...) \*/
104/* do { \*/
105/* if (!dest) \*/
106/* dest = talloc_asprintf(ctx, fmt, ## args); \*/
107/* else \*/
108/* dest = talloc_asprintf_append((char*)dest, fmt, ## args); \*/
109/* } while (0)*/
110
111int osmo_constant_time_cmp(const uint8_t *exp, const uint8_t *rel, const int count);
112uint64_t osmo_decode_big_endian(const uint8_t *data, size_t data_len);
113uint8_t *osmo_encode_big_endian(uint64_t value, size_t data_len);
114
115size_t osmo_strlcpy(char *dst, const char *src, size_t siz);
116
117bool osmo_is_hexstr(const char *str, int min_digits, int max_digits,
118 bool require_even);
119
120bool osmo_identifier_valid(const char *str);
121bool osmo_separated_identifiers_valid(const char *str, const char *sep_chars);
122
123const char *osmo_escape_str(const char *str, int len);
124const char *osmo_escape_str_buf(const char *str, int in_len, char *buf, size_t bufsize);
125
126/*! @} */