Make sure that input file name and output file name are distinct (to the extent possible).

This commit is contained in:
LoRd_MuldeR 2022-02-20 15:39:33 +01:00
parent 9b0d109336
commit 65c17e6759
Signed by: mulder
GPG Key ID: 2B5913365F57E03F
4 changed files with 130 additions and 40 deletions

View File

@ -147,6 +147,7 @@ int MAIN(const int argc, CHR *const argv[])
init_terminal(); init_terminal();
setup_signal_handler(SIGINT, sigint_handler); setup_signal_handler(SIGINT, sigint_handler);
int result = EXIT_FAILURE; int result = EXIT_FAILURE;
CHR *input_file = NULL, *output_file = NULL;
char *passphrase_buffer = NULL; char *passphrase_buffer = NULL;
FPRINTF(stderr, T("SlunkCrypt Utility (%") T(PRIstr) T("-%") T(PRIstr) T("), by LoRd_MuldeR <MuldeR2@GMX.de>\n"), OS_TYPE, CPU_ARCH); FPRINTF(stderr, T("SlunkCrypt Utility (%") T(PRIstr) T("-%") T(PRIstr) T("), by LoRd_MuldeR <MuldeR2@GMX.de>\n"), OS_TYPE, CPU_ARCH);
@ -203,8 +204,6 @@ int MAIN(const int argc, CHR *const argv[])
check_excess_arguments(argc, 5); check_excess_arguments(argc, 5);
const CHR *const passphrase = PW_FROM_ENV ? GETENV(ENV_PASSWORD) : argv[2U]; const CHR *const passphrase = PW_FROM_ENV ? GETENV(ENV_PASSWORD) : argv[2U];
const CHR *const input_file = argv[PW_FROM_ENV ? 2U : 3U], *const output_file = argv[PW_FROM_ENV ? 3U : 4U];
if ((!passphrase) || (!passphrase[0U])) if ((!passphrase) || (!passphrase[0U]))
{ {
FPUTS(T("Error: The passphrase must be specified, directly or indirectly!\n\n"), stderr); FPUTS(T("Error: The passphrase must be specified, directly or indirectly!\n\n"), stderr);
@ -220,9 +219,23 @@ int MAIN(const int argc, CHR *const argv[])
} }
} }
if ((!input_file[0U]) || (!output_file[0U])) input_file = absolute_path(argv[PW_FROM_ENV ? 2U : 3U]);
if ((!input_file) || (!input_file[0U]))
{ {
FPUTS(T("Error: The input file and/or output file must not be empty!\n\n"), stderr); FPUTS(T("Error: Input file path could not be resolved!\n\n"), stderr);
goto clean_up;
}
output_file = absolute_path(argv[PW_FROM_ENV ? 3U : 4U]);
if ((!output_file) || (!output_file[0U]))
{
FPUTS(T("Error: Output file path could not be resolved!\n\n"), stderr);
goto clean_up;
}
if (!path_compare(input_file, output_file))
{
FPUTS(T("Error: Input and output files must not be the same path! (effectively)\n\n"), stderr);
goto clean_up; goto clean_up;
} }
@ -303,6 +316,16 @@ clean_up:
free(passphrase_buffer); free(passphrase_buffer);
} }
if (input_file)
{
free(input_file);
}
if (output_file)
{
free(output_file);
}
return result; return result;
} }

View File

@ -58,6 +58,7 @@
# define STRNICMP(X,Y,Z) _wcsnicmp((X),(Y),(Z)) # define STRNICMP(X,Y,Z) _wcsnicmp((X),(Y),(Z))
# define STRRCHR(X,Y) wcsrchr((X),(Y)) # define STRRCHR(X,Y) wcsrchr((X),(Y))
# define STRTOUL(X) wcstoul((X), NULL, 0) # define STRTOUL(X) wcstoul((X), NULL, 0)
# define STRDUP(X) _wcsdup((X))
# define FPUTS(X,Y) fputws((X),(Y)) # define FPUTS(X,Y) fputws((X),(Y))
# define FPRINTF(X,Y,...) fwprintf((X),(Y),__VA_ARGS__) # define FPRINTF(X,Y,...) fwprintf((X),(Y),__VA_ARGS__)
# define REMOVE(X) _wremove((X)) # define REMOVE(X) _wremove((X))
@ -81,6 +82,7 @@
# define STRNICMP(X,Y,Z) strncasecmp((X),(Y),(Z)) # define STRNICMP(X,Y,Z) strncasecmp((X),(Y),(Z))
# define STRRCHR(X,Y) strrchr((X),(Y)) # define STRRCHR(X,Y) strrchr((X),(Y))
# define STRTOUL(X) strtoul((X), NULL, 0) # define STRTOUL(X) strtoul((X), NULL, 0)
# define STRDUP(X) strdup((X))
# define FPUTS(X,Y) fputs((X),(Y)) # define FPUTS(X,Y) fputs((X),(Y))
# define FPRINTF(X,Y,...) fprintf((X),(Y),__VA_ARGS__) # define FPRINTF(X,Y,...) fprintf((X),(Y),__VA_ARGS__)
# define REMOVE(X) remove((X)) # define REMOVE(X) remove((X))

View File

@ -19,6 +19,7 @@
#include <sys/types.h> #include <sys/types.h>
#include <time.h> #include <time.h>
#include <signal.h> #include <signal.h>
#include <stdarg.h>
/* Platform support */ /* Platform support */
#ifdef _WIN32 #ifdef _WIN32
@ -35,6 +36,7 @@
# define _O_U8TEXT 0x40000 # define _O_U8TEXT 0x40000
# endif # endif
#else #else
# include <unistd.h>
# if defined(__USE_LARGEFILE64) && (__USE_LARGEFILE64) # if defined(__USE_LARGEFILE64) && (__USE_LARGEFILE64)
# define STAT_T struct stat64 # define STAT_T struct stat64
# define FSTAT(X,Y) fstat64((X),(Y)) # define FSTAT(X,Y) fstat64((X),(Y))
@ -105,6 +107,65 @@ void setup_signal_handler(const int signo, signal_handler_t* const handler)
#endif #endif
} }
// ==========================================================================
// String functions
// ==========================================================================
char *concat_str(const char *const str, ...)
{
const char *ptr;
char *buffer;
size_t buff_size = 1U;
va_list arg;
va_start(arg, str);
for (ptr = str; ptr != NULL; ptr = va_arg(arg, const char*))
{
buff_size += strlen(ptr);
}
if ((buffer = (char*)calloc(buff_size, sizeof(char))))
{
va_end(arg);
va_start(arg, str);
for (ptr = str; ptr != NULL; ptr = va_arg(arg, const char*))
{
strcat(buffer, ptr);
}
}
va_end(arg);
return buffer;
}
char* CHR_to_utf8(const CHR *const input)
{
#ifdef _WIN32
char *buffer = NULL;
DWORD buffer_size = 0U, result = 0U;
buffer_size = input ? WideCharToMultiByte(CP_UTF8, 0, input, -1, NULL, 0, NULL, NULL) : 0U;
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); /*simple string copy*/
#endif
}
// ========================================================================== // ==========================================================================
// Time functions // Time functions
// ========================================================================== // ==========================================================================
@ -149,41 +210,6 @@ uint64_t clock_read(void)
return 0U; return 0U;
} }
// ==========================================================================
// Character set conversion
// ==========================================================================
char* CHR_to_utf8(const CHR *const input)
{
#ifdef _WIN32
char *buffer = NULL;
DWORD buffer_size = 0U, result = 0U;
buffer_size = input ? WideCharToMultiByte(CP_UTF8, 0, input, -1, NULL, 0, NULL, NULL) : 0U;
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); /*simple string copy*/
#endif
}
// ========================================================================== // ==========================================================================
// Byte-order support // Byte-order support
// ========================================================================== // ==========================================================================
@ -233,6 +259,42 @@ size_t fread_ui64(uint64_t *const value, FILE *const stream)
// File functions // File functions
// ========================================================================== // ==========================================================================
#define JOIN_PATHS(X,Y) concat_str((X), "/", (Y), NULL)
CHR *absolute_path(const CHR *const path)
{
#ifdef _WIN32
wchar_t *const result = _wfullpath(NULL, path, 0U);
#else
char *result = realpath(path, NULL);
if ((!result) && path[0U] && (path[0U] != '/'))
{
char *const cwd = getcwd(NULL, 0U);
if (cwd)
{
const char *path_off = path;
while ((path_off[0U] == '.') && (path_off[1U] == '/'))
{
path_off += 2U;
while (path_off[0U] == '/') ++path_off;
}
result = JOIN_PATHS(cwd, path_off);
free(cwd);
}
}
#endif
return result ? result : STRDUP(path);
}
int path_compare(const CHR *const path0, const CHR *const path1)
{
#ifdef _WIN32
return _wcsicmp(path0, path1);
#else
return strcmp(path0, path1);
#endif
}
uint64_t get_file_size(FILE* const file) uint64_t get_file_size(FILE* const file)
{ {
STAT_T stat; STAT_T stat;

View File

@ -23,8 +23,11 @@ size_t fwrite_ui64(const uint64_t value, FILE *const stream);
size_t fread_ui64(uint64_t *const value, FILE *const stream); size_t fread_ui64(uint64_t *const value, FILE *const stream);
char* CHR_to_utf8(const CHR *const input); char* CHR_to_utf8(const CHR *const input);
uint64_t get_file_size(FILE* const file);
CHR *absolute_path(const CHR *const path);
int path_compare(const CHR *const path0, const CHR *const path1);
const CHR *get_file_name(const CHR *path); const CHR *get_file_name(const CHR *path);
uint64_t get_file_size(FILE* const file);
uint64_t round_down(const uint64_t value, const uint64_t base); uint64_t round_down(const uint64_t value, const uint64_t base);