From d05354c42b33d26b9be1141ca74a0369e5cc937f Mon Sep 17 00:00:00 2001 From: Mounir IDRASSI Date: Fri, 18 Sep 2015 10:32:11 +0200 Subject: [PATCH] Add new switches -overwrite and -quiet. Add text coloring for logo text, error messages and hash result output. --- DirHash.cpp | 122 +++++++++++++++++++++++++++++++++++++++------------- DirHash.rc | 8 ++-- 2 files changed, 96 insertions(+), 34 deletions(-) diff --git a/DirHash.cpp b/DirHash.cpp index f70136d..3793b00 100644 --- a/DirHash.cpp +++ b/DirHash.cpp @@ -30,6 +30,7 @@ #include #include #include +#include #include #include #include @@ -40,6 +41,8 @@ using namespace std; static BYTE g_pbBuffer[4096]; static TCHAR g_szCanonalizedName[MAX_PATH + 1]; +static WORD g_wAttributes = FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED; +static HANDLE g_hConsole = NULL; // Used for sorting directory content bool compare_nocase (LPCWSTR first, LPCWSTR second) @@ -335,9 +338,27 @@ DWORD HashDirectory(LPCTSTR szDirPath, Hash* pHash, bool bIncludeNames, list excludeSpecList; + g_hConsole = GetStdHandle(STD_OUTPUT_HANDLE); + CONSOLE_SCREEN_BUFFER_INFO originalConsoleInfo; - setbuf (stdout, NULL); + // get original console attributes + if (GetConsoleScreenBufferInfo(g_hConsole, &originalConsoleInfo)) + g_wAttributes = originalConsoleInfo.wAttributes; - SetConsoleTitle(_T("DirHash by Mounir IDRASSI (mounir@idrix.fr) Copyright 2010-2015")); + setbuf (stdout, NULL); - _tprintf(_T("\nDirHash by Mounir IDRASSI (mounir@idrix.fr) Copyright 2010-2015\n\nRecursively compute hash of a given directory content in lexicographical order.\nIt can also compute the hash of a single file.\n\nSupported Algorithms : MD5, SHA1, SHA256, SHA384, SHA512\nUsing OpenSSL\n\n")); + SetConsoleTitle(_T("DirHash by Mounir IDRASSI (mounir@idrix.fr) Copyright 2010-2015")); if (argc < 2) { @@ -383,27 +411,29 @@ int _tmain(int argc, _TCHAR* argv[]) { if ((i + 1) >= argc) { - // missing file argument - _tprintf(_T("Error: Missing argument for switch -t\n\n")); + // missing file argument ShowUsage(); - WaitForExit(); + ShowError(_T("Error: Missing argument for switch -t\n")); + WaitForExit(bDontWait); return 1; } - outputFile = _tfopen(argv[i + 1], _T("a+t")); - if (!outputFile) - { - _tprintf(_T("Failed to open the result file for writing!\n")); - WaitForExit(); - return 1; - } + outputFileName = argv[i + 1]; i++; } + else if (_tcscmp(argv[i],_T("-overwrite")) == 0) + { + bOverwrite = true; + } else if (_tcscmp(argv[i],_T("-nowait")) == 0) { bDontWait = true; } + else if (_tcscmp(argv[i],_T("-quiet")) == 0) + { + bQuiet = true; + } else if (_tcscmp(argv[i],_T("-hashnames")) == 0) { bIncludeNames = true; @@ -412,10 +442,10 @@ int _tmain(int argc, _TCHAR* argv[]) { if ((i + 1) >= argc) { - // missing file argument - _tprintf(_T("Error: Missing argument for switch -exclude\n\n")); + // missing file argument ShowUsage(); - WaitForExit(); + ShowError(_T("Error: Missing argument for switch -exclude\n")); + WaitForExit(bDontWait); return 1; } @@ -428,10 +458,10 @@ int _tmain(int argc, _TCHAR* argv[]) pHash = Hash::GetHash(argv[i]); if (!pHash) { - if (outputFile) fclose(outputFile); - _tprintf(_T("Error: Argument \"%s\" not recognized\n\n"), argv[i]); + if (outputFile) fclose(outputFile); ShowUsage(); - WaitForExit(); + ShowError(_T("Error: Argument \"%s\" not recognized\n"), argv[i]); + WaitForExit(bDontWait); return 1; } } @@ -441,6 +471,9 @@ int _tmain(int argc, _TCHAR* argv[]) if (!pHash) pHash = new Sha1(); + if (!bQuiet) + ShowLogo(); + // Check that the input path plus 3 is not longer than MAX_PATH. // Three characters are for the "\*" plus NULL appended below. @@ -450,7 +483,8 @@ int _tmain(int argc, _TCHAR* argv[]) { if (outputFile) fclose(outputFile); delete pHash; - _tprintf(TEXT("\nError: Input directory/file path is too long. Maximum length is %d characters\n"), MAX_PATH); + if (!bQuiet) + ShowError(TEXT("Error: Input directory/file path is too long. Maximum length is %d characters\n"), MAX_PATH); WaitForExit(bDontWait); return (-1); } @@ -458,15 +492,19 @@ int _tmain(int argc, _TCHAR* argv[]) { if (outputFile) fclose(outputFile); delete pHash; - _tprintf(TEXT("Error: The given input file doesn't exist\n")); + if (!bQuiet) + ShowError(TEXT("Error: The given input file doesn't exist\n")); WaitForExit(bDontWait); return (-2); } - _tprintf(_T("Using %s to compute hash of \"%s\" ...\n"), + if (!bQuiet) + { + _tprintf(_T("Using %s to compute hash of \"%s\" ...\n"), pHash->GetID(), PathFindFileName(argv[1])); - fflush(stdout); + fflush(stdout); + } if (PathIsDirectory(argv[1])) { @@ -492,19 +530,43 @@ int _tmain(int argc, _TCHAR* argv[]) if (dwError == NO_ERROR) { pHash->Final(pbDigest); - if (outputFile) + + if (!outputFileName.empty()) + { + outputFile = _tfopen(outputFileName.c_str(), bOverwrite? _T("wt") : _T("a+t")); + if (!outputFile) + { + if (!bQuiet) + { + ShowError (_T("!!!Failed to open the result file for writing!!!\n")); + } + } + } + + if (!bQuiet) { - _ftprintf(outputFile, __T("%s hash of \"%s\" (%d bytes) = "), - pHash->GetID(), - PathFindFileName(argv[1]), - pHash->GetHashSize()); + if (outputFile) + { + _ftprintf(outputFile, __T("%s hash of \"%s\" (%d bytes) = "), + pHash->GetID(), + PathFindFileName(argv[1]), + pHash->GetHashSize()); + } + _tprintf(_T("%s (%d bytes) = "), pHash->GetID(), pHash->GetHashSize()); } - _tprintf(_T("%s (%d bytes) = "), pHash->GetID(), pHash->GetHashSize()); + + // display hash in yellow + SetConsoleTextAttribute (g_hConsole, FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY); + for (int i=0; i < pHash->GetHashSize(); i++) { _tprintf(_T("%.2X"), pbDigest[i]); if (outputFile) _ftprintf(outputFile, _T("%.2X"), pbDigest[i]); } + + // restore normal text color + SetConsoleTextAttribute (g_hConsole, g_wAttributes); + _tprintf(_T("\n")); if (outputFile) _ftprintf(outputFile, _T("\n")); } diff --git a/DirHash.rc b/DirHash.rc index 43b2dc2..2bb094a 100644 --- a/DirHash.rc +++ b/DirHash.rc @@ -53,8 +53,8 @@ END // VS_VERSION_INFO VERSIONINFO - FILEVERSION 1,4,0,0 - PRODUCTVERSION 1,4,0,0 + FILEVERSION 1,5,0,0 + PRODUCTVERSION 1,5,0,0 FILEFLAGSMASK 0x17L #ifdef _DEBUG FILEFLAGS 0x1L @@ -72,12 +72,12 @@ BEGIN VALUE "Comments", "By Mounir IDRASSI" VALUE "CompanyName", "IDRIX" VALUE "FileDescription", "compute Hash of dorectories and files" - VALUE "FileVersion", "1, 4, 0, 0" + VALUE "FileVersion", "1, 5, 0, 0" VALUE "InternalName", "DirHash" VALUE "LegalCopyright", "Copyright (C) 2015 IDRIX" VALUE "OriginalFilename", "DirHash.exe" VALUE "ProductName", "DirHash Application" - VALUE "ProductVersion", "1, 4, 0, 0" + VALUE "ProductVersion", "1, 5, 0, 0" END END BLOCK "VarFileInfo"