#include <ctype.h>
#include <signal.h>
+#define BUFF_SIZE 4096U
static volatile int g_interrupted = 0;
static char* read_passphrase(const CHR* const file_name)
{
return NULL;
}
- FILE *const fin = FOPEN(file_name, T("rb"));
- if (!fin)
+ FILE *const file = FOPEN(file_name, T("rb"));
+ if (!file)
{
return NULL;
}
do
{
- if (!fgets(buffer, (int)buff_size, fin))
+ if (!fgets(buffer, (int)buff_size, file))
{
- fclose(fin);
+ fclose(file);
free(buffer);
return NULL;
}
}
}
while (!buffer[0U]);
- fclose(fin);
+ fclose(file);
return buffer;
}
return !strong;
}
-static int open_files(FILE** const fin, FILE** const fout, const CHR* const input, const CHR* const output)
+static int open_files(FILE **const file_in, FILE **const file_out, const CHR* const input_path, const CHR* const output_path)
{
- *fin = FOPEN(input, T("rb"));
- if (!(*fin))
+ *file_in = FOPEN(input_path, T("rb"));
+ if (!(*file_in))
{
FPUTS(T("Error: Failed to open input file for reading!\n\n"), stderr);
return 1;
}
- *fout = FOPEN(output, T("wb"));
- if (!(*fout))
+ *file_out = FOPEN(output_path, T("wb"));
+ if (!(*file_out))
{
FPUTS(T("Error: Failed to open output file for writing!\n\n"), stderr);
- fclose(*fin);
+ fclose(*file_out);
return 1;
}
return 0;
}
-static int encrypt(const char* const passphrase, const CHR* const input, const CHR* const output)
+static int encrypt(const char* const passphrase, const CHR* const input_path, const CHR* const output_path)
{
mcrypt_t ctx = MCRYPT_NULL;
- FILE *fin = NULL, *fout = NULL;
+ FILE * file_in = NULL, * file_out = NULL;
int result = 1;
- if (open_files(&fin, &fout, input, output) != 0)
+ if (open_files(&file_in, &file_out, input_path, output_path) != 0)
{
goto clean_up;;
}
- const uint64_t file_size = get_file_size(fin);
+ const uint64_t file_size = get_file_size(file_in);
if (file_size == UINT64_MAX)
{
FPUTS(T("I/O error: Failed to determine size of input file!\n\n"), stderr);
goto clean_up;
}
- else if (file_size < 1LL)
+ else if (file_size < 1U)
{
- FPUTS(T("Error: Input file is empty!\n\n"), stderr);
+ FPUTS(T("Error: Input file is empty or an unsupported type!\n\n"), stderr);
goto clean_up;
}
goto clean_up;
}
- if (fwrite(&seed, sizeof(uint64_t), 1U, fout) < 1U)
+ if (fwrite(&seed, sizeof(uint64_t), 1U, file_out) < 1U)
{
FPUTS(T("I/O error: Failed to write seed value!\n\n"), stderr);
goto clean_up;
clock_t clk_now, clk_update = clock();
uint64_t crc_actual = CRC_INITIALIZER, bytes_read = sizeof(uint64_t);
- uint8_t buffer[1024U];
+ uint8_t buffer[BUFF_SIZE];
- while ((!feof(fin)) && (bytes_read < file_size))
+ while (bytes_read < file_size)
{
- const size_t count = fread(buffer, sizeof(uint8_t), 1024U, fin);
- if (ferror(fin))
- {
- FPUTS(T("\n\nI/O error: Failed to read input data!\n\n"), stderr);
- goto clean_up;
- }
+ const uint64_t bytes_remaining = file_size - bytes_read;
+ const size_t request_len = (bytes_remaining < BUFF_SIZE) ? ((size_t)bytes_remaining) : BUFF_SIZE;
+ const size_t count = fread(buffer, sizeof(uint8_t), request_len, file_in);
if (count > 0U)
{
crc_actual = crc64_update(crc_actual, buffer, count);
FPUTS(T("\n\nMCrypt error: Failed to encrypt data!\n\n"), stderr);
goto clean_up;
}
- if (fwrite(buffer, sizeof(uint8_t), count, fout) < count)
+ if (fwrite(buffer, sizeof(uint8_t), count, file_out) < count)
{
FPUTS(T("\n\nI/O error: Failed to write encrypted data!\n\n"), stderr);
goto clean_up;
}
}
+ if (count < request_len)
+ {
+ break; /*EOF*/
+ }
if ((clk_now = clock()) - clk_update > CLOCKS_PER_SEC)
{
FPRINTF(stderr, T("\b\b\b\b\b\b%5.1f%%"), (bytes_read / ((double)file_size)) * 100.0);
}
}
- crc_actual = crc64_finish(crc_actual);
+ if (ferror(file_in))
+ {
+ FPUTS(T("\n\nI/O error: Failed to read input data!\n\n"), stderr);
+ goto clean_up;
+ }
if (bytes_read < file_size)
{
goto clean_up;
}
+ crc_actual = crc64_finish(crc_actual);
FPRINTF(stderr, T("\b\b\b\b\b\b%5.1f%%\n\n"), 100.0);
- if (fwrite(&crc_actual, sizeof(uint64_t), 1U, fout) < 1U)
+ if (fwrite(&crc_actual, sizeof(uint64_t), 1U, file_out) < 1U)
{
FPUTS(T("I/O error: Failed to write CRC checksum!\n\n"), stderr);
goto clean_up;
mcrypt_free(ctx);
}
- if (fout)
+ if (file_out)
{
- fclose(fout);
+ fclose(file_out);
}
- if (fin)
+ if (file_in)
{
- fclose(fin);
+ fclose(file_in);
}
return result;
}
-static int decrypt(const char* const passphrase, const CHR* const input, const CHR* const output)
+static int decrypt(const char* const passphrase, const CHR* const input_path, const CHR* const output_path)
{
mcrypt_t ctx = MCRYPT_NULL;
- FILE *fin = NULL, *fout = NULL;
+ FILE *file_in = NULL, *file_out = NULL;
int result = 1;
- if (open_files(&fin, &fout, input, output) != 0)
+ if (open_files(&file_in, &file_out, input_path, output_path) != 0)
{
goto clean_up;
}
- const uint64_t file_size = get_file_size(fin);
+ const uint64_t file_size = get_file_size(file_in);
if (file_size == UINT64_MAX)
{
FPUTS(T("I/O error: Failed to determine size of input file!\n\n"), stderr);
goto clean_up;
}
- else if (file_size < 16LL)
+ else if (file_size < 16U)
{
FPUTS(T("Error: Input file is too small! Truncated?\n\n"), stderr);
goto clean_up;
}
uint64_t seed;
- if (fread(&seed, sizeof(uint64_t), 1U, fin) < 1U)
+ if (fread(&seed, sizeof(uint64_t), 1U, file_in) < 1U)
{
FPUTS(T("I/O error: Failed to read seed value!\n\n"), stderr);
goto clean_up;
clock_t clk_now, clk_update = clock();
uint64_t crc_actual = CRC_INITIALIZER, bytes_read = sizeof(uint64_t);
- uint8_t buffer[1024U];
+ uint8_t buffer[BUFF_SIZE];
const uint64_t read_limit = file_size - sizeof(uint64_t);
- while ((!feof(fin)) && (bytes_read < read_limit))
+ while (bytes_read < read_limit)
{
const uint64_t bytes_remaining = read_limit - bytes_read;
- const size_t read_len = (bytes_remaining < 1024U) ? ((size_t)bytes_remaining) : 1024U;
- const size_t count = fread(buffer, sizeof(uint8_t), read_len, fin);
- if (ferror(fin))
- {
- FPUTS(T("\n\nI/O error: Failed to read encrypted data!\n\n"), stderr);
- goto clean_up;
- }
+ const size_t request_len = (bytes_remaining < BUFF_SIZE) ? ((size_t)bytes_remaining) : BUFF_SIZE;
+ const size_t count = fread(buffer, sizeof(uint8_t), request_len, file_in);
if (count > 0U)
{
bytes_read += count;
goto clean_up;
}
crc_actual = crc64_update(crc_actual, buffer, count);
- if (fwrite(buffer, sizeof(uint8_t), count, fout) < count)
+ if (fwrite(buffer, sizeof(uint8_t), count, file_out) < count)
{
FPUTS(T("failed!\n\nI/O error: Failed to write decrypted data!\n\n"), stderr);
goto clean_up;
}
}
+ if (count < request_len)
+ {
+ break; /*EOF*/
+ }
if ((clk_now = clock()) - clk_update > CLOCKS_PER_SEC)
{
FPRINTF(stderr, T("\b\b\b\b\b\b%5.1f%%"), (bytes_read / ((double)read_limit)) * 100.0);
}
}
- crc_actual = crc64_finish(crc_actual);
+ if (ferror(file_in))
+ {
+ FPUTS(T("\n\nI/O error: Failed to read input data!\n\n"), stderr);
+ goto clean_up;
+ }
if (bytes_read < read_limit)
{
goto clean_up;
}
+ crc_actual = crc64_finish(crc_actual);
FPRINTF(stderr, T("\b\b\b\b\b\b%5.1f%%\n\n"), 100.0);
uint64_t crc_expected;
- if (fread(&crc_expected, sizeof(uint64_t), 1U, fin) < 1U)
+ if (fread(&crc_expected, sizeof(uint64_t), 1U, file_in) < 1U)
{
FPUTS(T("I/O error: Failed to read CRC checksum!\n\n"), stderr);
goto clean_up;
mcrypt_free(ctx);
}
- if (fout)
+ if (file_out)
{
- fclose(fout);
+ fclose(file_out);
}
- if (fin)
+ if (file_in)
{
- fclose(fin);
+ fclose(file_in);
}
return result;
FPRINTF(stderr, T("MCrypt Utility (%") T(PRIstr) T("-%") T(PRIstr) T("), by LoRd_MuldeR <MuldeR2@GMX.de>\n"), OS_TYPE, CPU_ARCH);
FPRINTF(stderr, T("Using libMCrypt v%") T(PRIstr) T(" [%") T(PRIstr) T("]\n\n"), LIBMCRYPT_VERSION, LIBMCRYPT_BUILDNO);
- if ((argc > 1) && (!STRICMP(argv[1U], T("--self-test"))))
+ if ((argc > 1) && ((!STRICMP(argv[1U], T("-t"))) || (!STRICMP(argv[1U], T("--self-test")))))
{
return self_test(); /*only self-test!*/
}
const clock_t clk_start = clock();
int result = -1;
- if (!STRICMP(command, T("--encrypt")))
+ if ((!STRICMP(command, T("-e"))) || (!STRICMP(command, T("--encrypt"))))
{
result = encrypt(passphrase_buffer, input_file, output_file);
}
- else if (!STRICMP(command, T("--decrypt")))
+ else if ((!STRICMP(command, T("-d"))) || (!STRICMP(command, T("--decrypt"))))
{
result = decrypt(passphrase_buffer, input_file, output_file);
}
goto exiting;
}
- FPUTS(T("--------\n\n"), stderr);
- fflush(stderr);
-
- const clock_t clk_end = clock();
- FPRINTF(stderr, T("Operation completed after %.1f seconds.\n\n"), (clk_end - clk_start) / ((double)CLOCKS_PER_SEC));
+ if (!g_interrupted)
+ {
+ FPUTS(T("--------\n\n"), stderr);
+ fflush(stderr);
+ const clock_t clk_end = clock();
+ FPRINTF(stderr, T("Operation completed after %.1f seconds.\n\n"), (clk_end - clk_start) / ((double)CLOCKS_PER_SEC));
+ }
exiting: