2020-10-13 15:04:59 +02:00
|
|
|
/******************************************************************************/
|
2020-10-19 21:56:12 +02:00
|
|
|
/* SlunkCrypt, by LoRd_MuldeR <MuldeR2@GMX.de> */
|
2020-10-13 15:04:59 +02:00
|
|
|
/* 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
|
2020-10-14 17:57:40 +02:00
|
|
|
#define _GNU_SOURCE 1
|
2020-10-13 15:04:59 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "utils.h"
|
2020-10-21 17:07:03 +02:00
|
|
|
#include <slunkcrypt.h>
|
2020-10-13 15:04:59 +02:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
2020-10-20 19:13:11 +02:00
|
|
|
#include <signal.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 19:33:01 +02:00
|
|
|
#include <io.h>
|
2020-10-13 15:37:40 +02:00
|
|
|
#include <fcntl.h>
|
2020-10-14 17:57:40 +02:00
|
|
|
#define STAT_T struct _stati64
|
|
|
|
#define FSTAT(X,Y) _fstati64((X),(Y))
|
|
|
|
#define FILENO(X) _fileno((X))
|
2020-10-13 15:37:40 +02:00
|
|
|
#define S_IFMT _S_IFMT
|
|
|
|
#define S_IFDIR _S_IFDIR
|
|
|
|
#define S_IFIFO _S_IFIFO
|
2020-10-21 17:07:03 +02:00
|
|
|
#ifndef _O_U8TEXT
|
|
|
|
#define _O_U8TEXT 0x40000
|
|
|
|
#endif
|
2020-10-16 18:05:37 +02:00
|
|
|
extern char *const _acmdln;
|
|
|
|
extern wchar_t *const _wcmdln;
|
2020-10-14 17:57:40 +02:00
|
|
|
#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))
|
2020-10-13 15:04:59 +02:00
|
|
|
#endif
|
|
|
|
|
2020-10-14 16:36:17 +02:00
|
|
|
void init_terminal(void)
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
|
|
|
SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOGPFAULTERRORBOX);
|
2020-10-16 19:33:12 +02:00
|
|
|
_setmode(_fileno(stdin), _O_BINARY);
|
2020-10-14 16:36:17 +02:00
|
|
|
_setmode(_fileno(stderr), _O_U8TEXT);
|
2020-10-21 17:07:03 +02:00
|
|
|
if (_acmdln) slunkcrypt_bzero(_acmdln, strlen(_acmdln) * sizeof(char));
|
|
|
|
if (_wcmdln) slunkcrypt_bzero(_wcmdln, wcslen(_wcmdln) * sizeof(wchar_t));
|
2020-10-14 16:36:17 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2020-10-20 19:13:11 +02:00
|
|
|
void setup_signal_handler(const int signo, signal_handler_t* const handler)
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
|
|
|
signal(signo, handler);
|
|
|
|
#else
|
|
|
|
struct sigaction act;
|
|
|
|
act.sa_handler = handler;
|
|
|
|
sigemptyset(&act.sa_mask);
|
|
|
|
act.sa_flags = 0;
|
|
|
|
sigaction(signo, &act, NULL);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2020-10-13 19:33:01 +02:00
|
|
|
char* CHR_to_utf8(const CHR*const input)
|
2020-10-13 15:04:59 +02:00
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
|
|
|
char* buffer;
|
|
|
|
DWORD buffer_size = 0U, result = 0U;
|
|
|
|
|
2020-10-13 19:33:01 +02:00
|
|
|
buffer_size = WideCharToMultiByte(CP_UTF8, 0, input, -1, NULL, 0, NULL, NULL);
|
2020-10-13 15:04:59 +02:00
|
|
|
if (buffer_size < 1U)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
buffer = (char*)malloc(sizeof(char) * buffer_size);
|
|
|
|
if (!buffer)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2020-10-13 19:33:01 +02:00
|
|
|
result = WideCharToMultiByte(CP_UTF8, 0, input, -1, (LPSTR)buffer, buffer_size, NULL, NULL);
|
2020-10-13 15:04:59 +02:00
|
|
|
if ((result > 0U) && (result <= buffer_size))
|
|
|
|
{
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
free(buffer);
|
|
|
|
return NULL;
|
|
|
|
#else
|
2020-10-13 19:50:29 +02:00
|
|
|
return strdup(input);
|
2020-10-13 15:04:59 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2020-10-14 16:36:17 +02:00
|
|
|
uint64_t get_file_size(FILE* const file)
|
2020-10-13 15:04:59 +02:00
|
|
|
{
|
2020-10-14 17:57:40 +02:00
|
|
|
STAT_T stat;
|
|
|
|
if (FSTAT(FILENO(file), &stat) != 0)
|
2020-10-13 15:04:59 +02:00
|
|
|
{
|
|
|
|
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
|
|
|
{
|
2020-10-14 21:55:39 +02:00
|
|
|
const int64_t ssize = stat.st_size;
|
|
|
|
return (ssize >= 0) ? ((uint64_t)ssize) : 0U;
|
2020-10-13 15:04:59 +02:00
|
|
|
}
|
|
|
|
return 0U;
|
|
|
|
}
|
2020-10-13 19:33:01 +02:00
|
|
|
|
2020-10-14 16:36:17 +02:00
|
|
|
const CHR* get_file_name(const CHR* path)
|
2020-10-13 19:33:01 +02:00
|
|
|
{
|
2020-10-14 16:36:17 +02:00
|
|
|
const CHR* ptr;
|
2020-10-14 17:57:40 +02:00
|
|
|
while ((ptr = STRRCHR(path, T('/'))))
|
2020-10-14 16:36:17 +02:00
|
|
|
{
|
|
|
|
path = ptr + 1U;
|
|
|
|
}
|
2020-10-14 17:57:40 +02:00
|
|
|
while ((ptr = STRRCHR(path, T('\\'))))
|
2020-10-14 16:36:17 +02:00
|
|
|
{
|
|
|
|
path = ptr + 1U;
|
|
|
|
}
|
|
|
|
return path;
|
2020-10-13 19:33:01 +02:00
|
|
|
}
|