blob: 1e5fab70c69c1b16e6ef94f3ec7c3d13d38b788a [file] [log] [blame]
Harald Welteec8b4502010-02-20 20:34:29 +01001#ifndef OSMOCORE_UTIL_H
2#define OSMOCORE_UTIL_H
3
Harald Weltebd598e32011-08-16 23:26:52 +02004/*! \file utils.h
5 * \brief General-purpose utilities in the Osmocom core library
6 */
7
8/*! \brief Determine number of elements in an array of static size */
Harald Welteec8b4502010-02-20 20:34:29 +01009#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
Harald Weltebd598e32011-08-16 23:26:52 +020010/*! \brief Return the maximum of two specified values */
Harald Weltee9b744e2011-06-26 14:19:54 +020011#define OSMO_MAX(a, b) (a) >= (b) ? (a) : (b)
Harald Weltebd598e32011-08-16 23:26:52 +020012/*! \brief Return the minimum of two specified values */
Harald Welte430be842011-07-27 22:23:11 +020013#define OSMO_MIN(a, b) (a) >= (b) ? (b) : (a)
Harald Welteec8b4502010-02-20 20:34:29 +010014
Harald Welted284cd92010-03-01 21:58:31 +010015#include <stdint.h>
16
Harald Weltebd598e32011-08-16 23:26:52 +020017/*! \brief A mapping between human-readable string and numeric value */
Harald Welted284cd92010-03-01 21:58:31 +010018struct value_string {
Harald Weltebd598e32011-08-16 23:26:52 +020019 unsigned int value; /*!< \brief numeric value */
20 const char *str; /*!< \brief human-readable string */
Harald Welted284cd92010-03-01 21:58:31 +010021};
22
Harald Weltebd598e32011-08-16 23:26:52 +020023/*! \fn get_value_string(const struct value_string *vs, uint32_t val)
24 * \brief get human-readable string for given value
25 * \param[in] vs Array of value_string tuples
26 * \param[in] val Value to be converted
27 * \returns pointer to human-readable string
28 */
Harald Welted284cd92010-03-01 21:58:31 +010029const char *get_value_string(const struct value_string *vs, uint32_t val);
Harald Weltebd598e32011-08-16 23:26:52 +020030
31/*! \fn get_string_value(const struct value_string *vs, const char *str)
32 * \brief get numeric value for given human-readable string
33 * \param[in] vs Array of value_string tuples
34 * \param[in] str human-readable string
35 * \returns numeric value (>0) or negative numer in case of error
36 */
Harald Welted284cd92010-03-01 21:58:31 +010037int get_string_value(const struct value_string *vs, const char *str);
38
Harald Weltebd598e32011-08-16 23:26:52 +020039/*! \fn osmo_bcd2char(uint8_t bcd)
40 * \brief Convert BCD-encoded digit into printable character
41 * \param[in] bcd A single BCD-encoded digit
42 * \returns single printable character
43 */
Pablo Neira Ayuso87f7b252011-05-07 12:43:08 +020044char osmo_bcd2char(uint8_t bcd);
Harald Weltea73e2f92010-03-04 10:50:32 +010045/* only works for numbers in ascci */
Pablo Neira Ayuso87f7b252011-05-07 12:43:08 +020046uint8_t osmo_char2bcd(char c);
Harald Welted284cd92010-03-01 21:58:31 +010047
Pablo Neira Ayuso87f7b252011-05-07 12:43:08 +020048int osmo_hexparse(const char *str, uint8_t *b, int max_len);
Harald Weltebd598e32011-08-16 23:26:52 +020049
50/*! \fn osmo_hexdump(const unsigned char *buf, int len)
51 * \brief Convert binary sequence to hexadecimal ASCII string
52 * This function will print a sequence of bytes as hexadecimal numbers,
53 * adding one space character between each byte (e.g. "1a ef d9")
54 * \param[in] buf pointer to sequence of bytes
55 * \param[in] len length of buf in number of bytes
56 * \returns pointer to zero-terminated string
57 */
Pablo Neira Ayuso87f7b252011-05-07 12:43:08 +020058char *osmo_hexdump(const unsigned char *buf, int len);
Harald Weltebd598e32011-08-16 23:26:52 +020059
60/*! \fn osmo_hexdump_nospc(const unsigned char *buf, int len)
61 * \brief Convert binary sequence to hexadecimal ASCII string
62 * This function will print a sequence of bytes as hexadecimal numbers,
63 * without any space character between each byte (e.g. "1aefd9")
64 * \param[in] buf pointer to sequence of bytes
65 * \param[in] len length of buf in number of bytes
66 * \returns pointer to zero-terminated string
67 */
Pablo Neira Ayuso87f7b252011-05-07 12:43:08 +020068char *osmo_osmo_hexdump_nospc(const unsigned char *buf, int len);
Harald Weltebd598e32011-08-16 23:26:52 +020069
70/*! \fn osmo_ubit_dump(const uint8_t *bits, unsigned int len)
71 * \brief Convert a sequence of unpacked bits to ASCII string
72 * \param[in] bits A sequence of unpacked bits
73 * \param[in] len Length of bits
74 */
Pablo Neira Ayuso87f7b252011-05-07 12:43:08 +020075char *osmo_ubit_dump(const uint8_t *bits, unsigned int len);
Harald Welte3eba9912010-07-30 10:37:29 +020076
Pablo Neira Ayuso87f7b252011-05-07 12:43:08 +020077#define osmo_static_assert(exp, name) typedef int dummy##name [(exp) ? 1 : -1];
Holger Hans Peter Freyther52c07ca2011-01-16 17:37:27 +010078
Harald Weltebd598e32011-08-16 23:26:52 +020079/*! \fn osmo_str2lower(char *out, const char *in)
80 * \brief Convert an entire string to lower case
81 * \param[out] out output string, caller-allocated
82 * \param[in] in input string
83 */
Harald Welte28222962011-02-18 20:37:04 +010084void osmo_str2lower(char *out, const char *in);
Harald Weltebd598e32011-08-16 23:26:52 +020085
86/*! \fn osmo_str2upper(char *out, const char *in)
87 * \brief Convert an entire string to upper case
88 * \param[out] out output string, caller-allocated
89 * \param[in] in input string
90 */
Harald Welte28222962011-02-18 20:37:04 +010091void osmo_str2upper(char *out, const char *in);
92
Pablo Neira Ayuso3abad6a2011-03-28 19:24:22 +020093#define OSMO_SNPRINTF_RET(ret, rem, offset, len) \
94do { \
95 len += ret; \
96 if (ret > rem) \
97 ret = rem; \
98 offset += ret; \
99 rem -= ret; \
100} while (0)
101
Harald Welteec8b4502010-02-20 20:34:29 +0100102#endif