#ifndef UTILS_H_
#define UTILS_H_

#include <stdint.h>

// Helper, write a message to stdout and exit with an error
#define LOG(...)                                                      \
    do {                                                              \
        (void) printf("[%s() %s:%d] ", __func__, __FILE__, __LINE__); \
        (void) printf(__VA_ARGS__);                                   \
        (void) printf("\n");                                          \
    } while (0)

#define PANIC(...)              \
    do {                        \
        LOG(__VA_ARGS__);       \
        exit(EXIT_FAILURE);     \
    } while(0)

// Used only for checking calls to libtomcrypt
#define LTC_CALL_OK(ret)                          \
do {                                              \
    if ((ret) != CRYPT_OK) {                      \
        fprintf(stderr,                           \
                "[%s %s():%d] Error [%s] (%d)\n", \
                __FILE__, __func__, __LINE__,     \
                #ret, ret);                       \
        res = (0);                                \
        goto end;                                 \
    }                                             \
} while(0)


// Simple struct with a pointer to buffer and it's length
typedef struct buf_t {
    uint8_t *b;  // pointer to some buffer
    size_t sz;   // it's size
} buf_t;

#define CHECK_BUF(x)                            \
do {                                            \
    if (!(x) || !(x)->b)                        \
        goto end;                               \
} while(0)

#endif // UTILS_H_