Author: cwittich
Date: Wed Apr 15 13:17:12 2009
New Revision: 40520

URL: http://svn.reactos.org/svn/reactos?rev=40520&view=rev
Log:
sync wininet urlcache.c with wine 1.1.19 - fixes memory corruptions

Modified:
    trunk/reactos/dll/win32/wininet/urlcache.c

Modified: trunk/reactos/dll/win32/wininet/urlcache.c
URL: 
http://svn.reactos.org/svn/reactos/trunk/reactos/dll/win32/wininet/urlcache.c?rev=40520&r1=40519&r2=40520&view=diff
==============================================================================
--- trunk/reactos/dll/win32/wininet/urlcache.c [iso-8859-1] (original)
+++ trunk/reactos/dll/win32/wininet/urlcache.c [iso-8859-1] Wed Apr 15 13:17:12 
2009
@@ -2,7 +2,7 @@
  * Wininet - Url Cache functions
  *
  * Copyright 2001,2002 CodeWeavers
- * Copyright 2003 Robert Shearman
+ * Copyright 2003-2008 Robert Shearman
  *
  * Eric Kohl
  * Aric Stewart
@@ -25,14 +25,22 @@
 #include "config.h"
 #include "wine/port.h"
 
+#define NONAMELESSUNION
+#define NONAMELESSSTRUCT
+
+#if defined(__MINGW32__) || defined (_MSC_VER)
+#include <ws2tcpip.h>
+#endif
+
 #include <stdarg.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <sys/types.h>
+#ifdef HAVE_SYS_SOCKET_H
+# include <sys/socket.h>
+#endif
 #include <time.h>
-
-#define NONAMELESSUNION
-#define NONAMELESSSTRUCT
 
 #include "windef.h"
 #include "winbase.h"
@@ -178,7 +186,7 @@
 /* List of all containers available */
 static struct list UrlContainers = LIST_INIT(UrlContainers);
 
-static HASH_CACHEFILE_ENTRY *URLCache_CreateHashTable(LPURLCACHE_HEADER 
pHeader, HASH_CACHEFILE_ENTRY *pPrevHash);
+static DWORD URLCache_CreateHashTable(LPURLCACHE_HEADER pHeader, 
HASH_CACHEFILE_ENTRY *pPrevHash, HASH_CACHEFILE_ENTRY **ppHash);
 
 /***********************************************************************
  *           URLCache_PathToObjectName (Internal)
@@ -206,11 +214,11 @@
  *  Opens the index file and saves mapping handle in hCacheIndexMapping
  *
  * RETURNS
- *    TRUE if succeeded
- *    FALSE if failed
- *
- */
-static BOOL URLCacheContainer_OpenIndex(URLCACHECONTAINER * pContainer)
+ *    ERROR_SUCCESS if succeeded
+ *    Any other Win32 error code if failed
+ *
+ */
+static DWORD URLCacheContainer_OpenIndex(URLCACHECONTAINER * pContainer)
 {
     HANDLE hFile;
     WCHAR wszFilePath[MAX_PATH];
@@ -220,7 +228,7 @@
     static const WCHAR wszMappingFormat[] = 
{'%','s','%','s','_','%','l','u',0};
 
     if (pContainer->hMapping)
-        return TRUE;
+        return ERROR_SUCCESS;
 
     strcpyW(wszFilePath, pContainer->path);
     strcatW(wszFilePath, wszIndex);
@@ -235,7 +243,7 @@
     if (hFile == INVALID_HANDLE_VALUE)
     {
         TRACE("Could not open or create cache index file \"%s\"\n", 
debugstr_w(wszFilePath));
-        return FALSE;
+        return GetLastError();
     }
 
     /* At this stage we need the mutex because we may be about to create the
@@ -247,7 +255,7 @@
     if (dwFileSize == INVALID_FILE_SIZE)
     {
        ReleaseMutex(pContainer->hMutex);
-        return FALSE;
+        return GetLastError();
     }
 
     if (dwFileSize == 0)
@@ -256,7 +264,7 @@
        HKEY    key;
        char    achZeroes[0x1000];
        DWORD   dwOffset;
-       DWORD dwError = 0;
+       DWORD dwError = ERROR_SUCCESS;
 
        /* Write zeroes to the entire file so we can safely map it without
         * fear of getting a SEGV because the disk is full.
@@ -282,7 +290,7 @@
            }
        }
 
-       if (!dwError)
+       if (dwError == ERROR_SUCCESS)
        {
            HANDLE hMapping = CreateFileMappingW(hFile, NULL, PAGE_READWRITE, 
0, NEWFILE_SIZE, NULL);
 
@@ -296,6 +304,7 @@
                    WCHAR wszDirPath[MAX_PATH];
                    FILETIME ft;
                    int i, j;
+                    HASH_CACHEFILE_ENTRY *pHashEntry;
 
                    dwFileSize = NEWFILE_SIZE;
                
@@ -326,7 +335,7 @@
                        RegCloseKey(key);
                    }
                
-                   URLCache_CreateHashTable(pHeader, NULL);
+                   URLCache_CreateHashTable(pHeader, NULL, &pHashEntry);
 
                    /* Last step - create the directories */
        
@@ -416,8 +425,7 @@
            CloseHandle(hFile);
            DeleteFileW(wszFilePath);
            ReleaseMutex(pContainer->hMutex);
-           SetLastError(dwError);
-           return FALSE;
+           return dwError;
        }
 
     }
@@ -433,10 +441,10 @@
     if (!pContainer->hMapping)
     {
         ERR("Couldn't create file mapping (error is %d)\n", GetLastError());
-        return FALSE;
-    }
-
-    return TRUE;
+        return GetLastError();
+    }
+
+    return ERROR_SUCCESS;
 }
 
 /***********************************************************************
@@ -579,7 +587,7 @@
         );
 }
 
-static BOOL URLCacheContainers_FindContainerW(LPCWSTR lpwszUrl, 
URLCACHECONTAINER ** ppContainer)
+static DWORD URLCacheContainers_FindContainerW(LPCWSTR lpwszUrl, 
URLCACHECONTAINER ** ppContainer)
 {
     URLCACHECONTAINER * pContainer;
 
@@ -590,19 +598,18 @@
         int prefix_len = strlenW(pContainer->cache_prefix);
         if (!strncmpW(pContainer->cache_prefix, lpwszUrl, prefix_len))
         {
-            TRACE("found container with prefx %s for URL %s\n", 
debugstr_w(pContainer->cache_prefix), debugstr_w(lpwszUrl));
+            TRACE("found container with prefix %s for URL %s\n", 
debugstr_w(pContainer->cache_prefix), debugstr_w(lpwszUrl));
             *ppContainer = pContainer;
-            return TRUE;
+            return ERROR_SUCCESS;
         }
     }
     ERR("no container found\n");
-    SetLastError(ERROR_FILE_NOT_FOUND);
-    return FALSE;
-}
-
-static BOOL URLCacheContainers_FindContainerA(LPCSTR lpszUrl, 
URLCACHECONTAINER ** ppContainer)
-{
-    BOOL ret;
+    return ERROR_FILE_NOT_FOUND;
+}
+
+static DWORD URLCacheContainers_FindContainerA(LPCSTR lpszUrl, 
URLCACHECONTAINER ** ppContainer)
+{
+    DWORD ret;
     LPWSTR lpwszUrl;
     int url_len = MultiByteToWideChar(CP_ACP, 0, lpszUrl, -1, NULL, 0);
     if (url_len && (lpwszUrl = HeapAlloc(GetProcessHeap(), 0, url_len * 
sizeof(WCHAR))))
@@ -612,7 +619,7 @@
         HeapFree(GetProcessHeap(), 0, lpwszUrl);
         return ret;
     }
-    return FALSE;
+    return GetLastError();
 }
 
 static BOOL URLCacheContainers_Enum(LPCWSTR lpwszSearchPattern, DWORD dwIndex, 
URLCACHECONTAINER ** ppContainer)
@@ -654,12 +661,18 @@
 /***********************************************************************
  *           URLCacheContainer_LockIndex (Internal)
  *
+ * Locks the index for system-wide exclusive access.
+ *
+ * RETURNS
+ *  Cache file header if successful
+ *  NULL if failed and calls SetLastError.
  */
 static LPURLCACHE_HEADER URLCacheContainer_LockIndex(URLCACHECONTAINER * 
pContainer)
 {
     BYTE index;
     LPVOID pIndexData;
     URLCACHE_HEADER * pHeader;
+    DWORD error;
 
     /* acquire mutex */
     WaitForSingleObject(pContainer->hMutex, INFINITE);
@@ -670,7 +683,7 @@
     {
         ReleaseMutex(pContainer->hMutex);
         ERR("Couldn't MapViewOfFile. Error: %d\n", GetLastError());
-        return FALSE;
+        return NULL;
     }
     pHeader = (URLCACHE_HEADER *)pIndexData;
 
@@ -680,10 +693,12 @@
     if (pHeader->dwFileSize != pContainer->file_size)
     {
         URLCacheContainer_CloseIndex(pContainer);
-        if (!URLCacheContainer_OpenIndex(pContainer))
+        error = URLCacheContainer_OpenIndex(pContainer);
+        if (error != ERROR_SUCCESS)
         {
             ReleaseMutex(pContainer->hMutex);
-            return FALSE;
+            SetLastError(error);
+            return NULL;
         }
         pIndexData = MapViewOfFile(pContainer->hMapping, FILE_MAP_WRITE, 0, 0, 
0);
 
@@ -691,7 +706,7 @@
         {
             ReleaseMutex(pContainer->hMutex);
             ERR("Couldn't MapViewOfFile. Error: %d\n", GetLastError());
-            return FALSE;
+            return NULL;
         }
         pHeader = (URLCACHE_HEADER *)pIndexData;
     }
@@ -931,11 +946,11 @@
  *  Copies an entry from the cache index file to the Win32 structure
  *
  * RETURNS
- *    TRUE if the buffer was big enough
- *    FALSE if the buffer was too small
- *
- */
-static BOOL URLCache_CopyEntry(
+ *    ERROR_SUCCESS if the buffer was big enough
+ *    ERROR_INSUFFICIENT_BUFFER if the buffer was too small
+ *
+ */
+static DWORD URLCache_CopyEntry(
     URLCACHECONTAINER * pContainer,
     LPCURLCACHE_HEADER pHeader, 
     LPINTERNET_CACHE_ENTRY_INFOA lpCacheEntryInfo, 
@@ -983,9 +998,9 @@
         DWORD lenUrlBytes = (lenUrl+1) * (bUnicode ? sizeof(WCHAR) : 
sizeof(CHAR));
 
         lpCacheEntryInfo->lpszSourceUrlName = (LPSTR)lpCacheEntryInfo + 
dwRequiredSize - lenUrlBytes;
-         if (bUnicode)
-             MultiByteToWideChar(CP_ACP, 0, (LPSTR)pUrlEntry + 
pUrlEntry->dwOffsetUrl, -1, (LPWSTR)lpCacheEntryInfo->lpszSourceUrlName, lenUrl 
+ 1);
-         else
+        if (bUnicode)
+            MultiByteToWideChar(CP_ACP, 0, (LPSTR)pUrlEntry + 
pUrlEntry->dwOffsetUrl, -1, (LPWSTR)lpCacheEntryInfo->lpszSourceUrlName, lenUrl 
+ 1);
+        else
             memcpy(lpCacheEntryInfo->lpszSourceUrlName, (LPSTR)pUrlEntry + 
pUrlEntry->dwOffsetUrl, lenUrlBytes);
     }
 
@@ -1000,7 +1015,7 @@
         lpszLocalFileName = (LPSTR)lpCacheEntryInfo + dwRequiredSize;
         nLocalFilePathSize = *lpdwBufferSize - dwRequiredSize;
         if ((bUnicode && URLCache_LocalFileNameToPathW(pContainer, pHeader, 
(LPCSTR)pUrlEntry + pUrlEntry->dwOffsetLocalName, pUrlEntry->CacheDir, 
(LPWSTR)lpszLocalFileName, &nLocalFilePathSize)) ||
-            URLCache_LocalFileNameToPathA(pContainer, pHeader, 
(LPCSTR)pUrlEntry + pUrlEntry->dwOffsetLocalName, pUrlEntry->CacheDir, 
lpszLocalFileName, &nLocalFilePathSize))
+            (!bUnicode && URLCache_LocalFileNameToPathA(pContainer, pHeader, 
(LPCSTR)pUrlEntry + pUrlEntry->dwOffsetLocalName, pUrlEntry->CacheDir, 
lpszLocalFileName, &nLocalFilePathSize)))
         {
             lpCacheEntryInfo->lpszLocalFileName = lpszLocalFileName;
         }
@@ -1049,11 +1064,10 @@
     if (dwRequiredSize > *lpdwBufferSize)
     {
         *lpdwBufferSize = dwRequiredSize;
-        SetLastError(ERROR_INSUFFICIENT_BUFFER);
-        return FALSE;
+        return ERROR_INSUFFICIENT_BUFFER;
     }
     *lpdwBufferSize = dwRequiredSize;
-    return TRUE;
+    return ERROR_SUCCESS;
 }
 
 
@@ -1064,11 +1078,11 @@
  * according to the flags set by dwFieldControl.
  *
  * RETURNS
- *    TRUE if the buffer was big enough
- *    FALSE if the buffer was too small
- *
- */
-static BOOL URLCache_SetEntryInfo(URL_CACHEFILE_ENTRY * pUrlEntry, const 
INTERNET_CACHE_ENTRY_INFOW * lpCacheEntryInfo, DWORD dwFieldControl)
+ *    ERROR_SUCCESS if the buffer was big enough
+ *    ERROR_INSUFFICIENT_BUFFER if the buffer was too small
+ *
+ */
+static DWORD URLCache_SetEntryInfo(URL_CACHEFILE_ENTRY * pUrlEntry, const 
INTERNET_CACHE_ENTRY_INFOW * lpCacheEntryInfo, DWORD dwFieldControl)
 {
     if (dwFieldControl & CACHE_ENTRY_ACCTIME_FC)
         pUrlEntry->LastAccessTime = lpCacheEntryInfo->LastAccessTime;
@@ -1087,7 +1101,7 @@
     if (dwFieldControl & CACHE_ENTRY_SYNCTIME_FC)
         FileTimeToDosDateTime(&lpCacheEntryInfo->LastAccessTime, 
&pUrlEntry->wLastSyncDate, &pUrlEntry->wLastSyncTime);
 
-    return TRUE;
+    return ERROR_SUCCESS;
 }
 
 /***********************************************************************
@@ -1285,11 +1299,11 @@
  * key are entered into the hash table.
  *
  * RETURNS
- *    TRUE if the entry was added
- *    FALSE if the entry could not be added
- *
- */
-static BOOL URLCache_AddEntryToHash(LPURLCACHE_HEADER pHeader, LPCSTR lpszUrl, 
DWORD dwOffsetEntry)
+ *    ERROR_SUCCESS if the entry was added
+ *    Any other Win32 error code if the entry could not be added
+ *
+ */
+static DWORD URLCache_AddEntryToHash(LPURLCACHE_HEADER pHeader, LPCSTR 
lpszUrl, DWORD dwOffsetEntry)
 {
     /* see URLCache_FindEntryInHash for structure of hash tables */
 
@@ -1297,6 +1311,7 @@
     DWORD offset = (key % HASHTABLE_NUM_ENTRIES) * sizeof(struct _HASH_ENTRY);
     HASH_CACHEFILE_ENTRY * pHashEntry;
     DWORD dwHashTableNumber = 0;
+    DWORD error;
 
     key = (key / HASHTABLE_NUM_ENTRIES) * HASHTABLE_NUM_ENTRIES;
 
@@ -1324,17 +1339,17 @@
             {
                 pHashElement->dwHashKey = key;
                 pHashElement->dwOffsetEntry = dwOffsetEntry;
-                return TRUE;
+                return ERROR_SUCCESS;
             }
         }
     }
-    pHashEntry = URLCache_CreateHashTable(pHeader, pHashEntry);
-    if (!pHashEntry)
-        return FALSE;
+    error = URLCache_CreateHashTable(pHeader, pHashEntry, &pHashEntry);
+    if (error != ERROR_SUCCESS)
+        return error;
 
     pHashEntry->HashTable[offset].dwHashKey = key;
     pHashEntry->HashTable[offset].dwOffsetEntry = dwOffsetEntry;
-    return TRUE;
+    return ERROR_SUCCESS;
 }
 
 /***********************************************************************
@@ -1344,38 +1359,36 @@
  * hash tables.
  *
  * RETURNS
- *    TRUE if the hash table was created
- *    FALSE if the hash table could not be created
- *
- */
-static HASH_CACHEFILE_ENTRY *URLCache_CreateHashTable(LPURLCACHE_HEADER 
pHeader, HASH_CACHEFILE_ENTRY *pPrevHash)
-{
-    HASH_CACHEFILE_ENTRY *pHash;
+ *    ERROR_SUCCESS if the hash table was created
+ *    ERROR_DISK_FULL if the hash table could not be created
+ *
+ */
+static DWORD URLCache_CreateHashTable(LPURLCACHE_HEADER pHeader, 
HASH_CACHEFILE_ENTRY *pPrevHash, HASH_CACHEFILE_ENTRY **ppHash)
+{
     DWORD dwOffset;
     int i;
 
-    if (!URLCache_FindFirstFreeEntry(pHeader, 0x20, (CACHEFILE_ENTRY 
**)&pHash))
+    if (!URLCache_FindFirstFreeEntry(pHeader, 0x20, (CACHEFILE_ENTRY 
**)ppHash))
     {
         FIXME("no free space for hash table\n");
-        SetLastError(ERROR_DISK_FULL);
-        return NULL;
-    }
-
-    dwOffset = (BYTE *)pHash - (BYTE *)pHeader;
+        return ERROR_DISK_FULL;
+    }
+
+    dwOffset = (BYTE *)*ppHash - (BYTE *)pHeader;
 
     if (pPrevHash)
         pPrevHash->dwAddressNext = dwOffset;
     else
         pHeader->dwOffsetFirstHashTable = dwOffset;
-    pHash->CacheFileEntry.dwSignature = HASH_SIGNATURE;
-    pHash->CacheFileEntry.dwBlocksUsed = 0x20;
-    pHash->dwHashTableNumber = pPrevHash ? pPrevHash->dwHashTableNumber + 1 : 
0;
+    (*ppHash)->CacheFileEntry.dwSignature = HASH_SIGNATURE;
+    (*ppHash)->CacheFileEntry.dwBlocksUsed = 0x20;
+    (*ppHash)->dwHashTableNumber = pPrevHash ? pPrevHash->dwHashTableNumber + 
1 : 0;
     for (i = 0; i < HASHTABLE_SIZE; i++)
     {
-        pHash->HashTable[i].dwOffsetEntry = 0;
-        pHash->HashTable[i].dwHashKey = HASHTABLE_FREE;
-    }
-    return pHash;
+        (*ppHash)->HashTable[i].dwOffsetEntry = 0;
+        (*ppHash)->HashTable[i].dwHashKey = HASHTABLE_FREE;
+    }
+    return ERROR_SUCCESS;
 }
 
 /***********************************************************************
@@ -1488,14 +1501,23 @@
     const CACHEFILE_ENTRY * pEntry;
     const URL_CACHEFILE_ENTRY * pUrlEntry;
     URLCACHECONTAINER * pContainer;
+    DWORD error;
 
     TRACE("(%s, %p, %p)\n", debugstr_a(lpszUrlName), lpCacheEntryInfo, 
lpdwCacheEntryInfoBufferSize);
 
-    if (!URLCacheContainers_FindContainerA(lpszUrlName, &pContainer))
-        return FALSE;
-
-    if (!URLCacheContainer_OpenIndex(pContainer))
-        return FALSE;
+    error = URLCacheContainers_FindContainerA(lpszUrlName, &pContainer);
+    if (error != ERROR_SUCCESS)
+    {
+        SetLastError(error);
+        return FALSE;
+    }
+
+    error = URLCacheContainer_OpenIndex(pContainer);
+    if (error != ERROR_SUCCESS)
+    {
+        SetLastError(error);
+        return FALSE;
+    }
 
     if (!(pHeader = URLCacheContainer_LockIndex(pContainer)))
         return FALSE;
@@ -1524,18 +1546,23 @@
 
     if (lpdwCacheEntryInfoBufferSize)
     {
-        if (!URLCache_CopyEntry(
+        if (!lpCacheEntryInfo)
+            *lpdwCacheEntryInfoBufferSize = 0;
+
+        error = URLCache_CopyEntry(
             pContainer,
             pHeader,
             lpCacheEntryInfo,
             lpdwCacheEntryInfoBufferSize,
             pUrlEntry,
-            FALSE /* ANSI */))
+            FALSE /* ANSI */);
+        if (error != ERROR_SUCCESS)
         {
             URLCacheContainer_UnlockIndex(pContainer, pHeader);
+            SetLastError(error);
             return FALSE;
         }
-        TRACE("Local File Name: %s\n", 
debugstr_a(lpCacheEntryInfo->lpszLocalFileName));
+        TRACE("Local File Name: %s\n", debugstr_a((LPCSTR)pUrlEntry + 
pUrlEntry->dwOffsetLocalName));
     }
 
     URLCacheContainer_UnlockIndex(pContainer, pHeader);
@@ -1556,14 +1583,23 @@
     const CACHEFILE_ENTRY * pEntry;
     const URL_CACHEFILE_ENTRY * pUrlEntry;
     URLCACHECONTAINER * pContainer;
+    DWORD error;
 
     TRACE("(%s, %p, %p)\n", debugstr_w(lpszUrl), lpCacheEntryInfo, 
lpdwCacheEntryInfoBufferSize);
 
-    if (!URLCacheContainers_FindContainerW(lpszUrl, &pContainer))
-        return FALSE;
-
-    if (!URLCacheContainer_OpenIndex(pContainer))
-        return FALSE;
+    error = URLCacheContainers_FindContainerW(lpszUrl, &pContainer);
+    if (error != ERROR_SUCCESS)
+    {
+        SetLastError(error);
+        return FALSE;
+    }
+
+    error = URLCacheContainer_OpenIndex(pContainer);
+    if (error != ERROR_SUCCESS)
+    {
+        SetLastError(error);
+        return FALSE;
+    }
 
     if (!(pHeader = URLCacheContainer_LockIndex(pContainer)))
         return FALSE;
@@ -1591,18 +1627,23 @@
 
     if (lpdwCacheEntryInfoBufferSize)
     {
-        if (!URLCache_CopyEntry(
+        if (!lpCacheEntryInfo)
+            *lpdwCacheEntryInfoBufferSize = 0;
+
+        error = URLCache_CopyEntry(
             pContainer,
             pHeader,
             (LPINTERNET_CACHE_ENTRY_INFOA)lpCacheEntryInfo,
             lpdwCacheEntryInfoBufferSize,
             pUrlEntry,
-            TRUE /* UNICODE */))
+            TRUE /* UNICODE */);
+        if (error != ERROR_SUCCESS)
         {
             URLCacheContainer_UnlockIndex(pContainer, pHeader);
+            SetLastError(error);
             return FALSE;
         }
-        TRACE("Local File Name: %s\n", 
debugstr_w(lpCacheEntryInfo->lpszLocalFileName));
+        TRACE("Local File Name: %s\n", debugstr_a((LPCSTR)pUrlEntry + 
pUrlEntry->dwOffsetLocalName));
     }
 
     URLCacheContainer_UnlockIndex(pContainer, pHeader);
@@ -1657,14 +1698,23 @@
     struct _HASH_ENTRY * pHashEntry;
     CACHEFILE_ENTRY * pEntry;
     URLCACHECONTAINER * pContainer;
+    DWORD error;
 
     TRACE("(%s, %p, 0x%08x)\n", debugstr_a(lpszUrlName), lpCacheEntryInfo, 
dwFieldControl);
 
-    if (!URLCacheContainers_FindContainerA(lpszUrlName, &pContainer))
-        return FALSE;
-
-    if (!URLCacheContainer_OpenIndex(pContainer))
-        return FALSE;
+    error = URLCacheContainers_FindContainerA(lpszUrlName, &pContainer);
+    if (error != ERROR_SUCCESS)
+    {
+        SetLastError(error);
+        return FALSE;
+    }
+
+    error = URLCacheContainer_OpenIndex(pContainer);
+    if (error != ERROR_SUCCESS)
+    {
+        SetLastError(error);
+        return FALSE;
+    }
 
     if (!(pHeader = URLCacheContainer_LockIndex(pContainer)))
         return FALSE;
@@ -1705,14 +1755,23 @@
     struct _HASH_ENTRY * pHashEntry;
     CACHEFILE_ENTRY * pEntry;
     URLCACHECONTAINER * pContainer;
+    DWORD error;
 
     TRACE("(%s, %p, 0x%08x)\n", debugstr_w(lpszUrl), lpCacheEntryInfo, 
dwFieldControl);
 
-    if (!URLCacheContainers_FindContainerW(lpszUrl, &pContainer))
-        return FALSE;
-
-    if (!URLCacheContainer_OpenIndex(pContainer))
-        return FALSE;
+    error = URLCacheContainers_FindContainerW(lpszUrl, &pContainer);
+    if (error != ERROR_SUCCESS)
+    {
+        SetLastError(error);
+        return FALSE;
+    }
+
+    error = URLCacheContainer_OpenIndex(pContainer);
+    if (error != ERROR_SUCCESS)
+    {
+        SetLastError(error);
+        return FALSE;
+    }
 
     if (!(pHeader = URLCacheContainer_LockIndex(pContainer)))
         return FALSE;
@@ -1760,6 +1819,7 @@
     CACHEFILE_ENTRY * pEntry;
     URL_CACHEFILE_ENTRY * pUrlEntry;
     URLCACHECONTAINER * pContainer;
+    DWORD error;
 
     TRACE("(%s, %p, %p, 0x%08x)\n",
         debugstr_a(lpszUrlName),
@@ -1767,11 +1827,26 @@
         lpdwCacheEntryInfoBufferSize,
         dwReserved);
 
-    if (!URLCacheContainers_FindContainerA(lpszUrlName, &pContainer))
-        return FALSE;
-
-    if (!URLCacheContainer_OpenIndex(pContainer))
-        return FALSE;
+    if (!lpszUrlName || !lpdwCacheEntryInfoBufferSize ||
+        (!lpCacheEntryInfo && *lpdwCacheEntryInfoBufferSize))
+    {
+        SetLastError(ERROR_INVALID_PARAMETER);
+        return FALSE;
+    }
+
+    error = URLCacheContainers_FindContainerA(lpszUrlName, &pContainer);
+    if (error != ERROR_SUCCESS)
+    {
+        SetLastError(error);
+        return FALSE;
+    }
+
+    error = URLCacheContainer_OpenIndex(pContainer);
+    if (error != ERROR_SUCCESS)
+    {
+        SetLastError(error);
+        return FALSE;
+    }
 
     if (!(pHeader = URLCacheContainer_LockIndex(pContainer)))
         return FALSE;
@@ -1801,12 +1876,16 @@
     pUrlEntry->dwUseCount++;
     URLCache_HashEntrySetUse(pHashEntry, pUrlEntry->dwUseCount);
 
-    if (!URLCache_CopyEntry(pContainer, pHeader, lpCacheEntryInfo, 
lpdwCacheEntryInfoBufferSize, pUrlEntry, FALSE))
+    error = URLCache_CopyEntry(pContainer, pHeader, lpCacheEntryInfo,
+                               lpdwCacheEntryInfoBufferSize, pUrlEntry,
+                               FALSE);
+    if (error != ERROR_SUCCESS)
     {
         URLCacheContainer_UnlockIndex(pContainer, pHeader);
-        return FALSE;
-    }
-    TRACE("Local File Name: %s\n", lpCacheEntryInfo->lpszLocalFileName);
+        SetLastError(error);
+        return FALSE;
+    }
+    TRACE("Local File Name: %s\n", debugstr_a((LPCSTR)pUrlEntry + 
pUrlEntry->dwOffsetLocalName));
 
     URLCacheContainer_UnlockIndex(pContainer, pHeader);
 
@@ -1829,6 +1908,7 @@
     CACHEFILE_ENTRY * pEntry;
     URL_CACHEFILE_ENTRY * pUrlEntry;
     URLCACHECONTAINER * pContainer;
+    DWORD error;
 
     TRACE("(%s, %p, %p, 0x%08x)\n",
         debugstr_w(lpszUrlName),
@@ -1836,11 +1916,26 @@
         lpdwCacheEntryInfoBufferSize,
         dwReserved);
 
-    if (!URLCacheContainers_FindContainerW(lpszUrlName, &pContainer))
-        return FALSE;
-
-    if (!URLCacheContainer_OpenIndex(pContainer))
-        return FALSE;
+    if (!lpszUrlName || !lpdwCacheEntryInfoBufferSize ||
+        (!lpCacheEntryInfo && *lpdwCacheEntryInfoBufferSize))
+    {
+        SetLastError(ERROR_INVALID_PARAMETER);
+        return FALSE;
+    }
+
+    error = URLCacheContainers_FindContainerW(lpszUrlName, &pContainer);
+    if (error != ERROR_SUCCESS)
+    {
+        SetLastError(error);
+        return FALSE;
+    }
+
+    error = URLCacheContainer_OpenIndex(pContainer);
+    if (error != ERROR_SUCCESS)
+    {
+        SetLastError(error);
+        return FALSE;
+    }
 
     if (!(pHeader = URLCacheContainer_LockIndex(pContainer)))
         return FALSE;
@@ -1870,18 +1965,20 @@
     pUrlEntry->dwUseCount++;
     URLCache_HashEntrySetUse(pHashEntry, pUrlEntry->dwUseCount);
 
-    if (!URLCache_CopyEntry(
+    error = URLCache_CopyEntry(
         pContainer,
         pHeader,
         (LPINTERNET_CACHE_ENTRY_INFOA)lpCacheEntryInfo,
         lpdwCacheEntryInfoBufferSize,
         pUrlEntry,
-        TRUE /* UNICODE */))
+        TRUE /* UNICODE */);
+    if (error != ERROR_SUCCESS)
     {
         URLCacheContainer_UnlockIndex(pContainer, pHeader);
-        return FALSE;
-    }
-    TRACE("Local File Name: %s\n", 
debugstr_w(lpCacheEntryInfo->lpszLocalFileName));
+        SetLastError(error);
+        return FALSE;
+    }
+    TRACE("Local File Name: %s\n", debugstr_a((LPCSTR)pUrlEntry + 
pUrlEntry->dwOffsetLocalName));
 
     URLCacheContainer_UnlockIndex(pContainer, pHeader);
 
@@ -1902,6 +1999,7 @@
     CACHEFILE_ENTRY * pEntry;
     URL_CACHEFILE_ENTRY * pUrlEntry;
     URLCACHECONTAINER * pContainer;
+    DWORD error;
 
     TRACE("(%s, 0x%08x)\n", debugstr_a(lpszUrlName), dwReserved);
 
@@ -1912,11 +2010,19 @@
         return FALSE;
     }
 
-    if (!URLCacheContainers_FindContainerA(lpszUrlName, &pContainer))
+    error = URLCacheContainers_FindContainerA(lpszUrlName, &pContainer);
+    if (error != ERROR_SUCCESS)
+    {
+       SetLastError(error);
        return FALSE;
-
-    if (!URLCacheContainer_OpenIndex(pContainer))
-        return FALSE;
+    }
+
+    error = URLCacheContainer_OpenIndex(pContainer);
+    if (error != ERROR_SUCCESS)
+    {
+        SetLastError(error);
+        return FALSE;
+    }
 
     if (!(pHeader = URLCacheContainer_LockIndex(pContainer)))
         return FALSE;
@@ -1964,6 +2070,7 @@
     CACHEFILE_ENTRY * pEntry;
     URL_CACHEFILE_ENTRY * pUrlEntry;
     URLCACHECONTAINER * pContainer;
+    DWORD error;
 
     TRACE("(%s, 0x%08x)\n", debugstr_w(lpszUrlName), dwReserved);
 
@@ -1974,11 +2081,19 @@
         return FALSE;
     }
 
-    if (!URLCacheContainers_FindContainerW(lpszUrlName, &pContainer))
-       return FALSE;
-
-    if (!URLCacheContainer_OpenIndex(pContainer))
-        return FALSE;
+    error = URLCacheContainers_FindContainerW(lpszUrlName, &pContainer);
+    if (error != ERROR_SUCCESS)
+    {
+        SetLastError(error);
+        return FALSE;
+    }
+
+    error = URLCacheContainer_OpenIndex(pContainer);
+    if (error != ERROR_SUCCESS)
+    {
+        SetLastError(error);
+        return FALSE;
+    }
 
     if (!(pHeader = URLCacheContainer_LockIndex(pContainer)))
         return FALSE;
@@ -2095,6 +2210,7 @@
     LONG lBufferSize;
     BOOL bFound = FALSE;
     int count;
+    DWORD error;
     static const WCHAR szWWW[] = {'w','w','w',0};
 
     TRACE("(%s, 0x%08x, %s, %p, 0x%08x)\n",
@@ -2144,6 +2260,8 @@
        if (!len)
            return FALSE;
         szFile[len] = '\0';
+        while(len && szFile[--len] == '/') szFile[len] = '\0';
+
         /* FIXME: get rid of illegal characters like \, / and : */
     }
     else
@@ -2153,11 +2271,19 @@
 
     TRACE("File name: %s\n", debugstr_a(szFile));
 
-    if (!URLCacheContainers_FindContainerW(lpszUrlName, &pContainer))
-        return FALSE;
-
-    if (!URLCacheContainer_OpenIndex(pContainer))
-        return FALSE;
+    error = URLCacheContainers_FindContainerW(lpszUrlName, &pContainer);
+    if (error != ERROR_SUCCESS)
+    {
+        SetLastError(error);
+        return FALSE;
+    }
+
+    error = URLCacheContainer_OpenIndex(pContainer);
+    if (error != ERROR_SUCCESS)
+    {
+        SetLastError(error);
+        return FALSE;
+    }
 
     if (!(pHeader = URLCacheContainer_LockIndex(pContainer)))
         return FALSE;
@@ -2191,9 +2317,26 @@
 
     for (i = 0; i < 255; i++)
     {
-       static const WCHAR szFormat[] = {'[','%','u',']','%','s',0};
+        static const WCHAR szFormat[] = {'[','%','u',']','%','s',0};
         HANDLE hFile;
+        WCHAR *p;
+
         wsprintfW(lpszFileNameNoPath + countnoextension, szFormat, i, 
szExtension);
+        for (p = lpszFileNameNoPath + 1; *p; p++)
+        {
+            switch (*p)
+            {
+            case '<': case '>':
+            case ':': case '"':
+            case '/': case '\\':
+            case '|': case '?':
+            case '*':
+                *p = '_'; break;
+            default: break;
+            }
+        }
+        if (p[-1] == ' ' || p[-1] == '.') p[-1] = '_';
+
         TRACE("Trying: %s\n", debugstr_w(lpszFileName));
         hFile = CreateFileW(lpszFileName, GENERIC_READ, 0, NULL, CREATE_NEW, 
0, NULL);
         if (hFile != INVALID_HANDLE_VALUE)
@@ -2221,7 +2364,7 @@
  *   result will lose data for arbitrary binary data.
  *
  */
-static BOOL WINAPI CommitUrlCacheEntryInternal(
+static BOOL CommitUrlCacheEntryInternal(
     IN LPCWSTR lpszUrlName,
     IN LPCWSTR lpszLocalFileName,
     IN FILETIME ExpireTime,
@@ -2250,7 +2393,7 @@
     LPSTR lpszUrlNameA = NULL;
     LPSTR lpszFileExtensionA = NULL;
     char *pchLocalFileName = 0;
-    DWORD error = ERROR_SUCCESS;
+    DWORD error;
 
     TRACE("(%s, %s, ..., ..., %x, %p, %d, %s, %s)\n",
         debugstr_w(lpszUrlName),
@@ -2287,11 +2430,19 @@
         CloseHandle(hFile);
     }
 
-    if (!URLCacheContainers_FindContainerW(lpszUrlName, &pContainer))
-        return FALSE;
-
-    if (!URLCacheContainer_OpenIndex(pContainer))
-        return FALSE;
+    error = URLCacheContainers_FindContainerW(lpszUrlName, &pContainer);
+    if (error != ERROR_SUCCESS)
+    {
+        SetLastError(error);
+        return FALSE;
+    }
+
+    error = URLCacheContainer_OpenIndex(pContainer);
+    if (error != ERROR_SUCCESS)
+    {
+        SetLastError(error);
+        return FALSE;
+    }
 
     if (!(pHeader = URLCacheContainer_LockIndex(pContainer)))
         return FALSE;
@@ -2436,13 +2587,10 @@
     if (dwOffsetFileExtension)
         strcpy((LPSTR)((LPBYTE)pUrlEntry + dwOffsetFileExtension), 
lpszFileExtensionA);
 
-    if (!URLCache_AddEntryToHash(pHeader, lpszUrlNameA, 
(DWORD)((LPBYTE)pUrlEntry - (LPBYTE)pHeader)))
-    {
+    error = URLCache_AddEntryToHash(pHeader, lpszUrlNameA,
+        (DWORD)((LPBYTE)pUrlEntry - (LPBYTE)pHeader));
+    if (error != ERROR_SUCCESS)
         URLCache_DeleteEntry(pHeader, &pUrlEntry->CacheFileEntry);
-        URLCacheContainer_UnlockIndex(pContainer, pHeader);
-        HeapFree(GetProcessHeap(), 0, lpszUrlNameA);
-        return FALSE;
-    }
 
 cleanup:
     URLCacheContainer_UnlockIndex(pContainer, pHeader);
@@ -2675,7 +2823,7 @@
 
     pStream->hFile = hFile;
     strcpy(pStream->lpszUrl, lpszUrlName);
-    return (HANDLE)pStream;
+    return pStream;
 }
 
 /***********************************************************************
@@ -2742,14 +2890,23 @@
     LPURLCACHE_HEADER pHeader;
     struct _HASH_ENTRY * pHashEntry;
     CACHEFILE_ENTRY * pEntry;
+    DWORD error;
 
     TRACE("(%s)\n", debugstr_a(lpszUrlName));
 
-    if (!URLCacheContainers_FindContainerA(lpszUrlName, &pContainer))
-        return FALSE;
-
-    if (!URLCacheContainer_OpenIndex(pContainer))
-        return FALSE;
+    error = URLCacheContainers_FindContainerA(lpszUrlName, &pContainer);
+    if (error != ERROR_SUCCESS)
+    {
+        SetLastError(error);
+        return FALSE;
+    }
+
+    error = URLCacheContainer_OpenIndex(pContainer);
+    if (error != ERROR_SUCCESS)
+    {
+        SetLastError(error);
+        return FALSE;
+    }
 
     if (!(pHeader = URLCacheContainer_LockIndex(pContainer)))
         return FALSE;
@@ -2784,6 +2941,7 @@
     CACHEFILE_ENTRY * pEntry;
     LPSTR urlA;
     int url_len;
+    DWORD error;
 
     TRACE("(%s)\n", debugstr_w(lpszUrlName));
 
@@ -2796,16 +2954,22 @@
     }
     WideCharToMultiByte(CP_ACP, 0, lpszUrlName, -1, urlA, url_len, NULL, NULL);
 
-    if (!URLCacheContainers_FindContainerW(lpszUrlName, &pContainer))
+    error = URLCacheContainers_FindContainerW(lpszUrlName, &pContainer);
+    if (error != ERROR_SUCCESS)
     {
         HeapFree(GetProcessHeap(), 0, urlA);
-        return FALSE;
-    }
-    if (!URLCacheContainer_OpenIndex(pContainer))
+        SetLastError(error);
+        return FALSE;
+    }
+
+    error = URLCacheContainer_OpenIndex(pContainer);
+    if (error != ERROR_SUCCESS)
     {
         HeapFree(GetProcessHeap(), 0, urlA);
-        return FALSE;
-    }
+        SetLastError(error);
+        return FALSE;
+    }
+
     if (!(pHeader = URLCacheContainer_LockIndex(pContainer)))
     {
         HeapFree(GetProcessHeap(), 0, urlA);
@@ -3057,9 +3221,14 @@
     {
         LPURLCACHE_HEADER pHeader;
         HASH_CACHEFILE_ENTRY *pHashTableEntry;
-
-        if (!URLCacheContainer_OpenIndex(pContainer))
+        DWORD error;
+
+        error = URLCacheContainer_OpenIndex(pContainer);
+        if (error != ERROR_SUCCESS)
+        {
+            SetLastError(error);
             return FALSE;
+        }
 
         if (!(pHeader = URLCacheContainer_LockIndex(pContainer)))
             return FALSE;
@@ -3081,18 +3250,20 @@
                 TRACE("Found URL: %s\n", (LPSTR)pUrlEntry + 
pUrlEntry->dwOffsetUrl);
                 TRACE("Header info: %s\n", (LPBYTE)pUrlEntry + 
pUrlEntry->dwOffsetHeaderInfo);
 
-                if (!URLCache_CopyEntry(
+                error = URLCache_CopyEntry(
                     pContainer,
                     pHeader,
                     lpNextCacheEntryInfo,
                     lpdwNextCacheEntryInfoBufferSize,
                     pUrlEntry,
-                    FALSE /* not UNICODE */))
+                    FALSE /* not UNICODE */);
+                if (error != ERROR_SUCCESS)
                 {
                     URLCacheContainer_UnlockIndex(pContainer, pHeader);
+                    SetLastError(error);
                     return FALSE;
                 }
-                TRACE("Local File Name: %s\n", 
debugstr_a(lpNextCacheEntryInfo->lpszLocalFileName));
+                TRACE("Local File Name: %s\n", debugstr_a((LPCSTR)pUrlEntry + 
pUrlEntry->dwOffsetLocalName));
 
                 /* increment the current index so that next time the function
                  * is called the next entry is returned */
@@ -3109,6 +3280,9 @@
     return FALSE;
 }
 
+/***********************************************************************
+ *           FindNextUrlCacheEntryW (WININET.@)
+ */
 BOOL WINAPI FindNextUrlCacheEntryW(
   HANDLE hEnumHandle,
   LPINTERNET_CACHE_ENTRY_INFOW lpNextCacheEntryInfo,
@@ -3337,14 +3511,23 @@
     const CACHEFILE_ENTRY * pEntry;
     const URL_CACHEFILE_ENTRY * pUrlEntry;
     URLCACHECONTAINER * pContainer;
+    DWORD error;
 
     TRACE("(%s, %08x, %p)\n", debugstr_a(url), dwFlags, pftLastModified);
 
-    if (!URLCacheContainers_FindContainerA(url, &pContainer))
-        return FALSE;
-
-    if (!URLCacheContainer_OpenIndex(pContainer))
-        return FALSE;
+    error = URLCacheContainers_FindContainerA(url, &pContainer);
+    if (error != ERROR_SUCCESS)
+    {
+        SetLastError(error);
+        return FALSE;
+    }
+
+    error = URLCacheContainer_OpenIndex(pContainer);
+    if (error != ERROR_SUCCESS)
+    {
+        SetLastError(error);
+        return FALSE;
+    }
 
     if (!(pHeader = URLCacheContainer_LockIndex(pContainer)))
         return FALSE;
@@ -3390,14 +3573,23 @@
     const CACHEFILE_ENTRY * pEntry;
     const URL_CACHEFILE_ENTRY * pUrlEntry;
     URLCACHECONTAINER * pContainer;
+    DWORD error;
 
     TRACE("(%s, %08x, %p)\n", debugstr_w(url), dwFlags, pftLastModified);
 
-    if (!URLCacheContainers_FindContainerW(url, &pContainer))
-        return FALSE;
-
-    if (!URLCacheContainer_OpenIndex(pContainer))
-        return FALSE;
+    error = URLCacheContainers_FindContainerW(url, &pContainer);
+    if (error != ERROR_SUCCESS)
+    {
+        SetLastError(error);
+        return FALSE;
+    }
+
+    error = URLCacheContainer_OpenIndex(pContainer);
+    if (error != ERROR_SUCCESS)
+    {
+        SetLastError(error);
+        return FALSE;
+    }
 
     if (!(pHeader = URLCacheContainer_LockIndex(pContainer)))
         return FALSE;
@@ -3427,3 +3619,21 @@
 
     return TRUE;
 }
+
+/***********************************************************************
+ *           GetDiskInfoA (WININET.@)
+ */
+DWORD WINAPI GetDiskInfoA(void *p0, void *p1, void *p2, void *p3)
+{
+    FIXME("(%p, %p, %p, %p)\n", p0, p1, p2, p3);
+    return 0;
+}
+
+/***********************************************************************
+ *           RegisterUrlCacheNotification (WININET.@)
+ */
+DWORD WINAPI RegisterUrlCacheNotification(LPVOID a, DWORD b, DWORD c, DWORD d, 
DWORD e, DWORD f)
+{
+    FIXME("(%p %x %x %x %x %x)\n", a, b, c, d, e, f);
+    return 0;
+}

Reply via email to