From 1ecfbaf361620a7df1ffd89c4e05e13e50877f8f Mon Sep 17 00:00:00 2001 From: Christoph von Wittich Date: Wed, 15 Apr 2009 09:17:12 +0000 Subject: [PATCH] sync wininet urlcache.c with wine 1.1.19 - fixes memory corruptions svn path=/trunk/; revision=40520 --- reactos/dll/win32/wininet/urlcache.c | 452 ++++++++++++++++++++------- 1 file changed, 331 insertions(+), 121 deletions(-) diff --git a/reactos/dll/win32/wininet/urlcache.c b/reactos/dll/win32/wininet/urlcache.c index d0369289a2f..b4feba3260c 100644 --- a/reactos/dll/win32/wininet/urlcache.c +++ b/reactos/dll/win32/wininet/urlcache.c @@ -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,15 +25,23 @@ #include "config.h" #include "wine/port.h" +#define NONAMELESSUNION +#define NONAMELESSSTRUCT + +#if defined(__MINGW32__) || defined (_MSC_VER) +#include +#endif + #include #include #include #include +#include +#ifdef HAVE_SYS_SOCKET_H +# include +#endif #include -#define NONAMELESSUNION -#define NONAMELESSSTRUCT - #include "windef.h" #include "winbase.h" #include "winuser.h" @@ -178,7 +186,7 @@ typedef struct _URLCACHECONTAINER /* 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 @@ static void URLCache_PathToObjectName(LPWSTR lpszPath, WCHAR replace) * Opens the index file and saves mapping handle in hCacheIndexMapping * * RETURNS - * TRUE if succeeded - * FALSE if failed + * ERROR_SUCCESS if succeeded + * Any other Win32 error code if failed * */ -static BOOL URLCacheContainer_OpenIndex(URLCACHECONTAINER * pContainer) +static DWORD URLCacheContainer_OpenIndex(URLCACHECONTAINER * pContainer) { HANDLE hFile; WCHAR wszFilePath[MAX_PATH]; @@ -220,7 +228,7 @@ static BOOL URLCacheContainer_OpenIndex(URLCACHECONTAINER * pContainer) 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 @@ static BOOL URLCacheContainer_OpenIndex(URLCACHECONTAINER * pContainer) 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 @@ static BOOL URLCacheContainer_OpenIndex(URLCACHECONTAINER * pContainer) if (dwFileSize == INVALID_FILE_SIZE) { ReleaseMutex(pContainer->hMutex); - return FALSE; + return GetLastError(); } if (dwFileSize == 0) @@ -256,7 +264,7 @@ static BOOL URLCacheContainer_OpenIndex(URLCACHECONTAINER * pContainer) 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 @@ static BOOL URLCacheContainer_OpenIndex(URLCACHECONTAINER * pContainer) } } - if (!dwError) + if (dwError == ERROR_SUCCESS) { HANDLE hMapping = CreateFileMappingW(hFile, NULL, PAGE_READWRITE, 0, NEWFILE_SIZE, NULL); @@ -296,6 +304,7 @@ static BOOL URLCacheContainer_OpenIndex(URLCACHECONTAINER * pContainer) WCHAR wszDirPath[MAX_PATH]; FILETIME ft; int i, j; + HASH_CACHEFILE_ENTRY *pHashEntry; dwFileSize = NEWFILE_SIZE; @@ -326,7 +335,7 @@ static BOOL URLCacheContainer_OpenIndex(URLCACHECONTAINER * pContainer) RegCloseKey(key); } - URLCache_CreateHashTable(pHeader, NULL); + URLCache_CreateHashTable(pHeader, NULL, &pHashEntry); /* Last step - create the directories */ @@ -416,8 +425,7 @@ static BOOL URLCacheContainer_OpenIndex(URLCACHECONTAINER * pContainer) CloseHandle(hFile); DeleteFileW(wszFilePath); ReleaseMutex(pContainer->hMutex); - SetLastError(dwError); - return FALSE; + return dwError; } } @@ -433,10 +441,10 @@ static BOOL URLCacheContainer_OpenIndex(URLCACHECONTAINER * pContainer) if (!pContainer->hMapping) { ERR("Couldn't create file mapping (error is %d)\n", GetLastError()); - return FALSE; + return GetLastError(); } - return TRUE; + return ERROR_SUCCESS; } /*********************************************************************** @@ -579,7 +587,7 @@ void URLCacheContainers_DeleteAll(void) ); } -static BOOL URLCacheContainers_FindContainerW(LPCWSTR lpwszUrl, URLCACHECONTAINER ** ppContainer) +static DWORD URLCacheContainers_FindContainerW(LPCWSTR lpwszUrl, URLCACHECONTAINER ** ppContainer) { URLCACHECONTAINER * pContainer; @@ -590,19 +598,18 @@ static BOOL URLCacheContainers_FindContainerW(LPCWSTR lpwszUrl, URLCACHECONTAINE 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; + return ERROR_FILE_NOT_FOUND; } -static BOOL URLCacheContainers_FindContainerA(LPCSTR lpszUrl, URLCACHECONTAINER ** ppContainer) +static DWORD URLCacheContainers_FindContainerA(LPCSTR lpszUrl, URLCACHECONTAINER ** ppContainer) { - BOOL ret; + 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 @@ static BOOL URLCacheContainers_FindContainerA(LPCSTR lpszUrl, URLCACHECONTAINER HeapFree(GetProcessHeap(), 0, lpwszUrl); return ret; } - return FALSE; + return GetLastError(); } static BOOL URLCacheContainers_Enum(LPCWSTR lpwszSearchPattern, DWORD dwIndex, URLCACHECONTAINER ** ppContainer) @@ -654,12 +661,18 @@ static BOOL URLCacheContainers_Enum(LPCWSTR lpwszSearchPattern, DWORD dwIndex, U /*********************************************************************** * 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 @@ static LPURLCACHE_HEADER URLCacheContainer_LockIndex(URLCACHECONTAINER * pContai { ReleaseMutex(pContainer->hMutex); ERR("Couldn't MapViewOfFile. Error: %d\n", GetLastError()); - return FALSE; + return NULL; } pHeader = (URLCACHE_HEADER *)pIndexData; @@ -680,10 +693,12 @@ static LPURLCACHE_HEADER URLCacheContainer_LockIndex(URLCACHECONTAINER * pContai 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 @@ static LPURLCACHE_HEADER URLCacheContainer_LockIndex(URLCACHECONTAINER * pContai { ReleaseMutex(pContainer->hMutex); ERR("Couldn't MapViewOfFile. Error: %d\n", GetLastError()); - return FALSE; + return NULL; } pHeader = (URLCACHE_HEADER *)pIndexData; } @@ -931,11 +946,11 @@ static BOOL URLCache_LocalFileNameToPathA( * 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 + * ERROR_SUCCESS if the buffer was big enough + * ERROR_INSUFFICIENT_BUFFER if the buffer was too small * */ -static BOOL URLCache_CopyEntry( +static DWORD URLCache_CopyEntry( URLCACHECONTAINER * pContainer, LPCURLCACHE_HEADER pHeader, LPINTERNET_CACHE_ENTRY_INFOA lpCacheEntryInfo, @@ -983,9 +998,9 @@ static BOOL URLCache_CopyEntry( 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 @@ static BOOL URLCache_CopyEntry( 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 @@ static BOOL URLCache_CopyEntry( 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 @@ static BOOL URLCache_CopyEntry( * according to the flags set by dwFieldControl. * * RETURNS - * TRUE if the buffer was big enough - * FALSE if the buffer was too small + * ERROR_SUCCESS if the buffer was big enough + * ERROR_INSUFFICIENT_BUFFER if the buffer was too small * */ -static BOOL URLCache_SetEntryInfo(URL_CACHEFILE_ENTRY * pUrlEntry, const INTERNET_CACHE_ENTRY_INFOW * lpCacheEntryInfo, DWORD dwFieldControl) +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 @@ static BOOL URLCache_SetEntryInfo(URL_CACHEFILE_ENTRY * pUrlEntry, const INTERNE if (dwFieldControl & CACHE_ENTRY_SYNCTIME_FC) FileTimeToDosDateTime(&lpCacheEntryInfo->LastAccessTime, &pUrlEntry->wLastSyncDate, &pUrlEntry->wLastSyncTime); - return TRUE; + return ERROR_SUCCESS; } /*********************************************************************** @@ -1285,11 +1299,11 @@ static BOOL URLCache_DeleteEntryFromHash(struct _HASH_ENTRY * pHashEntry) * key are entered into the hash table. * * RETURNS - * TRUE if the entry was added - * FALSE if the entry could not be added + * ERROR_SUCCESS if the entry was added + * Any other Win32 error code if the entry could not be added * */ -static BOOL URLCache_AddEntryToHash(LPURLCACHE_HEADER pHeader, LPCSTR lpszUrl, DWORD dwOffsetEntry) +static DWORD URLCache_AddEntryToHash(LPURLCACHE_HEADER pHeader, LPCSTR lpszUrl, DWORD dwOffsetEntry) { /* see URLCache_FindEntryInHash for structure of hash tables */ @@ -1297,6 +1311,7 @@ static BOOL URLCache_AddEntryToHash(LPURLCACHE_HEADER pHeader, LPCSTR lpszUrl, D 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 @@ static BOOL URLCache_AddEntryToHash(LPURLCACHE_HEADER pHeader, LPCSTR lpszUrl, D { 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 @@ static BOOL URLCache_AddEntryToHash(LPURLCACHE_HEADER pHeader, LPCSTR lpszUrl, D * hash tables. * * RETURNS - * TRUE if the hash table was created - * FALSE if the hash table could not be created + * ERROR_SUCCESS if the hash table was created + * ERROR_DISK_FULL if the hash table could not be created * */ -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) { - HASH_CACHEFILE_ENTRY *pHash; 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; + return ERROR_DISK_FULL; } - dwOffset = (BYTE *)pHash - (BYTE *)pHeader; + 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; + (*ppHash)->HashTable[i].dwOffsetEntry = 0; + (*ppHash)->HashTable[i].dwHashKey = HASHTABLE_FREE; } - return pHash; + return ERROR_SUCCESS; } /*********************************************************************** @@ -1488,14 +1501,23 @@ BOOL WINAPI GetUrlCacheEntryInfoA( 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)) + error = URLCacheContainers_FindContainerA(lpszUrlName, &pContainer); + if (error != ERROR_SUCCESS) + { + SetLastError(error); return FALSE; + } - if (!URLCacheContainer_OpenIndex(pContainer)) + error = URLCacheContainer_OpenIndex(pContainer); + if (error != ERROR_SUCCESS) + { + SetLastError(error); return FALSE; + } if (!(pHeader = URLCacheContainer_LockIndex(pContainer))) return FALSE; @@ -1524,18 +1546,23 @@ BOOL WINAPI GetUrlCacheEntryInfoA( 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 @@ BOOL WINAPI GetUrlCacheEntryInfoW(LPCWSTR lpszUrl, 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)) + error = URLCacheContainers_FindContainerW(lpszUrl, &pContainer); + if (error != ERROR_SUCCESS) + { + SetLastError(error); return FALSE; + } - if (!URLCacheContainer_OpenIndex(pContainer)) + error = URLCacheContainer_OpenIndex(pContainer); + if (error != ERROR_SUCCESS) + { + SetLastError(error); return FALSE; + } if (!(pHeader = URLCacheContainer_LockIndex(pContainer))) return FALSE; @@ -1591,18 +1627,23 @@ BOOL WINAPI GetUrlCacheEntryInfoW(LPCWSTR lpszUrl, 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 @@ BOOL WINAPI SetUrlCacheEntryInfoA( 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)) + error = URLCacheContainers_FindContainerA(lpszUrlName, &pContainer); + if (error != ERROR_SUCCESS) + { + SetLastError(error); return FALSE; + } - if (!URLCacheContainer_OpenIndex(pContainer)) + error = URLCacheContainer_OpenIndex(pContainer); + if (error != ERROR_SUCCESS) + { + SetLastError(error); return FALSE; + } if (!(pHeader = URLCacheContainer_LockIndex(pContainer))) return FALSE; @@ -1705,14 +1755,23 @@ BOOL WINAPI SetUrlCacheEntryInfoW(LPCWSTR lpszUrl, LPINTERNET_CACHE_ENTRY_INFOW 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)) + error = URLCacheContainers_FindContainerW(lpszUrl, &pContainer); + if (error != ERROR_SUCCESS) + { + SetLastError(error); return FALSE; + } - if (!URLCacheContainer_OpenIndex(pContainer)) + error = URLCacheContainer_OpenIndex(pContainer); + if (error != ERROR_SUCCESS) + { + SetLastError(error); return FALSE; + } if (!(pHeader = URLCacheContainer_LockIndex(pContainer))) return FALSE; @@ -1760,6 +1819,7 @@ BOOL WINAPI RetrieveUrlCacheEntryFileA( 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 @@ BOOL WINAPI RetrieveUrlCacheEntryFileA( lpdwCacheEntryInfoBufferSize, dwReserved); - if (!URLCacheContainers_FindContainerA(lpszUrlName, &pContainer)) + if (!lpszUrlName || !lpdwCacheEntryInfoBufferSize || + (!lpCacheEntryInfo && *lpdwCacheEntryInfoBufferSize)) + { + SetLastError(ERROR_INVALID_PARAMETER); return FALSE; + } - if (!URLCacheContainer_OpenIndex(pContainer)) + 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 @@ BOOL WINAPI RetrieveUrlCacheEntryFileA( 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); + SetLastError(error); return FALSE; } - TRACE("Local File Name: %s\n", lpCacheEntryInfo->lpszLocalFileName); + TRACE("Local File Name: %s\n", debugstr_a((LPCSTR)pUrlEntry + pUrlEntry->dwOffsetLocalName)); URLCacheContainer_UnlockIndex(pContainer, pHeader); @@ -1829,6 +1908,7 @@ BOOL WINAPI RetrieveUrlCacheEntryFileW( 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 @@ BOOL WINAPI RetrieveUrlCacheEntryFileW( lpdwCacheEntryInfoBufferSize, dwReserved); - if (!URLCacheContainers_FindContainerW(lpszUrlName, &pContainer)) + if (!lpszUrlName || !lpdwCacheEntryInfoBufferSize || + (!lpCacheEntryInfo && *lpdwCacheEntryInfoBufferSize)) + { + SetLastError(ERROR_INVALID_PARAMETER); return FALSE; + } - if (!URLCacheContainer_OpenIndex(pContainer)) + 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 @@ BOOL WINAPI RetrieveUrlCacheEntryFileW( 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); + 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); @@ -1902,6 +1999,7 @@ BOOL WINAPI UnlockUrlCacheEntryFileA( CACHEFILE_ENTRY * pEntry; URL_CACHEFILE_ENTRY * pUrlEntry; URLCACHECONTAINER * pContainer; + DWORD error; TRACE("(%s, 0x%08x)\n", debugstr_a(lpszUrlName), dwReserved); @@ -1912,11 +2010,19 @@ BOOL WINAPI UnlockUrlCacheEntryFileA( return FALSE; } - if (!URLCacheContainers_FindContainerA(lpszUrlName, &pContainer)) + error = URLCacheContainers_FindContainerA(lpszUrlName, &pContainer); + if (error != ERROR_SUCCESS) + { + SetLastError(error); return FALSE; + } - if (!URLCacheContainer_OpenIndex(pContainer)) + error = URLCacheContainer_OpenIndex(pContainer); + if (error != ERROR_SUCCESS) + { + SetLastError(error); return FALSE; + } if (!(pHeader = URLCacheContainer_LockIndex(pContainer))) return FALSE; @@ -1964,6 +2070,7 @@ BOOL WINAPI UnlockUrlCacheEntryFileW( LPCWSTR lpszUrlName, DWORD dwReserved ) CACHEFILE_ENTRY * pEntry; URL_CACHEFILE_ENTRY * pUrlEntry; URLCACHECONTAINER * pContainer; + DWORD error; TRACE("(%s, 0x%08x)\n", debugstr_w(lpszUrlName), dwReserved); @@ -1974,11 +2081,19 @@ BOOL WINAPI UnlockUrlCacheEntryFileW( LPCWSTR lpszUrlName, DWORD dwReserved ) return FALSE; } - if (!URLCacheContainers_FindContainerW(lpszUrlName, &pContainer)) - return FALSE; - - if (!URLCacheContainer_OpenIndex(pContainer)) + 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 @@ BOOL WINAPI CreateUrlCacheEntryW( 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 @@ BOOL WINAPI CreateUrlCacheEntryW( 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 @@ BOOL WINAPI CreateUrlCacheEntryW( TRACE("File name: %s\n", debugstr_a(szFile)); - if (!URLCacheContainers_FindContainerW(lpszUrlName, &pContainer)) + error = URLCacheContainers_FindContainerW(lpszUrlName, &pContainer); + if (error != ERROR_SUCCESS) + { + SetLastError(error); return FALSE; + } - if (!URLCacheContainer_OpenIndex(pContainer)) + error = URLCacheContainer_OpenIndex(pContainer); + if (error != ERROR_SUCCESS) + { + SetLastError(error); return FALSE; + } if (!(pHeader = URLCacheContainer_LockIndex(pContainer))) return FALSE; @@ -2191,9 +2317,26 @@ BOOL WINAPI CreateUrlCacheEntryW( 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 @@ BOOL WINAPI CreateUrlCacheEntryW( * 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 @@ static BOOL WINAPI CommitUrlCacheEntryInternal( 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 @@ static BOOL WINAPI CommitUrlCacheEntryInternal( CloseHandle(hFile); } - if (!URLCacheContainers_FindContainerW(lpszUrlName, &pContainer)) + error = URLCacheContainers_FindContainerW(lpszUrlName, &pContainer); + if (error != ERROR_SUCCESS) + { + SetLastError(error); return FALSE; + } - if (!URLCacheContainer_OpenIndex(pContainer)) + error = URLCacheContainer_OpenIndex(pContainer); + if (error != ERROR_SUCCESS) + { + SetLastError(error); return FALSE; + } if (!(pHeader = URLCacheContainer_LockIndex(pContainer))) return FALSE; @@ -2436,13 +2587,10 @@ static BOOL WINAPI CommitUrlCacheEntryInternal( 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 @@ HANDLE WINAPI RetrieveUrlCacheEntryStreamA( pStream->hFile = hFile; strcpy(pStream->lpszUrl, lpszUrlName); - return (HANDLE)pStream; + return pStream; } /*********************************************************************** @@ -2742,14 +2890,23 @@ BOOL WINAPI DeleteUrlCacheEntryA(LPCSTR lpszUrlName) LPURLCACHE_HEADER pHeader; struct _HASH_ENTRY * pHashEntry; CACHEFILE_ENTRY * pEntry; + DWORD error; TRACE("(%s)\n", debugstr_a(lpszUrlName)); - if (!URLCacheContainers_FindContainerA(lpszUrlName, &pContainer)) + error = URLCacheContainers_FindContainerA(lpszUrlName, &pContainer); + if (error != ERROR_SUCCESS) + { + SetLastError(error); return FALSE; + } - if (!URLCacheContainer_OpenIndex(pContainer)) + error = URLCacheContainer_OpenIndex(pContainer); + if (error != ERROR_SUCCESS) + { + SetLastError(error); return FALSE; + } if (!(pHeader = URLCacheContainer_LockIndex(pContainer))) return FALSE; @@ -2784,6 +2941,7 @@ BOOL WINAPI DeleteUrlCacheEntryW(LPCWSTR lpszUrlName) CACHEFILE_ENTRY * pEntry; LPSTR urlA; int url_len; + DWORD error; TRACE("(%s)\n", debugstr_w(lpszUrlName)); @@ -2796,16 +2954,22 @@ BOOL WINAPI DeleteUrlCacheEntryW(LPCWSTR lpszUrlName) } 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); + SetLastError(error); return FALSE; } - if (!URLCacheContainer_OpenIndex(pContainer)) + + error = URLCacheContainer_OpenIndex(pContainer); + if (error != ERROR_SUCCESS) { HeapFree(GetProcessHeap(), 0, urlA); + SetLastError(error); return FALSE; } + if (!(pHeader = URLCacheContainer_LockIndex(pContainer))) { HeapFree(GetProcessHeap(), 0, urlA); @@ -3057,9 +3221,14 @@ BOOL WINAPI FindNextUrlCacheEntryA( { LPURLCACHE_HEADER pHeader; HASH_CACHEFILE_ENTRY *pHashTableEntry; + DWORD error; - if (!URLCacheContainer_OpenIndex(pContainer)) + error = URLCacheContainer_OpenIndex(pContainer); + if (error != ERROR_SUCCESS) + { + SetLastError(error); return FALSE; + } if (!(pHeader = URLCacheContainer_LockIndex(pContainer))) return FALSE; @@ -3081,18 +3250,20 @@ BOOL WINAPI FindNextUrlCacheEntryA( 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 @@ BOOL WINAPI FindNextUrlCacheEntryA( return FALSE; } +/*********************************************************************** + * FindNextUrlCacheEntryW (WININET.@) + */ BOOL WINAPI FindNextUrlCacheEntryW( HANDLE hEnumHandle, LPINTERNET_CACHE_ENTRY_INFOW lpNextCacheEntryInfo, @@ -3337,14 +3511,23 @@ BOOL WINAPI IsUrlCacheEntryExpiredA( LPCSTR url, DWORD dwFlags, FILETIME* pftLas 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)) + error = URLCacheContainers_FindContainerA(url, &pContainer); + if (error != ERROR_SUCCESS) + { + SetLastError(error); return FALSE; + } - if (!URLCacheContainer_OpenIndex(pContainer)) + error = URLCacheContainer_OpenIndex(pContainer); + if (error != ERROR_SUCCESS) + { + SetLastError(error); return FALSE; + } if (!(pHeader = URLCacheContainer_LockIndex(pContainer))) return FALSE; @@ -3390,14 +3573,23 @@ BOOL WINAPI IsUrlCacheEntryExpiredW( LPCWSTR url, DWORD dwFlags, FILETIME* pftLa 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)) + error = URLCacheContainers_FindContainerW(url, &pContainer); + if (error != ERROR_SUCCESS) + { + SetLastError(error); return FALSE; + } - if (!URLCacheContainer_OpenIndex(pContainer)) + error = URLCacheContainer_OpenIndex(pContainer); + if (error != ERROR_SUCCESS) + { + SetLastError(error); return FALSE; + } if (!(pHeader = URLCacheContainer_LockIndex(pContainer))) return FALSE; @@ -3427,3 +3619,21 @@ BOOL WINAPI IsUrlCacheEntryExpiredW( LPCWSTR url, DWORD dwFlags, FILETIME* pftLa 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; +}