Implemented checksum based on BLAKE2s hash function.
[slunkcrypt.git] / frontend / src / utils.c
blob0c435f337b23602a5208c1698acd59b5ad889f43
1 /******************************************************************************/
2 /* SlunkCrypt, by LoRd_MuldeR <MuldeR2@GMX.de> */
3 /* This work has been released under the CC0 1.0 Universal license! */
4 /******************************************************************************/
6 #ifdef _WIN32
7 # define WIN32_LEAN_AND_MEAN 1
8 # define _CRT_SECURE_NO_WARNINGS 1
9 #else
10 # define _GNU_SOURCE 1
11 #endif
13 /* Internal */
14 #include <slunkcrypt.h>
15 #include "utils.h"
17 /* CRT */
18 #include <sys/stat.h>
19 #include <sys/types.h>
20 #include <signal.h>
22 /* Platform support */
23 #ifdef _WIN32
24 # include <Windows.h>
25 # include <io.h>
26 # include <fcntl.h>
27 # define STAT_T struct _stati64
28 # define FSTAT(X,Y) _fstati64((X),(Y))
29 # define FILENO(X) _fileno((X))
30 # define S_IFMT _S_IFMT
31 # define S_IFDIR _S_IFDIR
32 # define S_IFIFO _S_IFIFO
33 # ifndef _O_U8TEXT
34 # define _O_U8TEXT 0x40000
35 # endif
36 #else
37 # if defined(__USE_LARGEFILE64) && (__USE_LARGEFILE64)
38 # define STAT_T struct stat64
39 # define FSTAT(X,Y) fstat64((X),(Y))
40 # else
41 # define STAT_T struct stat
42 # define FSTAT(X,Y) fstat((X),(Y))
43 # endif
44 # define FILENO(X) fileno((X))
45 #endif
47 // ==========================================================================
48 // Terminal initialization
49 // ==========================================================================
51 #ifdef _WIN32
52 #ifdef _DLL
53 #define _acmdln GetCommandLineA()
54 #define _wcmdln GetCommandLineW()
55 #else
56 extern char *const _acmdln;
57 extern wchar_t *const _wcmdln;
58 #endif
59 static void clear_cmdline_args(char *const acmdln, wchar_t *const wcmdln)
61 if (acmdln && acmdln[0U])
63 const size_t len = strlen(acmdln);
64 slunkcrypt_bzero(acmdln, len * sizeof(char));
65 if (len > 5U) strcpy(acmdln, "slunk");
67 if (wcmdln && wcmdln[0U])
69 const size_t len = wcslen(wcmdln);
70 slunkcrypt_bzero(wcmdln, len * sizeof(wchar_t));
71 if (len > 5U) wcscpy(wcmdln, L"slunk");
74 #endif
76 void init_terminal(void)
78 #ifdef _WIN32
79 SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOGPFAULTERRORBOX);
80 _setmode(_fileno(stdout), _O_BINARY);
81 _setmode(_fileno(stdin), _O_BINARY);
82 _setmode(_fileno(stderr), _O_U8TEXT);
83 clear_cmdline_args(_acmdln, _wcmdln);
84 #endif
87 // ==========================================================================
88 // Signal handling
89 // ==========================================================================
91 void setup_signal_handler(const int signo, signal_handler_t* const handler)
93 #ifdef _WIN32
94 signal(signo, handler);
95 #else
96 struct sigaction act;
97 act.sa_handler = handler;
98 sigemptyset(&act.sa_mask);
99 act.sa_flags = 0;
100 sigaction(signo, &act, NULL);
101 #endif
104 // ==========================================================================
105 // Character set conversion
106 // ==========================================================================
108 char* CHR_to_utf8(const CHR*const input)
110 #ifdef _WIN32
111 char* buffer;
112 DWORD buffer_size = 0U, result = 0U;
114 buffer_size = WideCharToMultiByte(CP_UTF8, 0, input, -1, NULL, 0, NULL, NULL);
115 if (buffer_size < 1U)
117 return NULL;
120 buffer = (char*)malloc(sizeof(char) * buffer_size);
121 if (!buffer)
123 return NULL;
126 result = WideCharToMultiByte(CP_UTF8, 0, input, -1, (LPSTR)buffer, buffer_size, NULL, NULL);
127 if ((result > 0U) && (result <= buffer_size))
129 return buffer;
132 free(buffer);
133 return NULL;
134 #else
135 return strdup(input);
136 #endif
139 // ==========================================================================
140 // Byte-order support
141 // ==========================================================================
143 #if defined(__BYTE_ORDER) && defined(__BIG_ENDIAN) && (__BYTE_ORDER == __BIG_ENDIAN)
144 # define BIG_ENDIAN_BYTE_ORDER 1
145 #elif defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__) && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)
146 # define BIG_ENDIAN_BYTE_ORDER 1
147 #else
148 # define BIG_ENDIAN_BYTE_ORDER 0
149 #endif
151 uint64_t swap_bytes_u64(const uint64_t value)
153 #if BIG_ENDIAN_BYTE_ORDER
154 return
155 (((value) >> 56) & 0x00000000000000FF) | (((value) >> 40) & 0x000000000000FF00) |
156 (((value) >> 24) & 0x0000000000FF0000) | (((value) >> 8) & 0x00000000FF000000) |
157 (((value) << 8) & 0x000000FF00000000) | (((value) << 24) & 0x0000FF0000000000) |
158 (((value) << 40) & 0x00FF000000000000) | (((value) << 56) & 0xFF00000000000000);
159 #else
160 return value; /*nothing to do*/
161 #endif
164 // ==========================================================================
165 // File functions
166 // ==========================================================================
168 uint64_t get_file_size(FILE* const file)
170 STAT_T stat;
171 if (FSTAT(FILENO(file), &stat) == 0)
173 const uint16_t file_type = stat.st_mode & S_IFMT;
174 if ((file_type != S_IFDIR) && (file_type != S_IFIFO))
176 const int64_t ssize = stat.st_size;
177 return (ssize >= 0) ? ((uint64_t)ssize) : 0U;
179 return 0U;
181 return UINT64_MAX;
184 const CHR* get_file_name(const CHR* path)
186 const CHR* ptr;
187 while ((ptr = STRRCHR(path, T('/'))))
189 path = ptr + 1U;
191 while ((ptr = STRRCHR(path, T('\\'))))
193 path = ptr + 1U;
195 return path;
198 // ==========================================================================
199 // Math functions
200 // ==========================================================================
202 uint64_t round_down(const uint64_t value, const uint64_t base)
204 const uint64_t modulus = value % base;
205 return modulus ? (value - modulus) : value;