1 /******************************************************************************/
2 /* SlunkCrypt, by LoRd_MuldeR <MuldeR2@GMX.de> */
3 /* This work has been released under the CC0 1.0 Universal license! */
4 /******************************************************************************/
7 # define WIN32_LEAN_AND_MEAN 1
8 # define _CRT_SECURE_NO_WARNINGS 1
10 # define _GNU_SOURCE 1
14 #include <slunkcrypt.h>
19 #include <sys/types.h>
22 /* Platform support */
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
34 # define _O_U8TEXT 0x40000
37 # if defined(__USE_LARGEFILE64) && (__USE_LARGEFILE64)
38 # define STAT_T struct stat64
39 # define FSTAT(X,Y) fstat64((X),(Y))
41 # define STAT_T struct stat
42 # define FSTAT(X,Y) fstat((X),(Y))
44 # define FILENO(X) fileno((X))
47 // ==========================================================================
48 // Terminal initialization
49 // ==========================================================================
53 #define _acmdln GetCommandLineA()
54 #define _wcmdln GetCommandLineW()
56 extern char *const _acmdln
;
57 extern wchar_t *const _wcmdln
;
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");
76 void init_terminal(void)
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
);
87 // ==========================================================================
89 // ==========================================================================
91 void setup_signal_handler(const int signo
, signal_handler_t
* const handler
)
94 signal(signo
, handler
);
97 act
.sa_handler
= handler
;
98 sigemptyset(&act
.sa_mask
);
100 sigaction(signo
, &act
, NULL
);
104 // ==========================================================================
105 // Character set conversion
106 // ==========================================================================
108 char* CHR_to_utf8(const CHR
*const input
)
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)
120 buffer
= (char*)malloc(sizeof(char) * buffer_size
);
126 result
= WideCharToMultiByte(CP_UTF8
, 0, input
, -1, (LPSTR
)buffer
, buffer_size
, NULL
, NULL
);
127 if ((result
> 0U) && (result
<= buffer_size
))
135 return strdup(input
);
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
148 # define BIG_ENDIAN_BYTE_ORDER 0
151 uint64_t swap_bytes_u64(const uint64_t value
)
153 #if BIG_ENDIAN_BYTE_ORDER
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);
160 return value
; /*nothing to do*/
164 // ==========================================================================
166 // ==========================================================================
168 uint64_t get_file_size(FILE* const file
)
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;
184 const CHR
* get_file_name(const CHR
* path
)
187 while ((ptr
= STRRCHR(path
, T('/'))))
191 while ((ptr
= STRRCHR(path
, T('\\'))))
198 // ==========================================================================
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
;