OSDN Git Service

Make it possible to read the passphrase from STDIN.
authorLoRd_MuldeR <mulder2@gmx.de>
Fri, 16 Oct 2020 17:33:12 +0000 (19:33 +0200)
committerLoRd_MuldeR <mulder2@gmx.de>
Sat, 20 Mar 2021 20:18:22 +0000 (21:18 +0100)
frontend/src/main.c
frontend/src/utils.c
libMCrypt/include/mcrypt.h

index f1594a2..992a95d 100644 (file)
@@ -22,24 +22,28 @@ static volatile int g_interrupted = 0;
 
 static char* read_passphrase(const CHR* const file_name)
 {
-       const size_t buff_size = 1024U;
-       char *const buffer = (char*) malloc(buff_size * sizeof(char));
+       static const size_t buff_size = 512U;
+       char *buffer = (char*) malloc(buff_size * sizeof(char));
        if (!buffer)
        {
                return NULL;
        }
-       FILE *const file = FOPEN(file_name, T("rb"));
+
+       const int use_stdin = (STRICMP(file_name, T("-")) == 0);
+       FILE *const file = use_stdin ? stdin : FOPEN(file_name, T("rb"));
        if (!file)
        {
+               free(buffer);
                return NULL;
        }
+
        do
        {
                if (!fgets(buffer, (int)buff_size, file))
                {
-                       fclose(file);
                        free(buffer);
-                       return NULL;
+                       buffer = NULL;
+                       goto finish;
                }
                size_t length = strlen(buffer);
                while ((length > 0U) && ((buffer[length - 1U] == '\r') || (buffer[length - 1U] == '\n')))
@@ -48,7 +52,14 @@ static char* read_passphrase(const CHR* const file_name)
                }
        }
        while (!buffer[0U]);
-       fclose(file);
+
+finish:
+
+       if ((!use_stdin) && file)
+       {
+               fclose(file);
+       }
+
        return buffer;
 }
 
@@ -505,16 +516,17 @@ int MAIN(int argc, CHR* argv[])
                        FPUTS(T("This software has been released under the CC0 1.0 Universal license:\n"), stderr);
                        FPUTS(T("https://creativecommons.org/publicdomain/zero/1.0/legalcode\n"), stderr);
                        FPUTS(T("====================================================================\n\n"), stderr);
-                       FPUTS(T("Usage:\n"), stderr);
+                       FPUTS(T("Synopsis:\n"), stderr);
                        FPRINTF(stderr, T("  %") T(PRISTR) T(" --encrypt [[@][:]<passphrase>] <input.txt> <output.enc>\n"), program);
                        FPRINTF(stderr, T("  %") T(PRISTR) T(" --decrypt [[@][:]<passphrase>] <input.enc> <output.txt>\n\n"), program);
-                       FPUTS(T("Notes:\n"), stderr);
+                       FPUTS(T("Remarks:\n"), stderr);
                        FPUTS(T("- If <passphrase> is prefixed with a '@' character, then it specifies the file\n"), stderr);
                        FPUTS(T("  to read the passphrase from; only the first line in that file is used!\n"), stderr);
                        FPUTS(T("- If <passphrase> is prefixed with a ':' character, then the leading character\n"), stderr);
                        FPUTS(T("  is skipped and the remainder of the argument is used as passphrase.\n"), stderr);
                        FPUTS(T("- If the argument <passphrase> is *not* present, then the environment variable\n"), stderr);
-                       FPRINTF(stderr, T("  \"%") T(PRISTR) T("\" must be set and it specifies the passphrase to be used.\n\n"), ENVV_PASSWD_NAME);
+                       FPRINTF(stderr, T("  \"%") T(PRISTR) T("\" must be set; it specifies the passphrase to be used.\n"), ENVV_PASSWD_NAME);
+                       FPUTS(T("- Specify \"@-\" in order to read the passphrase from the standard input stream!\n\n"), stderr);
                        return 0;
                }
                if ((!STRICMP(argv[1U], T("-t"))) || (!STRICMP(argv[1U], T("--self-test"))))
index dd16004..08e8b2e 100644 (file)
@@ -41,6 +41,7 @@ void init_terminal(void)
 {
 #ifdef _WIN32
        SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOGPFAULTERRORBOX);
+       _setmode(_fileno(stdin),  _O_BINARY);
        _setmode(_fileno(stderr), _O_U8TEXT);
        if (_acmdln) SecureZeroMemory(_acmdln, strlen(_acmdln) * sizeof(char));
        if (_wcmdln) SecureZeroMemory(_wcmdln, wcslen(_wcmdln) * sizeof(wchar_t));
index 0e34b02..afcf8f0 100644 (file)
@@ -9,6 +9,9 @@
 #include <stdlib.h>
 #include <stdint.h>
 
+/*
+ * Version info
+ */
 extern const char *const LIBMCRYPT_VERSION;
 extern const char* const LIBMCRYPT_BUILDNO;