30 #include <sys/types.h>
54 #define sol_util_array_size(arr) (sizeof(arr) / sizeof((arr)[0]))
59 #define SOL_UTIL_NSEC_PER_SEC 1000000000ULL
64 #define SOL_UTIL_MSEC_PER_SEC 1000ULL
69 #define SOL_UTIL_USEC_PER_SEC 1000000ULL
74 #define SOL_UTIL_NSEC_PER_MSEC 1000000ULL
79 #define SOL_UTIL_NSEC_PER_USEC 1000ULL
89 #define sol_util_min(x, y) \
91 __typeof__(x)_min1 = (x); \
92 __typeof__(y)_min2 = (y); \
93 (void)(&_min1 == &_min2); \
94 _min1 < _min2 ? _min1 : _min2; \
105 #define sol_util_max(x, y) \
107 __typeof__(x)_max1 = (x); \
108 __typeof__(y)_max2 = (y); \
109 (void)(&_max1 == &_max2); \
110 _max1 > _max2 ? _max1 : _max2; \
140 result->tv_nsec = t1->tv_nsec + t2->tv_nsec;
141 result->tv_sec = t1->tv_sec + t2->tv_sec;
158 result->tv_nsec = t1->tv_nsec - t2->tv_nsec;
159 result->tv_sec = t1->tv_sec - t2->tv_sec;
160 if (result->tv_nsec < 0) {
180 int retval = (t1->tv_sec > t2->tv_sec) - (t1->tv_sec < t2->tv_sec);
184 return (t1->tv_nsec > t2->tv_nsec) - (t1->tv_nsec < t2->tv_nsec);
194 static inline struct timespec
211 static inline struct timespec
275 #define sol_util_strerrora(errnum) \
277 SOL_BUFFER_DECLARE_STATIC(buf ## __COUNT__, 512); \
278 sol_util_strerror((errnum), &buf ## __COUNT__); \
490 static inline int32_t
586 static inline ssize_t
589 ssize_t req_len = slice.
len / 3;
592 if (slice.
len % 3 != 0)
690 static inline ssize_t
709 static inline ssize_t
712 return slice.
len / 2;
731 __asm__ __volatile__ (
"" : :
"g" (buf) :
"memory");
763 double sol_util_strtod_n(
const char *nptr,
char **endptr, ssize_t len,
bool use_locale);
789 long int sol_util_strtol_n(
const char *nptr,
char **endptr, ssize_t len,
int base);
815 unsigned long int sol_util_strtoul_n(
const char *nptr,
char **endptr, ssize_t len,
int base);
820 #define sol_util_uint16_bytes_swap(val) \
821 ((uint16_t)((((val) >> 8) & 0xff) | (((val) & 0xff) << 8)))
835 static inline uint16_t
838 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
857 static inline uint16_t
860 #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
879 static inline uint16_t
882 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
901 static inline uint16_t
904 #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
914 #define sol_util_uint32_bytes_swap(val) \
915 ((uint32_t)((((val) & 0xff000000) >> 24) | (((val) & 0x00ff0000) >> 8) | \
916 (((val) & 0x0000ff00) << 8) | (((val) & 0x000000ff) << 24)))
930 static inline uint32_t
933 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
952 static inline uint32_t
955 #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
974 static inline uint32_t
977 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
996 static inline uint32_t
999 #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
1009 #define sol_util_uint64_bytes_swap(val) \
1010 ((uint64_t)((((val) & 0xff00000000000000ull) >> 56) \
1011 | (((val) & 0x00ff000000000000ull) >> 40) | (((val) & 0x0000ff0000000000ull) >> 24) \
1012 | (((val) & 0x000000ff00000000ull) >> 8) | (((val) & 0x00000000ff000000ull) << 8) \
1013 | (((val) & 0x0000000000ff0000ull) << 24) | (((val) & 0x000000000000ff00ull) << 40) \
1014 | (((val) & 0x00000000000000ffull) << 56)))
1028 static inline uint64_t
1031 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
1050 static inline uint64_t
1053 #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
1072 static inline uint64_t
1075 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
1094 static inline uint64_t
1097 #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
#define SOL_STATIC_ARRAY_SIZE(n)
Convenience macro to declare the size of a static array that will handle differences between C and C+...
Definition: sol-macros.h:208
static void sol_util_timespec_sub(const struct timespec *t1, const struct timespec *t2, struct timespec *result)
Subtracts two time values.
Definition: sol-util.h:156
int sol_util_int32_mul(int32_t op1, int32_t op2, int32_t *out)
Multiply two values checking for overflow.
#define SOL_UTIL_NSEC_PER_SEC
number of nanoseconds in a second: 1,000,000,000.
Definition: sol-util.h:59
unsigned long int sol_util_strtoul_n(const char *nptr, char **endptr, ssize_t len, int base)
Wrapper over strtoul() that consumes up to len bytes.
static uint64_t sol_util_cpu_to_be64(uint64_t val)
Convert a 64 bytes integer to big endian format.
Definition: sol-util.h:1029
static ssize_t sol_util_base16_calculate_decoded_len(const struct sol_str_slice slice)
Calculate the size necessary to decode a given slice in base16.
Definition: sol-util.h:710
#define sol_util_uint32_bytes_swap(val)
Swaps the bytes of a 32 bytes unsigned int.
Definition: sol-util.h:914
static int32_t sol_util_int32_clamp(int32_t start, int32_t end, int32_t value)
Restricts a number between two other numbers.
Definition: sol-util.h:491
static uint64_t sol_util_cpu_to_le64(uint64_t val)
Convert a 64 bytes integer to little endian format.
Definition: sol-util.h:1051
static uint32_t sol_util_be32_to_cpu(uint32_t val)
Convert a 32 bytes big endian integer to cpu endianness.
Definition: sol-util.h:975
static ssize_t sol_util_base64_calculate_encoded_len(const struct sol_str_slice slice, const char base64_map[SOL_STATIC_ARRAY_SIZE(65)])
Calculate the size necessary to encode a given slice in base64.
Definition: sol-util.h:587
int sol_util_size_mul(size_t op1, size_t op2, size_t *out)
Multiply two values checking for overflow.
static uint16_t sol_util_be16_to_cpu(uint16_t val)
Convert a 16 bytes big endian integer to cpu endianness.
Definition: sol-util.h:880
#define SOL_ATTR_STRFTIME(fmt)
Specifies that a function takes strftime style arguments which should be type-checked against a forma...
Definition: sol-macros.h:191
int sol_util_uuid_bytes_from_string(struct sol_str_slice uuid_str, struct sol_buffer *uuid_bytes)
Convert a UUID in string format to a byte array with UUID bytes.
int32_t sol_util_unicode_code_from_utf8(const uint8_t *buf, size_t buf_len, uint8_t *bytes_read)
Convert a utf-8 character to unicode code.
#define SOL_UTIL_NSEC_PER_USEC
number of nanoseconds in a microsecond: 1,000,000,000 / 1,000,000 = 1,000.
Definition: sol-util.h:79
int sol_util_uuid_gen(bool uppercase, bool with_hyphens, struct sol_buffer *uuid_buf)
Generates a new universally unique identifier (UUID) string.
static struct sol_buffer value
Definition: server.c:42
void * sol_util_memdup(const void *data, size_t len)
Duplicate memory.
ssize_t sol_util_base64_decode(void *buf, size_t buflen, const struct sol_str_slice slice, const char base64_map[SOL_STATIC_ARRAY_SIZE(65)])
Decode the binary slice from base64 using the given map.
int8_t sol_util_utf8_from_unicode_code(uint8_t *buf, size_t buf_len, uint32_t unicode_code)
Convert from unicode code to utf-8 string.
static uint32_t sol_util_cpu_to_be32(uint32_t val)
Convert a 32 bytes integer to big endian format.
Definition: sol-util.h:931
int sol_util_replace_str_from_slice_if_changed(char **str, const struct sol_str_slice slice)
Replace the string's contents.
int sol_util_int64_mul(int64_t op1, int64_t op2, int64_t *out)
Multiply two values checking for overflow.
#define SOL_UTIL_MSEC_PER_SEC
number of milliseconds in a second: 1,000.
Definition: sol-util.h:64
int sol_util_uint64_mul(uint64_t op1, uint64_t op2, uint64_t *out)
Multiply two values checking for overflow.
String slice type.
Definition: sol-str-slice.h:84
These are routines that Soletta provides for its buffer implementation.
ssize_t sol_util_base16_decode(void *buf, size_t buflen, const struct sol_str_slice slice, enum sol_decode_case decode_case)
Decode the binary slice from base16 (hexadecimal).
static uint64_t sol_util_le64_to_cpu(uint64_t val)
Convert a 64 bytes little endian integer to cpu endianness.
Definition: sol-util.h:1095
bool sol_util_uuid_str_is_valid(const struct sol_str_slice uuid)
Checks if a given universally unique identifier (UUID), in string form, is valid. ...
int sol_util_uuid_string_from_bytes(bool uppercase, bool with_hyphens, const uint8_t uuid_bytes[SOL_STATIC_ARRAY_SIZE(16)], struct sol_buffer *uuid_str)
Convert a UUID in byte format to UUID string format.
int sol_util_size_sub(size_t op1, size_t op2, size_t *out)
Subtract two values checking for overflow.
static uint32_t sol_util_le32_to_cpu(uint32_t val)
Convert a 32 bytes little endian integer to cpu endianness.
Definition: sol-util.h:997
static void sol_util_timespec_add(const struct timespec *t1, const struct timespec *t2, struct timespec *result)
Sum two time values.
Definition: sol-util.h:138
static struct timespec sol_util_timespec_from_usec(int usec)
Create a struct timespec from microseconds.
Definition: sol-util.h:212
int sol_util_uint64_add(uint64_t op1, uint64_t op2, uint64_t *out)
Add two values checking for overflow.
bool sol_util_double_eq(double var0, double var1)
Checks var0 and var1 for equality.
static uint16_t sol_util_cpu_to_be16(uint16_t val)
Convert a 16 bytes integer to big endian format.
Definition: sol-util.h:836
int sol_util_unescape_quotes(const struct sol_str_slice slice, struct sol_buffer *buf)
Unescape a string removing quotes from it.
static void sol_util_clear_memory_secure(void *buf, size_t len)
Clear an allocated memory securely.
Definition: sol-util.h:725
ssize_t sol_util_base16_encode(void *buf, size_t buflen, const struct sol_str_slice slice, bool uppercase)
Encode the binary slice to base16 (hexadecimal).
char * sol_util_strerror(int errnum, struct sol_buffer *buf)
Gets a string from a given error.
static int sol_util_timespec_compare(const struct timespec *t1, const struct timespec *t2)
Compare two time values.
Definition: sol-util.h:178
int sol_util_size_add(size_t op1, size_t op2, size_t *out)
Add two values checking for overflow.
#define SOL_UTIL_USEC_PER_SEC
number of microseconds in a second: 1,000,000.
Definition: sol-util.h:69
ssize_t sol_util_base64_calculate_decoded_len(const struct sol_str_slice slice, const char base64_map[SOL_STATIC_ARRAY_SIZE(65)])
Calculate the size necessary to decode a given slice in base64.
long int sol_util_strtol_n(const char *nptr, char **endptr, ssize_t len, int base)
Wrapper over strtol() that consumes up to len bytes.
static int sol_util_usec_from_timespec(const struct timespec *ts)
Gets the number of microseconds for given time.
Definition: sol-util.h:242
static ssize_t sol_util_base16_calculate_encoded_len(const struct sol_str_slice slice)
Calculate the size necessary to encode a given slice in base16.
Definition: sol-util.h:691
double sol_util_strtod_n(const char *nptr, char **endptr, ssize_t len, bool use_locale)
Wrapper over strtod() that consumes up to len bytes and may not use a locale.
ssize_t sol_util_strftime(struct sol_buffer *buf, const char *format, const struct tm *timeptr, bool use_locale) SOL_ATTR_STRFTIME(2)
Wrapper around strftime()/strftime_l()
These are routines that Soletta provides for its string slice implementation.
struct timespec sol_util_timespec_get_current(void)
Gets the current time (Monotonic).
int sol_util_replace_str_if_changed(char **str, const char *new_str)
Replace the string's contents.
static uint16_t sol_util_le16_to_cpu(uint16_t val)
Convert a 16 bytes little endian integer to cpu endianness.
Definition: sol-util.h:902
sol_decode_case
Case of a string to be decoded.
Definition: sol-buffer.h:142
static struct timespec sol_util_timespec_from_msec(int msec)
Create a struct timespec from milliseconds.
Definition: sol-util.h:195
#define sol_util_uint16_bytes_swap(val)
Swaps the bytes of a 16 bytes unsigned int.
Definition: sol-util.h:820
int sol_util_ssize_mul(ssize_t op1, ssize_t op2, ssize_t *out)
Multiply two values checking for overflow.
static int sol_util_msec_from_timespec(const struct timespec *ts)
Gets the number of milliseconds for given time.
Definition: sol-util.h:229
#define sol_util_uint64_bytes_swap(val)
Swaps the bytes of a 32 bytes unsigned int.
Definition: sol-util.h:1009
int sol_util_timespec_get_realtime(struct timespec *t)
Gets the current time (System-wide clock).
A sol_buffer is a dynamic array, that can be resized if needed.
Definition: sol-buffer.h:130
int sol_util_uint32_mul(uint32_t op1, uint32_t op2, uint32_t *out)
Multiply two values checking for overflow.
static uint16_t sol_util_cpu_to_le16(uint16_t val)
Convert a 16 bytes integer to little endian format.
Definition: sol-util.h:858
#define SOL_UTIL_NSEC_PER_MSEC
number of nanoseconds in a milliseconds: 1,000,000,000 / 1,000 = 1,000,000.
Definition: sol-util.h:74
ssize_t sol_util_base64_encode(void *buf, size_t buflen, const struct sol_str_slice slice, const char base64_map[SOL_STATIC_ARRAY_SIZE(65)])
Encode the binary slice to base64 using the given map.
static uint32_t sol_util_cpu_to_le32(uint32_t val)
Convert a 32 bytes integer to little endian format.
Definition: sol-util.h:953
size_t len
Slice length.
Definition: sol-str-slice.h:85
static uint64_t sol_util_be64_to_cpu(uint64_t val)
Convert a 64 bytes big endian integer to cpu endianness.
Definition: sol-util.h:1073