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