[RAPPS]
[reactos.git] / reactos / base / applications / rapps / loaddlg.c
index 2ed9abf..b81b642 100644 (file)
@@ -286,6 +286,7 @@ static BOOL CertIsValid(HINTERNET hInternet, LPWSTR lpszHostName)
             }
             InternetCloseHandle(hRequest);
         }
+        InternetCloseHandle(hConnect);
     }
     return Ret;
 }
@@ -355,6 +356,14 @@ ThreadFunc(LPVOID Context)
     if (FAILED(StringCbCatNW(path, sizeof(path), p + 1, filenameLength)))
         goto end;
 
+    /* is the file already there? let's avoid having to download it */
+    if (!bCab && AppInfo->szSHA1[0] != 0 && GetFileAttributesW(path) != INVALID_FILE_ATTRIBUTES)
+    {
+        /* only open it in case of total correctness */
+        if (VerifyInteg(AppInfo->szSHA1, path))
+            goto run;
+    }
+
     /* create an async download context for it */
     bTempfile = TRUE;
     dl = CreateDl(Context, &bCancelled);
@@ -408,10 +417,11 @@ ThreadFunc(LPVOID Context)
     if(FAILED(StringCbLengthW(AppInfo->szUrlDownload, sizeof(AppInfo->szUrlDownload), &urlLength)))
         goto end;
 
-    urlComponents.dwSchemeLength = urlLength*sizeof(WCHAR);
-    urlComponents.lpszScheme = malloc(urlComponents.dwSchemeLength);
-    urlComponents.dwHostNameLength = urlLength*sizeof(WCHAR);
-    urlComponents.lpszHostName = malloc(urlComponents.dwHostNameLength);
+    urlLength /= sizeof(WCHAR);
+    urlComponents.dwSchemeLength = urlLength + 1;
+    urlComponents.lpszScheme = malloc(urlComponents.dwSchemeLength * sizeof(WCHAR));
+    urlComponents.dwHostNameLength = urlLength + 1;
+    urlComponents.lpszHostName = malloc(urlComponents.dwHostNameLength * sizeof(WCHAR));
 
     if(!InternetCrackUrlW(AppInfo->szUrlDownload, urlLength+1, ICU_DECODE | ICU_ESCAPE, &urlComponents))
         goto end;
@@ -461,8 +471,32 @@ ThreadFunc(LPVOID Context)
     if (bCancelled)
         goto end;
 
+    /* if this thing isn't a RAPPS update and it has a SHA-1 checksum
+       verify its integrity by using the native advapi32.A_SHA1 functions */
+    if (!bCab && AppInfo->szSHA1[0] != 0)
+    {
+        WCHAR szMsgText[MAX_STR_LEN];
+
+        /* change a few strings in the download dialog to reflect the verification process */
+        LoadStringW(hInst, IDS_INTEG_CHECK_TITLE, szMsgText, _countof(szMsgText));
+
+        SetWindowText(Dlg, szMsgText);
+        SendMessageW(GetDlgItem(Dlg, IDC_DOWNLOAD_STATUS), WM_SETTEXT, 0, (LPARAM)path);
+
+        /* this may take a while, depending on the file size */
+        if (!VerifyInteg(AppInfo->szSHA1, path))
+        {
+            if (!LoadStringW(hInst, IDS_INTEG_CHECK_FAIL, szMsgText, _countof(szMsgText)))
+                goto end;
+
+            MessageBoxW(Dlg, szMsgText, NULL, MB_OK | MB_ICONERROR);
+            goto end;
+        }
+    }
+
     ShowWindow(Dlg, SW_HIDE);
 
+run:
     /* run it */
     if (!bCab)
         ShellExecuteW( NULL, L"open", path, NULL, NULL, SW_SHOWNORMAL );