From 8aa378f8af74b93790e48c5cdc64fd139da1ff96 Mon Sep 17 00:00:00 2001 From: Robert van Herk Date: Fri, 30 Mar 2012 10:38:35 +0200 Subject: msi: fixed MsiGetFileHashW for empty files (try 2) In Wine, MsiGetFileHashW fails on empty files. However, in Windows it just generates a 0 hash. A patch + test is provided. Try 2's whitespacing is consistent with the rest of the files. modified: dlls/msi/msi.c modified: dlls/msi/tests/msi.c --- dlls/msi/msi.c | 34 ++++++++++++++++++++++------------ dlls/msi/tests/msi.c | 42 +++++++++++++++++++++++++++++++++++++++++- 2 files changed, 63 insertions(+), 13 deletions(-) diff --git a/dlls/msi/msi.c b/dlls/msi/msi.c index 53ae641..99d7c68 100644 --- a/dlls/msi/msi.c +++ b/dlls/msi/msi.c @@ -4014,24 +4014,34 @@ UINT WINAPI MsiGetFileHashW( LPCWSTR szFilePath, DWORD dwOptions, } length = GetFileSize( handle, NULL ); - mapping = CreateFileMappingW( handle, NULL, PAGE_READONLY, 0, 0, NULL ); - if (mapping) + if (length) { - p = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, length ); - if (p) + mapping = CreateFileMappingW( handle, NULL, PAGE_READONLY, 0, 0, NULL ); + if (mapping) { - MD5_CTX ctx; + p = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, length ); + if (p) + { + MD5_CTX ctx; - MD5Init( &ctx ); - MD5Update( &ctx, p, length ); - MD5Final( &ctx ); - UnmapViewOfFile( p ); + MD5Init( &ctx ); + MD5Update( &ctx, p, length ); + MD5Final( &ctx ); + UnmapViewOfFile( p ); - memcpy( pHash->dwData, ctx.digest, sizeof pHash->dwData ); - r = ERROR_SUCCESS; + memcpy( pHash->dwData, ctx.digest, sizeof pHash->dwData ); + r = ERROR_SUCCESS; + } + CloseHandle( mapping ); } - CloseHandle( mapping ); } + else + { + /* Empty file -> set hash to 0 */ + memset( pHash->dwData, 0, sizeof pHash->dwData ); + r = ERROR_SUCCESS; + } + CloseHandle( handle ); return r; diff --git a/dlls/msi/tests/msi.c b/dlls/msi/tests/msi.c index aaf1f42..d508ca8 100644 --- a/dlls/msi/tests/msi.c +++ b/dlls/msi/tests/msi.c @@ -493,6 +493,45 @@ static void test_MsiGetFileHash(void) } } +static void test_MsiGetFileHashForEmptyFile(void) { + char path[MAX_PATH]; + MSIFILEHASHINFO hi; + char file[MAX_PATH]; + UINT res; + + /* Create empty temp file */ + GetTempPath( + MAX_PATH, + path + ); + res = GetTempFileName( + path, + "tmp", + 0, + file + ); + + ok (res, "Could not create temporary file.\n"); + + if (res) + { + /*Get the hash of the empty temp file */ + + hi.dwFileHashInfoSize = sizeof(MSIFILEHASHINFO); + /* Fill hash with some random data */ + hi.dwData[0] = 15; + hi.dwData[1] = 25; + hi.dwData[2] = 35; + hi.dwData[3] = 45; + + res = MsiGetFileHash(file, 0, &hi); + ok(res == ERROR_SUCCESS, "MsiGetFileHash failed for empty file.\n"); + ok(hi.dwData[0] == 0 && hi.dwData[1] == 0 && hi.dwData[2] == 0 && hi.dwData[3] == 0, + "MsiGetFileHash of emptyfile should generate hashcode 0." + ); + } +} + /* copied from dlls/msi/registry.c */ static BOOL squash_guid(LPCWSTR in, LPWSTR out) { @@ -11964,7 +12003,8 @@ START_TEST(msi) test_null(); test_getcomponentpath(); test_MsiGetFileHash(); - + test_MsiGetFileHashForEmptyFile(); + if (!pConvertSidToStringSidA) win_skip("ConvertSidToStringSidA not implemented\n"); else -- 1.7.7.6