46 lines
1.6 KiB
C
46 lines
1.6 KiB
C
/*
|
|
Copyright (c) 2014 IOnU Security Inc. All rights reserved
|
|
Created April 2014 by Kendrick Webster
|
|
|
|
K2Client/util.h - miscellaneous utility and low-level support functions
|
|
*/
|
|
#pragma once
|
|
|
|
#include "Hash.h"
|
|
#include "constants.h"
|
|
|
|
/* logging */
|
|
typedef enum
|
|
{
|
|
LOG_FATAL,
|
|
LOG_ERROR,
|
|
LOG_WARNING,
|
|
LOG_INFORMATIONAL,
|
|
LOG_DEBUG,
|
|
LOG_VERBOSE
|
|
}
|
|
log_level_t;
|
|
void initialize_logging(void);
|
|
void finalize_logging(void);
|
|
void set_log_level(log_level_t level); // ok to call at any time (w.r.t. initialize_logging et. al.)
|
|
void log_printf(log_level_t lev, const char* fmt, ...);
|
|
#define log_fatal(...) log_printf(LOG_FATAL, __VA_ARGS__)
|
|
#define log_error(...) log_printf(LOG_ERROR, __VA_ARGS__)
|
|
#define log_warn(...) log_printf(LOG_WARNING, __VA_ARGS__)
|
|
#define log_info(...) log_printf(LOG_INFORMATIONAL, __VA_ARGS__)
|
|
#define log_debug(...) log_printf(LOG_DEBUG, __VA_ARGS__)
|
|
#define log_verbose(...) log_printf(LOG_VERBOSE, __VA_ARGS__)
|
|
|
|
|
|
/* random number generator, thread safe and fast */
|
|
void seed_random(void); /* hash an entropy source (high-resolution clock, etc.) into the entropy pool */
|
|
void seed_random_with_bytes(void* buf, size_t len); /* hash a buffer into the RNG entropy pool */
|
|
unsigned int get_random(unsigned int modulus); /* returns number between 0 and (modulus - 1), note: slight bias (see implementation) */
|
|
void get_random_bytes(void* buf, size_t len); /* fills <len> bytes of <buf> with randomness */
|
|
|
|
|
|
/* misc */
|
|
#define TRUE 1
|
|
#define FALSE 0
|
|
int safe_strcpy(char* d, unsigned int n, const char* s); /* returns TRUE if string was truncated */
|