crypt32: Use ReadFile rather than a memory-mapped file to check the type of a file.

This commit is contained in:
Juan Lang 2008-08-01 10:18:36 -07:00 committed by Alexandre Julliard
parent c03dfb8f50
commit 3f563c3d9b
1 changed files with 24 additions and 29 deletions

View File

@ -1,6 +1,6 @@
/* /*
* Copyright 2002 Mike McCormack for CodeWeavers * Copyright 2002 Mike McCormack for CodeWeavers
* Copyright 2005 Juan Lang * Copyright 2005-2008 Juan Lang
* Copyright 2006 Paul Vriens * Copyright 2006 Paul Vriens
* *
* This library is free software; you can redistribute it and/or * This library is free software; you can redistribute it and/or
@ -276,15 +276,15 @@ BOOL WINAPI CryptSIPRetrieveSubjectGuid
(LPCWSTR FileName, HANDLE hFileIn, GUID *pgSubject) (LPCWSTR FileName, HANDLE hFileIn, GUID *pgSubject)
{ {
HANDLE hFile; HANDLE hFile;
HANDLE hFilemapped;
LPVOID pMapped;
BOOL bRet = FALSE; BOOL bRet = FALSE;
DWORD fileSize; DWORD count;
IMAGE_DOS_HEADER *dos; LARGE_INTEGER zero, oldPos;
/* FIXME, find out if there is a name for this GUID */ /* 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 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 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' }; 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); TRACE("(%s %p %p)\n", wine_dbgstr_w(FileName), hFileIn, pgSubject);
@ -307,38 +307,33 @@ BOOL WINAPI CryptSIPRetrieveSubjectGuid
if (hFile == INVALID_HANDLE_VALUE) return FALSE; if (hFile == INVALID_HANDLE_VALUE) return FALSE;
} }
hFilemapped = CreateFileMappingA(hFile, NULL, PAGE_READONLY, 0, 0, NULL); zero.QuadPart = 0;
/* Last error is set by CreateFileMapping */ SetFilePointerEx(hFile, zero, &oldPos, FILE_CURRENT);
if (!hFilemapped) goto cleanup3; SetFilePointer(hFile, 0, NULL, FILE_BEGIN);
if (!ReadFile(hFile, hdr, sizeof(hdr), &count, NULL))
goto cleanup;
pMapped = MapViewOfFile(hFilemapped, FILE_MAP_READ, 0, 0, 0); if (count < SIP_MAX_MAGIC_NUMBER)
/* Last error is set by MapViewOfFile */
if (!pMapped) goto cleanup2;
/* Native checks it right here */
fileSize = GetFileSize(hFile, NULL);
if (fileSize < 4)
{ {
SetLastError(ERROR_INVALID_PARAMETER); SetLastError(ERROR_INVALID_PARAMETER);
goto cleanup1; goto cleanup;
} }
/* As everything is in place now we start looking at the file header */ /* As everything is in place now we start looking at the file header */
dos = (IMAGE_DOS_HEADER *)pMapped; if (!memcmp(hdr, &dosHdr, sizeof(dosHdr)))
if (dos->e_magic == IMAGE_DOS_SIGNATURE)
{ {
*pgSubject = unknown; *pgSubject = unknown;
SetLastError(S_OK); SetLastError(S_OK);
bRet = TRUE; bRet = TRUE;
goto cleanup1; goto cleanup;
} }
/* Quick-n-dirty check for a cab file. */ /* Quick-n-dirty check for a cab file. */
if (!memcmp(pMapped, cabHdr, sizeof(cabHdr))) if (!memcmp(hdr, cabHdr, sizeof(cabHdr)))
{ {
*pgSubject = cabGUID; *pgSubject = cabGUID;
SetLastError(S_OK); SetLastError(S_OK);
bRet = TRUE; bRet = TRUE;
goto cleanup1; goto cleanup;
} }
/* FIXME /* FIXME
@ -352,14 +347,14 @@ BOOL WINAPI CryptSIPRetrieveSubjectGuid
/* Let's set the most common error for now */ /* Let's set the most common error for now */
SetLastError(TRUST_E_SUBJECT_FORM_UNKNOWN); SetLastError(TRUST_E_SUBJECT_FORM_UNKNOWN);
/* The 3 different cleanups are here because we shouldn't overwrite the last error */ cleanup:
cleanup1: /* If we didn't open this one we shouldn't close it (hFile is a copy),
UnmapViewOfFile(pMapped); * but we should reset the file pointer to its original position.
cleanup2: */
CloseHandle(hFilemapped); if (!hFileIn)
cleanup3: CloseHandle(hFile);
/* If we didn't open this one we shouldn't close it (hFile is a copy) */ else
if (!hFileIn) CloseHandle(hFile); SetFilePointerEx(hFile, oldPos, NULL, FILE_BEGIN);
return bRet; return bRet;
} }