Public feed
#886881
unsigned char* GetInternetFile(LPCTSTR szURL, size_t cbMaxSize, size_t* lpcbActualSize)
{
TRACE("STREAM: %s\r\n",szURL);
HINTERNET hNet = NULL;
HINTERNET hUrlFile = NULL;
unsigned char* buffer = NULL;
DWORD cbBytesRead = 0;
SIZE_T cbBytesTotal = 0;
BOOL bResult = FALSE;
const DWORD cbReadSize = 0x4000;
CString header = "Authorization: Basic aWhnOjUzeHk0aGlt\r\n";
DWORD dwBuffSize = 0;
dwBuffSize = header.GetLength();
if (!(hNet = InternetOpen(TEXT("IHG Downloader"), INTERNET_OPEN_TYPE_PRECONFIG,NULL,NULL,INTERNET_FLAG_NO_COOKIES | INTERNET_FLAG_NO_UI | INTERNET_FLAG_PRAGMA_NOCACHE | INTERNET_FLAG_SECURE)))
goto cleanup;
DWORD options = INTERNET_FLAG_NEED_FILE|INTERNET_FLAG_HYPERLINK|INTERNET_FLAG_RESYNCHRONIZE|INTERNET_FLAG_RELOAD|INTERNET_FLAG_KEEP_CONNECTION|INTERNET_FLAG_SECURE;
if (!(hUrlFile = InternetOpenUrl(hNet, szURL, header, dwBuffSize, options, 0)))
goto cleanup;
do
{
if (!(buffer = (unsigned char*) ReallocOrFree(buffer, cbBytesTotal + cbReadSize)))
goto cleanup;
if (!InternetReadFile(hUrlFile, buffer + cbBytesTotal, cbReadSize, &cbBytesRead))
goto cleanup;
cbBytesTotal += cbBytesRead;
/* Max size check and size_t overflow check */
if (cbBytesTotal > cbMaxSize || ((((size_t) -1) - cbReadSize) - 1) < cbBytesTotal)
goto cleanup;
} while (cbBytesRead > 0);
if (!(buffer = (unsigned char*) ReallocOrFree(buffer, cbBytesTotal + 1)))
goto cleanup;
buffer[cbBytesTotal] = '\0';
bResult = TRUE;
cleanup:
if (hUrlFile)
InternetCloseHandle(hUrlFile);
if (hNet)
InternetCloseHandle(hNet);
if (!bResult)
free(buffer);
if (lpcbActualSize) *lpcbActualSize = (bResult ? cbBytesTotal : 0);
return (bResult ? buffer : NULL);
}
void* ReallocOrFree(void* original_ptr, size_t new_size)
{
void* temp = realloc(original_ptr, new_size);
if (!temp) free(original_ptr);
return temp;
}