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

Alexandre Julliard julliard at winehq.org
Mon Aug 4 08:53:30 CDT 2008


Module: wine
Branch: master
Commit: 3f563c3d9bdf8f31615382fff6b9d44c4cb49fdb
URL:    http://source.winehq.org/git/wine.git/?a=commit;h=3f563c3d9bdf8f31615382fff6b9d44c4cb49fdb

Author: Juan Lang <juan.lang at gmail.com>
Date:   Fri Aug  1 10:18:36 2008 -0700

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

---

 dlls/crypt32/sip.c |   53 +++++++++++++++++++++++----------------------------
 1 files changed, 24 insertions(+), 29 deletions(-)

diff --git a/dlls/crypt32/sip.c b/dlls/crypt32/sip.c
index 171165a..8668f5b 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  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,33 @@ 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;
+    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;
 
-    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 (count < SIP_MAX_MAGIC_NUMBER)
     {
         SetLastError(ERROR_INVALID_PARAMETER);
-        goto cleanup1;
+        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 +347,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;
 }




More information about the wine-cvs mailing list