Soletta™ Framework
Framework for making IoT devices

Full online documentation | C API Index
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
Macros | Functions
sol-util.h File Reference

Useful general routines. More...

#include <sol-buffer.h>
#include <sol-str-slice.h>
#include <ctype.h>
#include <errno.h>
#include <inttypes.h>
#include <limits.h>
#include <stdarg.h>
#include <string.h>
#include <sys/types.h>
#include <time.h>

Go to the source code of this file.

Macros

#define sol_util_array_size(arr)   (sizeof(arr) / sizeof((arr)[0]))
 Calculates the number of elements in an array. More...
 
#define sol_util_max(x, y)
 Gets the maximum value. More...
 
#define sol_util_min(x, y)
 Gets the minimum value. More...
 
#define SOL_UTIL_MSEC_PER_SEC   1000ULL
 number of milliseconds in a second: 1,000. More...
 
#define SOL_UTIL_NSEC_PER_MSEC   1000000ULL
 number of nanoseconds in a milliseconds: 1,000,000,000 / 1,000 = 1,000,000. More...
 
#define SOL_UTIL_NSEC_PER_SEC   1000000000ULL
 number of nanoseconds in a second: 1,000,000,000. More...
 
#define SOL_UTIL_NSEC_PER_USEC   1000ULL
 number of nanoseconds in a microsecond: 1,000,000,000 / 1,000,000 = 1,000. More...
 
#define sol_util_strerrora(errnum)
 Gets a string from a given error using the stack. More...
 
#define sol_util_uint16_bytes_swap(val)   ((uint16_t)((((val) >> 8) & 0xff) | (((val) & 0xff) << 8)))
 Swaps the bytes of a 16 bytes unsigned int. More...
 
#define sol_util_uint32_bytes_swap(val)
 Swaps the bytes of a 32 bytes unsigned int. More...
 
#define sol_util_uint64_bytes_swap(val)
 Swaps the bytes of a 32 bytes unsigned int. More...
 
#define SOL_UTIL_USEC_PER_SEC   1000000ULL
 number of microseconds in a second: 1,000,000. More...
 

Functions

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. More...
 
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. More...
 
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). More...
 
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). More...
 
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. More...
 
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. More...
 
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. More...
 
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. More...
 
static uint16_t sol_util_be16_to_cpu (uint16_t val)
 Convert a 16 bytes big endian integer to cpu endianness. More...
 
static uint32_t sol_util_be32_to_cpu (uint32_t val)
 Convert a 32 bytes big endian integer to cpu endianness. More...
 
static uint64_t sol_util_be64_to_cpu (uint64_t val)
 Convert a 64 bytes big endian integer to cpu endianness. More...
 
static void sol_util_clear_memory_secure (void *buf, size_t len)
 Clear an allocated memory securely. More...
 
static uint16_t sol_util_cpu_to_be16 (uint16_t val)
 Convert a 16 bytes integer to big endian format. More...
 
static uint32_t sol_util_cpu_to_be32 (uint32_t val)
 Convert a 32 bytes integer to big endian format. More...
 
static uint64_t sol_util_cpu_to_be64 (uint64_t val)
 Convert a 64 bytes integer to big endian format. More...
 
static uint16_t sol_util_cpu_to_le16 (uint16_t val)
 Convert a 16 bytes integer to little endian format. More...
 
static uint32_t sol_util_cpu_to_le32 (uint32_t val)
 Convert a 32 bytes integer to little endian format. More...
 
static uint64_t sol_util_cpu_to_le64 (uint64_t val)
 Convert a 64 bytes integer to little endian format. More...
 
bool sol_util_double_eq (double var0, double var1)
 Checks var0 and var1 for equality. More...
 
static int32_t sol_util_int32_clamp (int32_t start, int32_t end, int32_t value)
 Restricts a number between two other numbers. More...
 
int sol_util_int32_mul (int32_t op1, int32_t op2, int32_t *out)
 Multiply two values checking for overflow. More...
 
int sol_util_int64_mul (int64_t op1, int64_t op2, int64_t *out)
 Multiply two values checking for overflow. More...
 
static uint16_t sol_util_le16_to_cpu (uint16_t val)
 Convert a 16 bytes little endian integer to cpu endianness. More...
 
static uint32_t sol_util_le32_to_cpu (uint32_t val)
 Convert a 32 bytes little endian integer to cpu endianness. More...
 
static uint64_t sol_util_le64_to_cpu (uint64_t val)
 Convert a 64 bytes little endian integer to cpu endianness. More...
 
void * sol_util_memdup (const void *data, size_t len)
 Duplicate memory. More...
 
static int sol_util_msec_from_timespec (const struct timespec *ts)
 Gets the number of milliseconds for given time. More...
 
int sol_util_replace_str_from_slice_if_changed (char **str, const struct sol_str_slice slice)
 Replace the string's contents. More...
 
int sol_util_replace_str_if_changed (char **str, const char *new_str)
 Replace the string's contents. More...
 
int sol_util_size_add (size_t op1, size_t op2, size_t *out)
 Add two values checking for overflow. More...
 
int sol_util_size_mul (size_t op1, size_t op2, size_t *out)
 Multiply two values checking for overflow. More...
 
int sol_util_size_sub (size_t op1, size_t op2, size_t *out)
 Subtract two values checking for overflow. More...
 
int sol_util_ssize_mul (ssize_t op1, ssize_t op2, ssize_t *out)
 Multiply two values checking for overflow. More...
 
char * sol_util_strerror (int errnum, struct sol_buffer *buf)
 Gets a string from a given error. More...
 
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() More...
 
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. More...
 
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. More...
 
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. More...
 
static void sol_util_timespec_add (const struct timespec *t1, const struct timespec *t2, struct timespec *result)
 Sum two time values. More...
 
static int sol_util_timespec_compare (const struct timespec *t1, const struct timespec *t2)
 Compare two time values. More...
 
static struct timespec sol_util_timespec_from_msec (int msec)
 Create a struct timespec from milliseconds. More...
 
static struct timespec sol_util_timespec_from_usec (int usec)
 Create a struct timespec from microseconds. More...
 
struct timespec sol_util_timespec_get_current (void)
 Gets the current time (Monotonic). More...
 
int sol_util_timespec_get_realtime (struct timespec *t)
 Gets the current time (System-wide clock). More...
 
static void sol_util_timespec_sub (const struct timespec *t1, const struct timespec *t2, struct timespec *result)
 Subtracts two time values. More...
 
int sol_util_uint32_mul (uint32_t op1, uint32_t op2, uint32_t *out)
 Multiply two values checking for overflow. More...
 
int sol_util_uint64_add (uint64_t op1, uint64_t op2, uint64_t *out)
 Add two values checking for overflow. More...
 
int sol_util_uint64_mul (uint64_t op1, uint64_t op2, uint64_t *out)
 Multiply two values checking for overflow. More...
 
int sol_util_unescape_quotes (const struct sol_str_slice slice, struct sol_buffer *buf)
 Unescape a string removing quotes from it. More...
 
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. More...
 
static int sol_util_usec_from_timespec (const struct timespec *ts)
 Gets the number of microseconds for given time. More...
 
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. More...
 
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. More...
 
int sol_util_uuid_gen (bool uppercase, bool with_hyphens, struct sol_buffer *uuid_buf)
 Generates a new universally unique identifier (UUID) string. More...
 
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. More...
 
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. More...
 

Detailed Description

Useful general routines.