From 605db9fe7d3de4dab867117a98cc7bbb82d26021 Mon Sep 17 00:00:00 2001 From: Juan Lang Date: Fri, 1 Aug 2008 10:18:36 -0700 Subject: [PATCH] Use ReadFile rather than a memory-mapped file to check the type of a file --- dlls/crypt32/sip.c | 53 +++++++++++++++++++++++++--------------------------- 1 files changed, 25 insertions(+), 28 deletions(-) diff --git a/dlls/crypt32/sip.c b/dlls/crypt32/sip.c index 171165a..86eb48e 100644 --- a/dlls/crypt32/sip.c +++ b/dlls/crypt32/sip.c @@ -1,6 +1,6 @@ /* * Copyright 2002 Mike McCormack for CodeWeavers - * Copyright 2005 Juan Lang + * Copyright 2005-2008 Juan Lang * Copyright 2006 Paul Vriens * * This library is free software; you can redistribute it and/or @@ -276,15 +276,15 @@ BOOL WINAPI CryptSIPRetrieveSubjectGuid (LPCWSTR FileName, HANDLE hFileIn, GUID *pgSubject) { HANDLE hFile; - HANDLE hFilemapped; - LPVOID pMapped; BOOL bRet = FALSE; - DWORD fileSize; - IMAGE_DOS_HEADER *dos; + DWORD fileSize, count; + LARGE_INTEGER zero, oldPos; /* FIXME, find out if there is a name for this GUID */ static const GUID unknown = { 0xC689AAB8, 0x8E78, 0x11D0, { 0x8C,0x47,0x00,0xC0,0x4F,0xC2,0x95,0xEE }}; static const GUID cabGUID = { 0xc689aaba, 0x8e78, 0x11d0, {0x8c,0x47,0x00,0xc0,0x4f,0xc2,0x95,0xee }}; + static const WORD dosHdr = IMAGE_DOS_SIGNATURE; static const BYTE cabHdr[] = { 'M','S','C','F' }; + BYTE hdr[SIP_MAX_MAGIC_NUMBER]; TRACE("(%s %p %p)\n", wine_dbgstr_w(FileName), hFileIn, pgSubject); @@ -307,38 +307,35 @@ BOOL WINAPI CryptSIPRetrieveSubjectGuid if (hFile == INVALID_HANDLE_VALUE) return FALSE; } - hFilemapped = CreateFileMappingA(hFile, NULL, PAGE_READONLY, 0, 0, NULL); - /* Last error is set by CreateFileMapping */ - if (!hFilemapped) goto cleanup3; - - pMapped = MapViewOfFile(hFilemapped, FILE_MAP_READ, 0, 0, 0); - /* Last error is set by MapViewOfFile */ - if (!pMapped) goto cleanup2; - /* Native checks it right here */ fileSize = GetFileSize(hFile, NULL); - if (fileSize < 4) + if (fileSize < SIP_MAX_MAGIC_NUMBER) { SetLastError(ERROR_INVALID_PARAMETER); - goto cleanup1; + goto cleanup; } + zero.QuadPart = 0; + SetFilePointerEx(hFile, zero, &oldPos, FILE_CURRENT); + SetFilePointer(hFile, 0, NULL, FILE_BEGIN); + if (!ReadFile(hFile, hdr, sizeof(hdr), &count, NULL)) + goto cleanup; + /* As everything is in place now we start looking at the file header */ - dos = (IMAGE_DOS_HEADER *)pMapped; - if (dos->e_magic == IMAGE_DOS_SIGNATURE) + if (!memcmp(hdr, &dosHdr, sizeof(dosHdr))) { *pgSubject = unknown; SetLastError(S_OK); bRet = TRUE; - goto cleanup1; + goto cleanup; } /* Quick-n-dirty check for a cab file. */ - if (!memcmp(pMapped, cabHdr, sizeof(cabHdr))) + if (!memcmp(hdr, cabHdr, sizeof(cabHdr))) { *pgSubject = cabGUID; SetLastError(S_OK); bRet = TRUE; - goto cleanup1; + goto cleanup; } /* FIXME @@ -352,14 +349,14 @@ BOOL WINAPI CryptSIPRetrieveSubjectGuid /* Let's set the most common error for now */ SetLastError(TRUST_E_SUBJECT_FORM_UNKNOWN); - /* The 3 different cleanups are here because we shouldn't overwrite the last error */ -cleanup1: - UnmapViewOfFile(pMapped); -cleanup2: - CloseHandle(hFilemapped); -cleanup3: - /* If we didn't open this one we shouldn't close it (hFile is a copy) */ - if (!hFileIn) CloseHandle(hFile); +cleanup: + /* If we didn't open this one we shouldn't close it (hFile is a copy), + * but we should reset the file pointer to its original position. + */ + if (!hFileIn) + CloseHandle(hFile); + else + SetFilePointerEx(hFile, oldPos, NULL, FILE_BEGIN); return bRet; } -- 1.4.1