105 lines
2.2 KiB
C
105 lines
2.2 KiB
C
/******************************************************************************/
|
|
/* 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
|
|
#else
|
|
#define _GNU_SOURCE 1
|
|
#endif
|
|
|
|
#include "utils.h"
|
|
#include <sys/stat.h>
|
|
#include <sys/types.h>
|
|
|
|
#ifdef _WIN32
|
|
#include <Windows.h>
|
|
#include <io.h>
|
|
#include <fcntl.h>
|
|
#define STAT_T struct _stati64
|
|
#define FSTAT(X,Y) _fstati64((X),(Y))
|
|
#define FILENO(X) _fileno((X))
|
|
#define S_IFMT _S_IFMT
|
|
#define S_IFDIR _S_IFDIR
|
|
#define S_IFIFO _S_IFIFO
|
|
#else
|
|
#if defined(__USE_LARGEFILE64) && (__USE_LARGEFILE64)
|
|
#define STAT_T struct stat64
|
|
#define FSTAT(X,Y) fstat64((X),(Y))
|
|
#else
|
|
#define STAT_T struct stat
|
|
#define FSTAT(X,Y) fstat((X),(Y))
|
|
#endif
|
|
#define FILENO(X) fileno((X))
|
|
#endif
|
|
|
|
void init_terminal(void)
|
|
{
|
|
#ifdef _WIN32
|
|
SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOGPFAULTERRORBOX);
|
|
_setmode(_fileno(stderr), _O_U8TEXT);
|
|
#endif
|
|
}
|
|
|
|
char* CHR_to_utf8(const CHR*const input)
|
|
{
|
|
#ifdef _WIN32
|
|
char* buffer;
|
|
DWORD buffer_size = 0U, result = 0U;
|
|
|
|
buffer_size = WideCharToMultiByte(CP_UTF8, 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(CP_UTF8, 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)
|
|
{
|
|
STAT_T stat;
|
|
if (FSTAT(FILENO(file), &stat) != 0)
|
|
{
|
|
return UINT64_MAX;
|
|
}
|
|
uint16_t file_type = stat.st_mode & S_IFMT;
|
|
if ((file_type != S_IFDIR) && (file_type != S_IFIFO))
|
|
{
|
|
return (stat.st_size >= 0LL) ? ((uint64_t)stat.st_size) : 0U;
|
|
}
|
|
return 0U;
|
|
}
|
|
|
|
const CHR* get_file_name(const CHR* path)
|
|
{
|
|
const CHR* ptr;
|
|
while ((ptr = STRRCHR(path, T('/'))))
|
|
{
|
|
path = ptr + 1U;
|
|
}
|
|
while ((ptr = STRRCHR(path, T('\\'))))
|
|
{
|
|
path = ptr + 1U;
|
|
}
|
|
return path;
|
|
}
|