Skip to content

Commit

Permalink
fix path handling for printing hashes
Browse files Browse the repository at this point in the history
  • Loading branch information
cwansart committed Oct 28, 2024
1 parent ad2cffc commit 5e13d65
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 15 deletions.
13 changes: 12 additions & 1 deletion main.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "sha256sum.h"
#include "shlwapi.h"
#include "pathcch.h"

#define MAJOR_VERSION 2
#define MINOR_VERSION 0
Expand Down Expand Up @@ -47,6 +48,16 @@ int run(int argc, LPWSTR argv[])
{
WIN32_FIND_DATA findFileData;
HANDLE hFind = FindFirstFile(current->file, &findFileData);

//PathCchRemoveFileSpec(current->file, MAX_PATH);
//PathRemoveFileSpec(current->file);
//TCHAR msg[MAX_PATH];
//wsprintfW(msg, L"%ls ", current->file);
//WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE), msg, lstrlenW(msg), NULL, NULL);

//return 0;
//wsprintf(L"%ls ", current->file);


if (hFind == INVALID_HANDLE_VALUE)
{
Expand All @@ -56,7 +67,7 @@ int run(int argc, LPWSTR argv[])

do
{
ErrorCode printHashStatus = PrintHash(&args, current->file);
ErrorCode printHashStatus = PrintHash(&args, current->file, findFileData.cFileName);
if (printHashStatus != SUCCESS)
{
FindClose(hFind);
Expand Down
116 changes: 104 additions & 12 deletions sha256.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#pragma comment(lib, "bcrypt.lib")

#include "sha256sum.h"
#include "pathcch.h"
#include "shlwapi.h"
#include "strsafe.h"
#include <bcrypt.h>

#define NT_SUCCESS(Status) (((NTSTATUS)(Status)) >= 0)
Expand Down Expand Up @@ -227,27 +230,116 @@ ErrorCode CalcHash(__in Args* args, __out LPWSTR* file_hash, __in LPWSTR file)
return status;
}

ErrorCode PrintHash(__in Args* args, __in LPWSTR file)
//void print(LPWSTR key, LPWSTR value)
//{
// TCHAR msg[MAX_PATH];
// wsprintfW(msg, L"%ls: %ls\n", key, value);
// WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE), msg, lstrlenW(msg), NULL, NULL);
//}

TCHAR getPathSeparator(LPWSTR filePath, size_t filePathLen)
{
LPWSTR hash = NULL;
LPWSTR absPath = malloc((MAX_PATH + 1) * sizeof(wchar_t));
ErrorCode ok = SUCCESS;
int lastSlashIndex = -1;

// Find the index of the last slash or backslash
for (int i = 0; i < filePathLen; i++)
{
if (filePath[i] == L'/' || filePath[i] == L'\\')
{
return filePath[i];
}
}
return L'\\'; // return windows default if none found
}

BOOL getPathWithoutFileName(LPWSTR result, size_t resultLen, LPWSTR filePath, size_t filePathLen)
{
int lastSlashIndex = -1;

// Find the index of the last slash or backslash
for (int i = 0; i < filePathLen; i++)
{
if (filePath[i] == L'/' || filePath[i] == L'\\')
{
lastSlashIndex = i;
}
}

// If no path separator was found this function fails
if (lastSlashIndex == -1)
{
return FALSE;
}
else
{
memcpy(result, filePath, (lastSlashIndex + 1) * sizeof(TCHAR));
result[lastSlashIndex] = L'\0';
}
return TRUE;
}

if (GetFullPathNameW(file, MAX_PATH, absPath, NULL) == 0 || absPath == NULL)
ErrorCode PrintHash(__in Args* args, __in LPWSTR userInputFilePath, __in LPWSTR fileName)
{
// get full path from user input path, remove the file and append fileName so we get
// a clean absolute file path
TCHAR absPath[MAX_PATH];
if (GetFullPathName(userInputFilePath, MAX_PATH, absPath, NULL) == 0 || absPath == NULL)
{
ok = PARSE_ARGS_ALLOCATE_ERROR;
return PARSE_ARGS_ALLOCATE_ERROR; // TODO: add new error
}

if (ok == SUCCESS)
PathCchRemoveFileSpec(absPath, MAX_PATH);

TCHAR absFilePath[MAX_PATH];
PathCchCombine(absFilePath, MAX_PATH, absPath, fileName);

// now calculate the file hash using the absolute file path we just constructed
LPWSTR hash = NULL;
ErrorCode ok = CalcHash(args, &hash, absFilePath);
if (hash != NULL && ok == SUCCESS)
{
ErrorCode ok = CalcHash(args, &hash, absPath);
if (hash != NULL && ok == SUCCESS)
// depending whether it is a relative or an absolute path the output needs to be different to
// immitade the output of sha256sum from Linux
BOOL isRel = PathIsRelativeW(userInputFilePath);
if (isRel == TRUE)
{
size_t userInputFilePathLen;
StringCchLengthW(userInputFilePath, MAX_PATH, &userInputFilePathLen);
TCHAR inputPath[MAX_PATH];
BOOL containsPath = getPathWithoutFileName(inputPath, MAX_PATH, userInputFilePath, userInputFilePathLen);

// if the user passed a relative file without a .\ or ..\ and other prefixes
if (containsPath == FALSE)
{
wprintf(L"%ls *%ls\n", hash, fileName);
}
// if the user passed a relative file with .\, ..\ and so on, we
// need to concatenate the inputFilePath and the given fileName
else
{
TCHAR inputFilePath[MAX_PATH];
StringCchCopyW(inputFilePath, MAX_PATH, inputPath);

TCHAR separator = getPathSeparator(userInputFilePath, userInputFilePathLen);
if (FAILED(StringCchCatW(inputFilePath, MAX_PATH, &separator)))
{
return PARSE_ARGS_ALLOCATE_ERROR; // TODO: add new error
}

if (FAILED(StringCchCatW(inputFilePath, MAX_PATH, fileName)))
{
return PARSE_ARGS_ALLOCATE_ERROR; // TODO: add new error
}
wprintf(L"%ls *%ls\n", hash, inputFilePath);
}
}
// in case of an absolute path the absolute path shall be used
else
{
wprintf(L"%ls *%ls\n", hash, file);
wprintf(L"%ls *%ls\n", hash, absFilePath);
}
}
free(absPath);
return ok;
return SUCCESS;
}

ErrorCode ParseLine(__in Args* args, __out FileHash* fh, __in int line_num, __in LPWSTR line)
Expand Down
2 changes: 1 addition & 1 deletion sha256sum.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ extern "C" {
ErrorCode ParseArgs(__out Args*, __in int, __in LPWSTR[]);

ErrorCode CalcHash(__in Args*, __out LPWSTR*, __in LPWSTR);
ErrorCode PrintHash(__in Args*, __in LPWSTR);
ErrorCode PrintHash(__in Args*, __in LPWSTR, __in LPWSTR);
ErrorCode VerifyChecksums(__in Args*);

#ifdef __cplusplus
Expand Down
2 changes: 1 addition & 1 deletion sha256sum.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalDependencies>$(CoreLibraryDependencies);%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalDependencies>$(CoreLibraryDependencies);%(AdditionalDependencies);Shlwapi.lib;Pathcch.lib</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
Expand Down

0 comments on commit 5e13d65

Please sign in to comment.