11 #include <drift/dslcore.h>
12 #include <drift/GenLib.h>
13 #include <drift/Mutex.h>
14 #include <drift/Threading.h>
15 #include <drift/os_version.h>
21 DSL_Mutex * dslMutex()
23 static DSL_Mutex actualMutex;
27 int dsl_init_count = 0;
28 bool dsl_init_ret =
false;
29 uint32 dsl_runtime_options = 0;
31 vector<DSL_LIBRARY_FUNCTIONS> dsl_lib_funcs;
34 AutoMutexPtr(dslMutex());
35 if (dsl_init_count > 0 && dsl_init_ret) {
37 funcs.has_init =
true;
38 dsl_runtime_options |= funcs.nOption;
40 dsl_lib_funcs.push_back(funcs);
42 DSL_Library_Registerer::~DSL_Library_Registerer() {}
43 void DSL_Library_Registerer::EnsureLinked() {}
46 AutoMutexPtr(dslMutex());
51 if (dsl_init_count > 1) {
58 if (WSAStartup(MAKEWORD(2,2),&wsd) != 0) {
59 printf(
"DSL: Error initializing Winsock!\n");
66 for (
auto x = dsl_lib_funcs.begin(); x != dsl_lib_funcs.end(); x++) {
67 if (x->init == NULL || x->init()) {
69 dsl_runtime_options |= x->nOption;
78 AutoMutexPtr(dslMutex());
79 if (dsl_init_count > 0) {
85 if (dsl_init_count > 0) {
89 for (
auto x = dsl_lib_funcs.begin(); x != dsl_lib_funcs.end(); x++) {
90 if (x->has_init && x->cleanup != NULL) {
94 dsl_runtime_options &= ~x->nOption;
107 #if defined(ENABLE_ZLIB)
117 AutoMutexPtr(dslMutex());
118 static char version[64]={0};
122 sprintf(version,
"%d.%02d", ver.major, ver.minor);
123 #if defined(ENABLE_ZLIB)
129 if (dsl_runtime_options & DSL_OPTION_OPENSSL) {
132 if (dsl_runtime_options & DSL_OPTION_GNUTLS) {
135 if (dsl_runtime_options & DSL_OPTION_CURL) {
138 if (dsl_runtime_options & DSL_OPTION_MYSQL) {
141 if (dsl_runtime_options & DSL_OPTION_PHYSFS) {
144 if (dsl_runtime_options & DSL_OPTION_SQLITE) {
147 if (dsl_runtime_options & DSL_OPTION_SODIUM) {
153 bool DSL_CC dsl_rdrand(uint8 * buf,
size_t len);
161 #if (__GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 25))
162 while (left > 0 && tries++ < 10) {
163 int grn = getrandom(p, left, GRND_NONBLOCK);
172 #elif defined(SYS_getrandom)
173 while (left > 0 && tries++ < 10) {
174 int grn = syscall(SYS_getrandom, p, left, GRND_NONBLOCK);
184 while (left > 0 && tries++ < 10) {
185 FILE * fp = fopen(
"/dev/urandom",
"rb");
187 fp = fopen(
"/dev/random",
"rb");
191 size_t n = fread(p, 1, left, fp);
206 while (tries++ < 10) {
207 if (RtlGenRandom(buf, (ULONG)len)) {
213 #if defined(WIN32) || defined(GCC_RDRAND)
215 if (dsl_rdrand(buf, len)) {
222 for (
unsigned int i = 0; i < len; i++) {
223 buf[i] = rand() % 256;
231 #if defined(WIN32) || defined(GCC_RDRAND)
233 #include <immintrin.h>
235 bool DSL_CC dsl_rdrand(uint8 * buf,
size_t len) {
236 if (!InstructionSet::RDRAND()) {
243 while (left > 0 && tries < 10) {
244 #if defined(WIN64) || defined(__x86_64__)
249 long long unsigned int tmp;
251 if (_rdrand64_step(&tmp)) {
252 memcpy(p, &tmp,
sizeof(tmp));
262 if (_rdrand32_step(&tmp)) {
263 memcpy(p, &tmp,
sizeof(tmp));
271 if (_rdrand16_step(&tmp)) {
272 size_t toGen = (left <
sizeof(tmp)) ? left :
sizeof(tmp);
273 memcpy(p, &tmp, toGen);
282 return (left == 0) ? true :
false;
306 int len = vscprintf(fmt, va) + 1;
308 int len = vsnprintf(NULL, 0, fmt, va) + 1;
311 return dsl_strdup(
"vsnprintf error");
313 char * ret = (
char *)dsl_malloc(len);
314 if (vsnprintf(ret, len, fmt, va) < 0) {
315 return dsl_strdup(
"vsnprintf error");
324 int len = vscwprintf(fmt, va);
325 wchar_t * ret = (
wchar_t *)dsl_malloc((len+1)*
sizeof(wchar_t));
326 wvsprintfW(ret, fmt, va);
328 wchar_t * ret = NULL;
340 void * DSL_CC dsl_malloc(
size_t lSize) {
return malloc(lSize); }
341 void * DSL_CC
dsl_zmalloc(
size_t lSize) {
return calloc(1, lSize); }
342 void * DSL_CC dsl_realloc(
void * ptr,
size_t lSize) {
return realloc(ptr, lSize); }
343 char * DSL_CC dsl_strdup(
const char * ptr) {
return strdup(ptr); }
344 wchar_t * DSL_CC dsl_wcsdup(
const wchar_t * ptr) {
return wcsdup(ptr); }
string mprintf(const string fmt,...)
const char *DSL_CC dsl_get_version_string()
Returns the DSL version string.
wchar_t *DSL_CC dsl_wmprintf(const wchar_t *fmt,...)
void DSL_CC dsl_free(void *ptr)
void *DSL_CC dsl_zmalloc(size_t lSize)
char *DSL_CC dsl_vmprintf(const char *fmt, va_list va)
void DSL_CC dsl_freep(void **ptr)
void DSL_CC dsl_get_version(DSL_VERSION *ver)
Fills in a DSL_VERSION struct you provide.
char *DSL_CC dsl_mprintf(const char *fmt,...)
bool DSL_CC dsl_fill_random_buffer(uint8 *buf, size_t len, bool secure_only)
DSL_API uint64 DSL_CC GetTickCount64()
Linux version of GetTickCount64.
uint32 compile_options
See the DSL_OPTION_* defines. Will be a bitmask of 0 or more of them.