2020-10-13 15:04:59 +02:00
|
|
|
/******************************************************************************/
|
|
|
|
/* MCrypt, by LoRd_MuldeR <MuldeR2@GMX.de> */
|
|
|
|
/* This work has been released under the CC0 1.0 Universal license! */
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
#define WIN32_LEAN_AND_MEAN 1
|
|
|
|
#define _CRT_SECURE_NO_WARNINGS 1
|
2020-10-13 15:37:40 +02:00
|
|
|
#else
|
|
|
|
#define _LARGEFILE64_SOURCE 1
|
2020-10-13 15:04:59 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "utils.h"
|
2020-10-13 15:37:40 +02:00
|
|
|
#include <string.h>
|
2020-10-13 15:04:59 +02:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
2020-10-13 15:37:40 +02:00
|
|
|
|
2020-10-13 15:04:59 +02:00
|
|
|
#ifdef _WIN32
|
|
|
|
#include <Windows.h>
|
2020-10-13 15:37:40 +02:00
|
|
|
#include <fcntl.h>
|
|
|
|
#define S_IFMT _S_IFMT
|
|
|
|
#define S_IFDIR _S_IFDIR
|
|
|
|
#define S_IFIFO _S_IFIFO
|
2020-10-13 15:04:59 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
void erase(void *const ptr, const size_t length)
|
|
|
|
{
|
|
|
|
volatile uint8_t* buffer = ptr;
|
|
|
|
for (size_t i = 0U; i < length; ++i)
|
|
|
|
{
|
|
|
|
buffer[i] = 0U;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
char* utf16_to_bytes(const CHR*const input, const uint32_t code_page)
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
|
|
|
char* buffer;
|
|
|
|
DWORD buffer_size = 0U, result = 0U;
|
|
|
|
|
|
|
|
buffer_size = WideCharToMultiByte(code_page, 0, input, -1, NULL, 0, NULL, NULL);
|
|
|
|
if (buffer_size < 1U)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
buffer = (char*)malloc(sizeof(char) * buffer_size);
|
|
|
|
if (!buffer)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
result = WideCharToMultiByte(code_page, 0, input, -1, (LPSTR)buffer, buffer_size, NULL, NULL);
|
|
|
|
if ((result > 0U) && (result <= buffer_size))
|
|
|
|
{
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
free(buffer);
|
|
|
|
return NULL;
|
|
|
|
#else
|
|
|
|
return strdup(input);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t get_file_size(FILE *const file)
|
|
|
|
{
|
|
|
|
STAT64_T stat;
|
|
|
|
if (FSTAT64(FILENO(file), &stat) != 0)
|
|
|
|
{
|
|
|
|
return UINT64_MAX;
|
|
|
|
}
|
2020-10-13 15:37:40 +02:00
|
|
|
uint16_t file_type = stat.st_mode & S_IFMT;
|
|
|
|
if ((file_type != S_IFDIR) && (file_type != S_IFIFO))
|
2020-10-13 15:04:59 +02:00
|
|
|
{
|
|
|
|
return (stat.st_size >= 0LL) ? ((uint64_t)stat.st_size) : 0U;
|
|
|
|
}
|
|
|
|
return 0U;
|
|
|
|
}
|