Annotation of truecrypt/common/dlgcode.c, revision 1.1.1.8

1.1.1.7   root        1: /* Legal Notice: The source code contained in this file has been derived from
                      2:    the source code of Encryption for the Masses 2.02a, which is Copyright (c)
                      3:    1998-99 Paul Le Roux and which is covered by the 'License Agreement for
                      4:    Encryption for the Masses'. Modifications and additions to that source code
                      5:    contained in this file are Copyright (c) 2004-2005 TrueCrypt Foundation and
                      6:    Copyright (c) 2004 TrueCrypt Team, and are covered by TrueCrypt License 2.0
                      7:    the full text of which is contained in the file License.txt included in
                      8:    TrueCrypt binary and source code distribution archives.  */
1.1       root        9: 
1.1.1.7   root       10: #include "Tcdefs.h"
1.1       root       11: 
1.1.1.7   root       12: #include <dbt.h>
                     13: #include <fcntl.h>
                     14: #include <io.h>
                     15: #include <shlobj.h>
                     16: #include <sys/stat.h>
1.1       root       17: #include <stdlib.h>
1.1.1.5   root       18: #include <time.h>
1.1       root       19: 
1.1.1.7   root       20: #include "Resource.h"
                     21: 
                     22: #include "Apidrvr.h"
                     23: #include "Crypto.h"
                     24: #include "Dictionary.h"
                     25: #include "Dlgcode.h"
                     26: #include "Endian.h"
                     27: #include "Language.h"
                     28: #include "Keyfiles.h"
                     29: #include "Pkcs5.h"
                     30: #include "Random.h"
                     31: #include "Registry.h"
                     32: #include "Tests.h"
                     33: #include "Volumes.h"
                     34: #include "Xml.h"
1.1.1.5   root       35: 
1.1       root       36: char szHelpFile[TC_MAX_PATH];
1.1.1.7   root       37: char szHelpFile2[TC_MAX_PATH];
                     38: HFONT hFixedDigitFont = NULL;
1.1       root       39: HFONT hBoldFont = NULL;
                     40: HFONT hTitleFont = NULL;
                     41: HFONT hFixedFont = NULL;
                     42: 
                     43: HFONT hUserFont = NULL;
                     44: HFONT hUserUnderlineFont = NULL;
                     45: HFONT hUserBoldFont = NULL;
1.1.1.5   root       46: HFONT hUserUnderlineBoldFont = NULL;
1.1       root       47: 
1.1.1.7   root       48: HWND MainDlg = NULL;
                     49: wchar_t *lpszTitle = NULL;
                     50: 
                     51: BOOL Silent = FALSE;
                     52: BOOL bPreserveTimestamp = TRUE;
                     53: 
1.1       root       54: int nCurrentOS = 0;
                     55: int CurrentOSMajor = 0;
                     56: int CurrentOSMinor = 0;
                     57: 
1.1.1.7   root       58: /* Globals used by Mount and Format (separately per instance) */ 
                     59: BOOL   KeyFilesEnable = FALSE;
                     60: KeyFile        *FirstKeyFile = NULL;
                     61: KeyFilesDlgParam               defaultKeyFilesParam;
                     62: 
1.1       root       63: /* Handle to the device driver */
                     64: HANDLE hDriver = INVALID_HANDLE_VALUE;
                     65: HINSTANCE hInst = NULL;
                     66: HANDLE hMutex = NULL;
                     67: HCURSOR hCursor = NULL;
                     68: 
                     69: ATOM hDlgClass, hSplashClass;
                     70: 
1.1.1.7   root       71: static FILE *ConfigFileHandle;
                     72: static char *ConfigBuffer;
                     73: 
                     74: #define RANDOM_POOL_DISPLAY_REFRESH_INTERVAL   30
                     75: 
1.1       root       76: /* Windows dialog class */
                     77: #define WINDOWS_DIALOG_CLASS "#32770"
                     78: 
                     79: /* Custom class names */
                     80: #define TC_DLG_CLASS "CustomDlg"
                     81: #define TC_SPLASH_CLASS "SplashDlg"
                     82: 
1.1.1.7   root       83: /* Benchmarks */
                     84: 
1.1.1.5   root       85: #ifndef SETUP
1.1.1.7   root       86: 
1.1.1.5   root       87: #define BENCHMARK_MAX_ITEMS 100
1.1.1.7   root       88: #define BENCHMARK_DEFAULT_BUF_SIZE     BYTES_PER_MB
                     89: #define HASH_FNC_BENCHMARKS    FALSE   // For development purposes only. Must be FALSE when building a public release.
                     90: #define PKCS5_BENCHMARKS       FALSE   // For development purposes only. Must be FALSE when building a public release.
                     91: #if PKCS5_BENCHMARKS && HASH_FNC_BENCHMARKS
                     92: #error PKCS5_BENCHMARKS and HASH_FNC_BENCHMARKS are both TRUE (at least one of them should be FALSE).
                     93: #endif
1.1.1.5   root       94: 
                     95: enum 
                     96: {
                     97:        BENCHMARK_SORT_BY_NAME = 0,
                     98:        BENCHMARK_SORT_BY_SPEED
                     99: };
                    100: 
                    101: typedef struct 
                    102: {
                    103:        int id;
                    104:        char name[100];
                    105:        unsigned __int64 encSpeed;
                    106:        unsigned __int64 decSpeed;
                    107:        unsigned __int64 meanBytesPerSec;
                    108: } BENCHMARK_REC;
                    109: 
                    110: BENCHMARK_REC benchmarkTable [BENCHMARK_MAX_ITEMS];
                    111: int benchmarkTotalItems = 0;
                    112: int benchmarkBufferSize = BENCHMARK_DEFAULT_BUF_SIZE;
                    113: int benchmarkLastBufferSize = BENCHMARK_DEFAULT_BUF_SIZE;
                    114: int benchmarkSortMethod = BENCHMARK_SORT_BY_SPEED;
                    115: LARGE_INTEGER benchmarkPerformanceFrequency;
1.1.1.7   root      116: 
1.1.1.5   root      117: #endif // #ifndef SETUP
                    118: 
                    119: 
1.1       root      120: void
                    121: cleanup ()
                    122: {
                    123:        /* Cleanup the GDI fonts */
                    124:        if (hFixedFont != NULL)
                    125:                DeleteObject (hFixedFont);
1.1.1.7   root      126:        if (hFixedDigitFont != NULL)
                    127:                DeleteObject (hFixedDigitFont);
1.1       root      128:        if (hBoldFont != NULL)
                    129:                DeleteObject (hBoldFont);
                    130:        if (hTitleFont != NULL)
                    131:                DeleteObject (hTitleFont);
                    132:        if (hUserFont != NULL)
                    133:                DeleteObject (hUserFont);
                    134:        if (hUserUnderlineFont != NULL)
                    135:                DeleteObject (hUserUnderlineFont);
                    136:        if (hUserBoldFont != NULL)
                    137:                DeleteObject (hUserBoldFont);
1.1.1.5   root      138:        if (hUserUnderlineBoldFont != NULL)
                    139:                DeleteObject (hUserUnderlineBoldFont);
1.1       root      140:        /* Cleanup our dialog class */
                    141:        if (hDlgClass)
                    142:                UnregisterClass (TC_DLG_CLASS, hInst);
                    143:        if (hSplashClass)
                    144:                UnregisterClass (TC_SPLASH_CLASS, hInst);
                    145:        /* Close the device driver handle */
                    146:        if (hDriver != INVALID_HANDLE_VALUE)
                    147:        {
1.1.1.7   root      148:                // Unload driver mode if possible (non-install mode) 
                    149:                if (IsNonInstallMode ())
                    150:                        DriverUnload ();
                    151:                else
                    152:                        CloseHandle (hDriver);
1.1       root      153:        }
                    154: 
                    155:        if (hMutex != NULL)
                    156:        {
                    157:                CloseHandle (hMutex);
                    158:        }
1.1.1.7   root      159: 
                    160:        if (ConfigBuffer != NULL)
                    161:        {
                    162:                free (ConfigBuffer);
                    163:                ConfigBuffer = NULL;
                    164:        }
1.1       root      165: }
                    166: 
                    167: void
                    168: LowerCaseCopy (char *lpszDest, char *lpszSource)
                    169: {
                    170:        int i = strlen (lpszSource);
                    171: 
                    172:        lpszDest[i] = 0;
                    173:        while (--i >= 0)
                    174:        {
                    175:                lpszDest[i] = (char) tolower (lpszSource[i]);
                    176:        }
                    177: 
                    178: }
                    179: 
                    180: void
                    181: UpperCaseCopy (char *lpszDest, char *lpszSource)
                    182: {
                    183:        int i = strlen (lpszSource);
                    184: 
                    185:        lpszDest[i] = 0;
                    186:        while (--i >= 0)
                    187:        {
                    188:                lpszDest[i] = (char) toupper (lpszSource[i]);
                    189:        }
                    190: }
                    191: 
                    192: void
                    193: CreateFullVolumePath (char *lpszDiskFile, char *lpszFileName, BOOL * bDevice)
                    194: {
                    195:        if (strcmp (lpszFileName, "Floppy (A:)") == 0)
                    196:                strcpy (lpszFileName, "\\Device\\Floppy0");
                    197:        else if (strcmp (lpszFileName, "Floppy (B:)") == 0)
                    198:                strcpy (lpszFileName, "\\Device\\Floppy1");
                    199: 
                    200:        UpperCaseCopy (lpszDiskFile, lpszFileName);
                    201: 
                    202:        *bDevice = FALSE;
                    203: 
                    204:        if (memcmp (lpszDiskFile, "\\DEVICE", sizeof (char) * 7) == 0)
                    205:        {
                    206:                *bDevice = TRUE;
                    207:        }
                    208: 
                    209:        strcpy (lpszDiskFile, lpszFileName);
                    210: 
                    211: #if _DEBUG
                    212:        OutputDebugString ("CreateFullVolumePath: ");
                    213:        OutputDebugString (lpszDiskFile);
                    214:        OutputDebugString ("\n");
                    215: #endif
                    216: 
                    217: }
                    218: 
                    219: int
                    220: FakeDosNameForDevice (char *lpszDiskFile, char *lpszDosDevice, char *lpszCFDevice, BOOL bNameOnly)
                    221: {
                    222:        BOOL bDosLinkCreated = TRUE;
                    223:        sprintf (lpszDosDevice, "truecrypt%lu", GetCurrentProcessId ());
                    224: 
                    225:        if (bNameOnly == FALSE)
                    226:                bDosLinkCreated = DefineDosDevice (DDD_RAW_TARGET_PATH, lpszDosDevice, lpszDiskFile);
                    227: 
                    228:        if (bDosLinkCreated == FALSE)
                    229:        {
                    230:                return ERR_OS_ERROR;
                    231:        }
                    232:        else
                    233:                sprintf (lpszCFDevice, "\\\\.\\%s", lpszDosDevice);
                    234: 
                    235:        return 0;
                    236: }
                    237: 
                    238: int
                    239: RemoveFakeDosName (char *lpszDiskFile, char *lpszDosDevice)
                    240: {
                    241:        BOOL bDosLinkRemoved = DefineDosDevice (DDD_RAW_TARGET_PATH | DDD_EXACT_MATCH_ON_REMOVE |
                    242:                        DDD_REMOVE_DEFINITION, lpszDosDevice, lpszDiskFile);
                    243:        if (bDosLinkRemoved == FALSE)
                    244:        {
                    245:                return ERR_OS_ERROR;
                    246:        }
                    247: 
                    248:        return 0;
                    249: }
                    250: 
                    251: 
                    252: void
1.1.1.7   root      253: AbortProcess (char *stringId)
1.1       root      254: {
                    255:        MessageBeep (MB_ICONEXCLAMATION);
1.1.1.7   root      256:        MessageBoxW (NULL, GetString (stringId), lpszTitle, ICON_HAND);
1.1       root      257:        exit (1);
                    258: }
                    259: 
1.1.1.5   root      260: void
                    261: AbortProcessSilent (void)
                    262: {
                    263:        exit (1);
                    264: }
                    265: 
1.1       root      266: void *
                    267: err_malloc (size_t size)
                    268: {
                    269:        void *z = (void *) TCalloc (size);
                    270:        if (z)
                    271:                return z;
1.1.1.7   root      272:        AbortProcess ("OUTOFMEMORY");
1.1       root      273:        return 0;
                    274: }
                    275: 
                    276: char *
                    277: err_strdup (char *lpszText)
                    278: {
                    279:        int j = (strlen (lpszText) + 1) * sizeof (char);
                    280:        char *z = (char *) err_malloc (j);
                    281:        memmove (z, lpszText, j);
                    282:        return z;
                    283: }
                    284: 
1.1.1.5   root      285: DWORD
1.1       root      286: handleWin32Error (HWND hwndDlg)
                    287: {
1.1.1.7   root      288:        PWSTR lpMsgBuf;
1.1       root      289:        DWORD dwError = GetLastError ();
                    290: 
1.1.1.7   root      291:        if (Silent) return dwError;
                    292: 
                    293:        FormatMessageW (
1.1       root      294:                FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
                    295:                              NULL,
                    296:                              dwError,
                    297:                              MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT),       /* Default language */
1.1.1.7   root      298:                              (PWSTR) &lpMsgBuf,
1.1       root      299:                              0,
                    300:                              NULL
                    301:            );
                    302: 
1.1.1.7   root      303:        MessageBoxW (hwndDlg, lpMsgBuf, lpszTitle, ICON_HAND);
1.1       root      304:        LocalFree (lpMsgBuf);
1.1.1.5   root      305: 
1.1.1.7   root      306:        // Device not ready
                    307:        if (dwError == ERROR_NOT_READY)
                    308:                CheckSystemAutoMount();
                    309: 
1.1.1.5   root      310:        return dwError;
1.1       root      311: }
                    312: 
                    313: BOOL
1.1.1.7   root      314: translateWin32Error (wchar_t *lpszMsgBuf, int nSizeOfBuf)
1.1       root      315: {
                    316:        DWORD dwError = GetLastError ();
                    317: 
1.1.1.7   root      318:        if (FormatMessageW (FORMAT_MESSAGE_FROM_SYSTEM, NULL, dwError,
1.1       root      319:                           MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT),  /* Default language */
                    320:                           lpszMsgBuf, nSizeOfBuf, NULL))
                    321:                return TRUE;
                    322:        else
                    323:                return FALSE;
                    324: }
                    325: 
                    326: /* Except in response to the WM_INITDIALOG message, the dialog box procedure
                    327:    should return nonzero if it processes the message, and zero if it does
                    328:    not. - see DialogProc */
                    329: BOOL WINAPI
                    330: AboutDlgProc (HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam)
                    331: {
                    332:        WORD lw = LOWORD (wParam);
                    333:        if (lParam);            /* remove warning */
                    334: 
                    335:        switch (msg)
                    336:        {
                    337: 
                    338:        case WM_INITDIALOG:
                    339:                {
1.1.1.7   root      340:                        char szTmp[100];
1.1       root      341: 
1.1.1.7   root      342:                        LocalizeDialog (hwndDlg, "IDD_ABOUT_DLG");
1.1       root      343: 
1.1.1.8 ! root      344:                        SetWindowText (GetDlgItem (hwndDlg, IDC_HOMEPAGE), "www.truecrypt.org");
1.1.1.5   root      345:                        SendMessage (GetDlgItem (hwndDlg, IDC_HOMEPAGE), WM_SETFONT, (WPARAM) hUserUnderlineFont, 0);
                    346: 
                    347:                        // Version
                    348:                        SendMessage (GetDlgItem (hwndDlg, IDT_ABOUT_VERSION), WM_SETFONT, (WPARAM) hUserBoldFont, 0);
1.1       root      349:                        sprintf (szTmp, "TrueCrypt %s", VERSION_STRING);
1.1.1.7   root      350: #ifdef _DEBUG
                    351:                        strcat (szTmp, "  (debug)");
                    352: #endif
1.1       root      353:                        SetDlgItemText (hwndDlg, IDT_ABOUT_VERSION, szTmp);
1.1.1.5   root      354: 
                    355:                        // Credits
                    356:                        SendMessage (GetDlgItem (hwndDlg, IDC_ABOUT_CREDITS), WM_SETFONT, (WPARAM) hUserFont, (LPARAM) 0);
1.1.1.7   root      357:                        SetWindowText (GetDlgItem (hwndDlg, IDC_ABOUT_CREDITS), "\
1.1.1.5   root      358: Based on E4M by Paul Le Roux.\r\n\
                    359: Portions of this software are based in part on the works of the following people: \
                    360: Bruce Schneier, \
                    361: Horst Feistel, Don Coppersmith, \
                    362: Whitfield Diffie, Martin Hellman, Walt Tuchmann, \
                    363: Joan Daemen, Vincent Rijmen, \
                    364: Lars Knudsen, Ross Anderson, Eli Biham, \
                    365: David Wagner, John Kelsey, Niels Ferguson, Doug Whiting, Chris Hall, \
                    366: Carlisle Adams, Stafford Tavares, \
                    367: Hans Dobbertin, Antoon Bosselaers, Bart Preneel, \
1.1.1.7   root      368: Paulo Barreto, Dag Arne Osvik, Wei Dai, Peter Gutmann, and many others.\r\n\r\n\
1.1.1.5   root      369: Portions of this software:\r\n\
1.1.1.6   root      370: Copyright \xA9 2004-2005 TrueCrypt Foundation. All Rights Reserved.\r\n\
1.1.1.5   root      371: Copyright \xA9 1998-2000 Paul Le Roux. All Rights Reserved.\r\n\
                    372: Copyright \xA9 2004 TrueCrypt Team. All Rights Reserved.\r\n\
1.1.1.8 ! root      373: Copyright \xA9 1999-2005 Dr. Brian Gladman. All Rights Reserved.\r\n\
1.1.1.5   root      374: Copyright \xA9 1995-1997 Eric Young. All Rights Reserved.\r\n\
1.1.1.7   root      375: Copyright \xA9 2001 Markus Friedl. All Rights Reserved.\r\n\r\n\
1.1.1.5   root      376: A TrueCrypt Foundation Release");
                    377: 
1.1       root      378:                        return 1;
                    379:                }
                    380: 
                    381:        case WM_COMMAND:
                    382:                if (lw == IDOK || lw == IDCANCEL)
                    383:                {
                    384:                        EndDialog (hwndDlg, 0);
                    385:                        return 1;
                    386:                }
                    387: 
1.1.1.5   root      388:                if (lw == IDC_HOMEPAGE)
                    389:                {
                    390:                        char tmpstr [256];
                    391: 
                    392:                        ArrowWaitCursor ();
1.1.1.7   root      393:                        sprintf (tmpstr, "http://www.truecrypt.org/applink.php?version=%s", VERSION_STRING);
1.1.1.5   root      394:                        ShellExecute (NULL, "open", (LPCTSTR) tmpstr, NULL, NULL, SW_SHOWNORMAL);
                    395:                        Sleep (200);
                    396:                        NormalCursor ();
                    397:                        return 1;
                    398:                }
                    399: 
                    400:                // Disallow modification of credits
                    401:                if (HIWORD (wParam) == EN_UPDATE)
                    402:                {
                    403:                        SendMessage (hwndDlg, WM_INITDIALOG, 0, 0);
                    404:                        return 1;
                    405:                }
                    406: 
1.1       root      407:                return 0;
                    408: 
                    409:        case WM_CLOSE:
                    410:                EndDialog (hwndDlg, 0);
                    411:                return 1;
                    412:        }
                    413: 
                    414:        return 0;
                    415: }
                    416: 
                    417: 
                    418: BOOL
                    419: IsButtonChecked (HWND hButton)
                    420: {
                    421:        if (SendMessage (hButton, BM_GETCHECK, 0, 0) == BST_CHECKED)
                    422:                return TRUE;
                    423:        else
                    424:                return FALSE;
                    425: }
                    426: 
                    427: void
                    428: CheckButton (HWND hButton)
                    429: {
                    430:        SendMessage (hButton, BM_SETCHECK, BST_CHECKED, 0);
                    431: }
                    432: 
                    433: 
                    434: /*****************************************************************************
                    435:   ToSBCS: converts a unicode string to Single Byte Character String (SBCS).
                    436:   ***************************************************************************/
                    437: 
                    438: void
                    439: ToSBCS (LPWSTR lpszText)
                    440: {
                    441:        int j = wcslen (lpszText);
                    442:        if (j == 0)
                    443:        {
                    444:                strcpy ((char *) lpszText, "");
                    445:                return;
                    446:        }
                    447:        else
                    448:        {
                    449:                char *lpszNewText = (char *) err_malloc (j + 1);
                    450:                j = WideCharToMultiByte (CP_ACP, 0L, lpszText, -1, lpszNewText, j + 1, NULL, NULL);
                    451:                if (j > 0)
                    452:                        strcpy ((char *) lpszText, lpszNewText);
                    453:                else
                    454:                        strcpy ((char *) lpszText, "");
                    455:                free (lpszNewText);
                    456:        }
                    457: }
                    458: 
                    459: /*****************************************************************************
                    460:   ToUNICODE: converts a SBCS string to a UNICODE string.
                    461:   ***************************************************************************/
                    462: 
                    463: void
                    464: ToUNICODE (char *lpszText)
                    465: {
                    466:        int j = strlen (lpszText);
                    467:        if (j == 0)
                    468:        {
                    469:                wcscpy ((LPWSTR) lpszText, (LPWSTR) WIDE (""));
                    470:                return;
                    471:        }
                    472:        else
                    473:        {
                    474:                LPWSTR lpszNewText = (LPWSTR) err_malloc ((j + 1) * 2);
                    475:                j = MultiByteToWideChar (CP_ACP, 0L, lpszText, -1, lpszNewText, j + 1);
                    476:                if (j > 0)
                    477:                        wcscpy ((LPWSTR) lpszText, lpszNewText);
                    478:                else
                    479:                        wcscpy ((LPWSTR) lpszText, (LPWSTR) "");
                    480:                free (lpszNewText);
                    481:        }
                    482: }
                    483: 
                    484: /* InitDialog - initialize the applications main dialog, this function should
                    485:    be called only once in the dialogs WM_INITDIALOG message handler */
                    486: void
                    487: InitDialog (HWND hwndDlg)
                    488: {
1.1.1.7   root      489:        NONCLIENTMETRICSW metric;
                    490:        static BOOL aboutMenuAppended = FALSE;
                    491: 
1.1       root      492:        HDC hDC;
                    493:        int nHeight;
1.1.1.7   root      494:        LOGFONTW lf;
1.1       root      495:        HMENU hMenu;
1.1.1.7   root      496:        Font *font;
1.1       root      497: 
                    498:        hDC = GetDC (hwndDlg);
                    499: 
1.1.1.7   root      500:        // Normal
                    501:        font = GetFont ("font_normal");
                    502: 
                    503:        metric.cbSize = sizeof (metric);
                    504:        SystemParametersInfoW (SPI_GETNONCLIENTMETRICS, sizeof(metric), &metric, 0);
                    505: 
                    506:        metric.lfMessageFont.lfHeight = !font ? -11 : -font->Size;
                    507:        metric.lfMessageFont.lfWidth = 0;
                    508: 
                    509:        if (font && wcscmp (font->FaceName, L"default") != 0)
                    510:                wcsncpy ((WCHAR *)metric.lfMessageFont.lfFaceName,
                    511:                font->FaceName, sizeof (metric.lfMessageFont.lfFaceName)/2);
                    512: 
                    513:        hUserFont = CreateFontIndirectW (&metric.lfMessageFont);
                    514: 
                    515:        metric.lfMessageFont.lfUnderline = TRUE;
                    516:        hUserUnderlineFont = CreateFontIndirectW (&metric.lfMessageFont);
                    517: 
                    518:        metric.lfMessageFont.lfUnderline = FALSE;
                    519:        metric.lfMessageFont.lfWeight = FW_BOLD;
                    520:        hUserBoldFont = CreateFontIndirectW (&metric.lfMessageFont);
                    521: 
                    522:        metric.lfMessageFont.lfUnderline = TRUE;
                    523:        metric.lfMessageFont.lfWeight = FW_BOLD;
                    524:        hUserUnderlineBoldFont = CreateFontIndirectW (&metric.lfMessageFont);
                    525: 
                    526:        // Fixed digits
                    527:        nHeight = -((9 * GetDeviceCaps (hDC, LOGPIXELSY)) / 72);
1.1       root      528:        lf.lfHeight = nHeight;
                    529:        lf.lfWidth = 0;
                    530:        lf.lfEscapement = 0;
                    531:        lf.lfOrientation = 0;
1.1.1.7   root      532:        lf.lfWeight = FW_NORMAL;
1.1       root      533:        lf.lfItalic = FALSE;
                    534:        lf.lfUnderline = FALSE;
                    535:        lf.lfStrikeOut = FALSE;
                    536:        lf.lfCharSet = DEFAULT_CHARSET;
                    537:        lf.lfOutPrecision = OUT_DEFAULT_PRECIS;
                    538:        lf.lfClipPrecision = CLIP_DEFAULT_PRECIS;
                    539:        lf.lfQuality = PROOF_QUALITY;
                    540:        lf.lfPitchAndFamily = FF_DONTCARE;
1.1.1.7   root      541:        wcscpy (lf.lfFaceName, L"Courier New");
                    542:        hFixedDigitFont = CreateFontIndirectW (&lf);
                    543:        if (hFixedDigitFont == NULL)
1.1       root      544:        {
                    545:                handleWin32Error (hwndDlg);
1.1.1.7   root      546:                AbortProcess ("NOFONT");
1.1       root      547:        }
                    548: 
1.1.1.7   root      549:        // Bold
                    550:        font = GetFont ("font_bold");
1.1       root      551: 
1.1.1.7   root      552:        nHeight = -(((!font ? 10 : font->Size) * GetDeviceCaps (hDC, LOGPIXELSY)) / 72);
1.1       root      553:        lf.lfHeight = nHeight;
1.1.1.7   root      554:        lf.lfWeight = FW_BLACK;
                    555:        wcsncpy (lf.lfFaceName, !font ? L"Arial" : font->FaceName, sizeof (lf.lfFaceName)/2);
                    556:        hBoldFont = CreateFontIndirectW (&lf);
1.1       root      557:        if (hBoldFont == NULL)
                    558:        {
                    559:                handleWin32Error (hwndDlg);
1.1.1.7   root      560:                AbortProcess ("NOFONT");
1.1       root      561:        }
                    562: 
1.1.1.7   root      563:        // Title
                    564:        font = GetFont ("font_title");
                    565: 
                    566:        nHeight = -(((!font ? 16 : font->Size) * GetDeviceCaps (hDC, LOGPIXELSY)) / 72);
1.1       root      567:        lf.lfHeight = nHeight;
                    568:        lf.lfWeight = FW_REGULAR;
1.1.1.7   root      569:        wcsncpy (lf.lfFaceName, !font ? L"Times New Roman" : font->FaceName, sizeof (lf.lfFaceName)/2);
                    570:        hTitleFont = CreateFontIndirectW (&lf);
1.1       root      571:        if (hTitleFont == NULL)
                    572:        {
                    573:                handleWin32Error (hwndDlg);
1.1.1.7   root      574:                AbortProcess ("NOFONT");
1.1       root      575:        }
                    576: 
1.1.1.7   root      577:        // Fixed
                    578:        font = GetFont ("font_fixed");
                    579: 
                    580:        nHeight = -(((!font ? 9 : font->Size) * GetDeviceCaps (hDC, LOGPIXELSY)) / 72);
1.1       root      581:        lf.lfHeight = nHeight;
                    582:        lf.lfWidth = 0;
                    583:        lf.lfEscapement = 0;
                    584:        lf.lfOrientation = 0;
                    585:        lf.lfWeight = FW_NORMAL;
                    586:        lf.lfItalic = FALSE;
                    587:        lf.lfUnderline = FALSE;
                    588:        lf.lfStrikeOut = FALSE;
                    589:        lf.lfCharSet = DEFAULT_CHARSET;
                    590:        lf.lfOutPrecision = OUT_DEFAULT_PRECIS;
                    591:        lf.lfClipPrecision = CLIP_DEFAULT_PRECIS;
                    592:        lf.lfQuality = PROOF_QUALITY;
                    593:        lf.lfPitchAndFamily = FF_DONTCARE;
1.1.1.7   root      594:        wcsncpy (lf.lfFaceName, !font ? L"Lucida Console" : font->FaceName, sizeof (lf.lfFaceName)/2);
                    595:        hFixedFont = CreateFontIndirectW (&lf);
1.1       root      596:        if (hFixedFont == NULL)
                    597:        {
                    598:                handleWin32Error (hwndDlg);
1.1.1.7   root      599:                AbortProcess ("NOFONT");
1.1       root      600:        }
                    601: 
1.1.1.7   root      602:        if (!aboutMenuAppended)
                    603:        {
                    604:                hMenu = GetSystemMenu (hwndDlg, FALSE);
                    605:                AppendMenu (hMenu, MF_SEPARATOR, 0, NULL);
                    606:                AppendMenuW (hMenu, MF_ENABLED | MF_STRING, IDC_ABOUT, GetString ("ABOUTBOX"));
1.1.1.5   root      607: 
1.1.1.7   root      608:                aboutMenuAppended = TRUE;
                    609:        }
1.1       root      610: }
                    611: 
                    612: HDC
                    613: CreateMemBitmap (HINSTANCE hInstance, HWND hwnd, char *resource)
                    614: {
                    615:        HBITMAP picture = LoadBitmap (hInstance, resource);
                    616:        HDC viewDC = GetDC (hwnd), dcMem;
                    617: 
                    618:        dcMem = CreateCompatibleDC (viewDC);
                    619: 
                    620:        SetMapMode (dcMem, MM_TEXT);
                    621: 
                    622:        SelectObject (dcMem, picture);
                    623: 
                    624:        ReleaseDC (hwnd, viewDC);
                    625: 
                    626:        return dcMem;
                    627: }
                    628: 
                    629: /* Draw the specified bitmap at the specified location - Stretch to fit. */
                    630: void
                    631: PaintBitmap (HDC pdcMem, int x, int y, int nWidth, int nHeight, HDC hDC)
                    632: {
                    633:        HGDIOBJ picture = GetCurrentObject (pdcMem, OBJ_BITMAP);
                    634: 
                    635:        BITMAP bitmap;
                    636:        GetObject (picture, sizeof (BITMAP), &bitmap);
                    637: 
                    638:        BitBlt (hDC, x, y, nWidth, nHeight, pdcMem, 0, 0, SRCCOPY);
                    639: }
                    640: 
1.1.1.7   root      641: 
1.1       root      642: LRESULT CALLBACK
1.1.1.7   root      643: RedTick (HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1.1       root      644: {
1.1.1.7   root      645:   if (uMsg == WM_CREATE)
                    646:     {
                    647:     }
                    648:   else if (uMsg == WM_DESTROY)
                    649:     {
                    650:     }
                    651:   else if (uMsg == WM_TIMER)
                    652:     {
                    653:     }
                    654:   else if (uMsg == WM_PAINT)
                    655:     {
                    656:       PAINTSTRUCT tmp;
                    657:       HPEN hPen;
                    658:       HDC hDC;
                    659:       BOOL bEndPaint;
                    660:       RECT Rect;
                    661: 
                    662:       if (GetUpdateRect (hwnd, NULL, FALSE))
                    663:        {
                    664:          hDC = BeginPaint (hwnd, &tmp);
                    665:          bEndPaint = TRUE;
                    666:          if (hDC == NULL)
                    667:            return DefWindowProc (hwnd, uMsg, wParam, lParam);
                    668:        }
                    669:       else
                    670:        {
                    671:          hDC = GetDC (hwnd);
                    672:          bEndPaint = FALSE;
                    673:        }
                    674: 
                    675:       GetClientRect (hwnd, &Rect);
                    676: 
                    677:       hPen = CreatePen (PS_SOLID, 2, RGB (0, 255, 0));
                    678:       if (hPen != NULL)
                    679:        {
                    680:          HGDIOBJ hObj = SelectObject (hDC, hPen);
                    681:          WORD bx = LOWORD (GetDialogBaseUnits ());
                    682:          WORD by = HIWORD (GetDialogBaseUnits ());
1.1       root      683: 
1.1.1.7   root      684:          MoveToEx (hDC, (Rect.right - Rect.left) / 2, Rect.bottom, NULL);
                    685:          LineTo (hDC, Rect.right, Rect.top);
                    686:          MoveToEx (hDC, (Rect.right - Rect.left) / 2, Rect.bottom, NULL);
1.1       root      687: 
1.1.1.7   root      688:          LineTo (hDC, (3 * bx) / 4, (2 * by) / 8);
1.1       root      689: 
1.1.1.7   root      690:          SelectObject (hDC, hObj);
                    691:          DeleteObject (hPen);
                    692:        }
                    693: 
                    694:       if (bEndPaint)
                    695:        EndPaint (hwnd, &tmp);
                    696:       else
                    697:        ReleaseDC (hwnd, hDC);
1.1       root      698: 
1.1.1.7   root      699:       return TRUE;
                    700:     }
                    701: 
                    702:   return DefWindowProc (hwnd, uMsg, wParam, lParam);
                    703: }
1.1       root      704: 
1.1.1.7   root      705: BOOL
                    706: RegisterRedTick (HINSTANCE hInstance)
                    707: {
                    708:   WNDCLASS wc;
                    709:   ULONG rc;
1.1       root      710: 
1.1.1.7   root      711:   memset(&wc, 0 , sizeof wc);
1.1       root      712: 
1.1.1.7   root      713:   wc.style = CS_HREDRAW | CS_VREDRAW;
                    714:   wc.cbClsExtra = 0;
                    715:   wc.cbWndExtra = 4;
                    716:   wc.hInstance = hInstance;
                    717:   wc.hIcon = LoadIcon (NULL, IDI_APPLICATION);
                    718:   wc.hCursor = NULL;
                    719:   wc.hbrBackground = GetStockObject (LTGRAY_BRUSH);
                    720:   wc.lpszClassName = "REDTICK";
                    721:   wc.lpfnWndProc = &RedTick; 
                    722:   
                    723:   rc = (ULONG) RegisterClass (&wc);
1.1       root      724: 
1.1.1.7   root      725:   return rc == 0 ? FALSE : TRUE;
                    726: }
1.1       root      727: 
1.1.1.7   root      728: BOOL
                    729: UnregisterRedTick (HINSTANCE hInstance)
                    730: {
                    731:   return UnregisterClass ("REDTICK", hInstance);
                    732: }
1.1       root      733: 
1.1.1.7   root      734: LRESULT CALLBACK
                    735: SplashDlgProc (HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
                    736: {
1.1       root      737:        return DefDlgProc (hwnd, uMsg, wParam, lParam);
                    738: }
                    739: 
                    740: void
                    741: WaitCursor ()
                    742: {
                    743:        static HCURSOR hcWait;
                    744:        if (hcWait == NULL)
                    745:                hcWait = LoadCursor (NULL, IDC_WAIT);
                    746:        SetCursor (hcWait);
                    747:        hCursor = hcWait;
                    748: }
                    749: 
                    750: void
                    751: NormalCursor ()
                    752: {
                    753:        static HCURSOR hcArrow;
                    754:        if (hcArrow == NULL)
                    755:                hcArrow = LoadCursor (NULL, IDC_ARROW);
                    756:        SetCursor (hcArrow);
                    757:        hCursor = NULL;
                    758: }
                    759: 
                    760: void
                    761: ArrowWaitCursor ()
                    762: {
                    763:        static HCURSOR hcArrowWait;
                    764:        if (hcArrowWait == NULL)
                    765:                hcArrowWait = LoadCursor (NULL, IDC_APPSTARTING);
                    766:        SetCursor (hcArrowWait);
                    767:        hCursor = hcArrowWait;
                    768: }
1.1.1.7   root      769: void
                    770: AddComboPair (HWND hComboBox, char *lpszItem, int value)
                    771: {
                    772:        LPARAM nIndex;
                    773: 
                    774:        nIndex = SendMessage (hComboBox, CB_ADDSTRING, 0, (LPARAM) lpszItem);
                    775:        nIndex = SendMessage (hComboBox, CB_SETITEMDATA, nIndex, (LPARAM) value);
                    776: }
                    777: 
                    778: void
                    779: AddComboPairW (HWND hComboBox, wchar_t *lpszItem, int value)
                    780: {
                    781:        LPARAM nIndex;
                    782: 
                    783:        nIndex = SendMessageW (hComboBox, CB_ADDSTRING, 0, (LPARAM) lpszItem);
                    784:        nIndex = SendMessage (hComboBox, CB_SETITEMDATA, nIndex, (LPARAM) value);
                    785: }
                    786: 
                    787: void
                    788: SelectAlgo (HWND hComboBox, int *algo_id)
                    789: {
                    790:        LPARAM nCount = SendMessage (hComboBox, CB_GETCOUNT, 0, 0);
                    791:        LPARAM x, i;
                    792: 
                    793:        for (i = 0; i < nCount; i++)
                    794:        {
                    795:                x = SendMessage (hComboBox, CB_GETITEMDATA, i, 0);
                    796:                if (x == (LPARAM) * algo_id)
                    797:                {
                    798:                        SendMessage (hComboBox, CB_SETCURSEL, i, 0);
                    799:                        return;
                    800:                }
                    801:        }
                    802: 
                    803:        /* Something went wrong ; couldn't find the requested algo id so we drop
                    804:           back to a default */
                    805: 
                    806:        *algo_id = SendMessage (hComboBox, CB_GETITEMDATA, 0, 0);
                    807: 
                    808:        SendMessage (hComboBox, CB_SETCURSEL, 0, 0);
                    809: 
                    810: }
1.1       root      811: 
                    812: LRESULT CALLBACK
                    813: CustomDlgProc (HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
                    814: {
                    815:        if (uMsg == WM_SETCURSOR && hCursor != NULL)
                    816:        {
                    817:                SetCursor (hCursor);
                    818:                return TRUE;
                    819:        }
                    820: 
                    821:        return DefDlgProc (hwnd, uMsg, wParam, lParam);
                    822: }
                    823: 
1.1.1.7   root      824: 
1.1       root      825: /* InitApp - initialize the application, this function is called once in the
                    826:    applications WinMain function, but before the main dialog has been created */
                    827: void
                    828: InitApp (HINSTANCE hInstance)
                    829: {
                    830:        WNDCLASS wc;
                    831:        OSVERSIONINFO os;
1.1.1.7   root      832:        char langId[6];
1.1       root      833: 
                    834:        /* Save the instance handle for later */
                    835:        hInst = hInstance;
                    836: 
1.1.1.7   root      837:        SetPreferredLangId (ConfigReadString ("Language", "", langId, sizeof (langId)));
                    838:        
                    839:        if (langId[0] == 0)
                    840:                DialogBoxParamW (hInst, MAKEINTRESOURCEW (IDD_LANGUAGE), NULL,
                    841:                        (DLGPROC) LanguageDlgProc, (LPARAM) 1);
                    842: 
                    843:        LoadLanguageFile ();
                    844: 
1.1       root      845:        /* Pull down the windows version */
                    846:        os.dwOSVersionInfoSize = sizeof (OSVERSIONINFO);
1.1.1.7   root      847: 
1.1       root      848:        if (GetVersionEx (&os) == FALSE)
1.1.1.7   root      849:                AbortProcess ("NO_OS_VER");
                    850: 
                    851:        CurrentOSMajor = os.dwMajorVersion;
                    852:        CurrentOSMinor = os.dwMinorVersion;
                    853: 
                    854:        if (os.dwPlatformId == VER_PLATFORM_WIN32_NT && CurrentOSMajor == 5 && CurrentOSMinor == 0)
                    855:                nCurrentOS = WIN_2000;
                    856:        else if (os.dwPlatformId == VER_PLATFORM_WIN32_NT && CurrentOSMajor == 5 && CurrentOSMinor == 1)
                    857:                nCurrentOS = WIN_XP;
                    858:        else if (os.dwPlatformId == VER_PLATFORM_WIN32_NT && CurrentOSMajor == 5 && CurrentOSMinor == 2)
                    859:                nCurrentOS = WIN_XP64_OR_2003;
                    860:        else if (os.dwPlatformId == VER_PLATFORM_WIN32_NT && CurrentOSMajor == 6 && CurrentOSMinor == 0)
                    861:                nCurrentOS = WIN_VISTA;
                    862:        else if (os.dwPlatformId == VER_PLATFORM_WIN32_NT && CurrentOSMajor == 6 && CurrentOSMinor > 0)
                    863:                nCurrentOS = WIN_AFTER_VISTA;
                    864:        else if (os.dwPlatformId == VER_PLATFORM_WIN32_NT && CurrentOSMajor >= 7)
                    865:                nCurrentOS = WIN_AFTER_VISTA;
                    866:        else if (os.dwPlatformId == VER_PLATFORM_WIN32_NT && CurrentOSMajor == 4)
                    867:                nCurrentOS = WIN_NT4;
1.1       root      868:        else if (os.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS && os.dwMajorVersion == 4 && os.dwMinorVersion == 0)
                    869:                nCurrentOS = WIN_95;
                    870:        else if (os.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS && os.dwMajorVersion == 4 && os.dwMinorVersion == 10)
                    871:                nCurrentOS = WIN_98;
1.1.1.7   root      872:        else if (os.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS && os.dwMajorVersion == 4 && os.dwMinorVersion == 90)
                    873:                nCurrentOS = WIN_ME;
                    874:        else if (os.dwPlatformId == VER_PLATFORM_WIN32s)
                    875:                nCurrentOS = WIN_31;
                    876:        else
                    877:                nCurrentOS = WIN_UNKNOWN;
1.1       root      878: 
1.1.1.6   root      879:        // OS version check
                    880:        if (CurrentOSMajor < 5)
                    881:        {
1.1.1.7   root      882:                MessageBoxW (NULL, GetString ("UNSUPPORTED_OS"), lpszTitle, MB_ICONSTOP);
1.1.1.6   root      883:                exit (1);
                    884:        }
1.1.1.7   root      885:        else
                    886:        {
                    887:                OSVERSIONINFOEX osEx;
                    888: 
                    889:                // Service pack check
                    890:                osEx.dwOSVersionInfoSize = sizeof (OSVERSIONINFOEX);
                    891:                if (GetVersionEx ((LPOSVERSIONINFOA) &osEx) != 0)
                    892:                {
                    893:                        switch (nCurrentOS)
                    894:                        {
                    895:                        case WIN_2000:
                    896:                                if (osEx.wServicePackMajor < 3)
                    897:                                        Warning ("LARGE_IDE_WARNING_2K");
                    898:                                break;
                    899:                        case WIN_XP:
                    900:                                if (osEx.wServicePackMajor < 1)
                    901:                                        Warning ("LARGE_IDE_WARNING_XP");
                    902:                                break;
                    903:                        }
                    904:                }
                    905:        }
1.1       root      906: 
                    907:        /* Get the attributes for the standard dialog class */
                    908:        if ((GetClassInfo (hInst, WINDOWS_DIALOG_CLASS, &wc)) == 0)
1.1.1.7   root      909:                AbortProcess ("INIT_REGISTER");
1.1       root      910: 
                    911: #ifndef SETUP
                    912:        wc.hIcon = LoadIcon (hInstance, MAKEINTRESOURCE (IDI_TRUECRYPT_ICON));
                    913: #else
                    914: #include "../setup/resource.h"
                    915:        wc.hIcon = LoadIcon (hInstance, MAKEINTRESOURCE (IDI_SETUP));
                    916: #endif
                    917:        wc.lpszClassName = TC_DLG_CLASS;
                    918:        wc.lpfnWndProc = &CustomDlgProc;
                    919:        wc.hCursor = LoadCursor (NULL, IDC_ARROW);
                    920:        wc.cbWndExtra = DLGWINDOWEXTRA;
                    921: 
                    922:        hDlgClass = RegisterClass (&wc);
                    923:        if (hDlgClass == 0)
1.1.1.7   root      924:                AbortProcess ("INIT_REGISTER");
1.1       root      925: 
                    926:        wc.lpszClassName = TC_SPLASH_CLASS;
                    927:        wc.lpfnWndProc = &SplashDlgProc;
                    928:        wc.hCursor = LoadCursor (NULL, IDC_ARROW);
                    929:        wc.cbWndExtra = DLGWINDOWEXTRA;
                    930: 
                    931:        hSplashClass = RegisterClass (&wc);
                    932:        if (hSplashClass == 0)
1.1.1.7   root      933:                AbortProcess ("INIT_REGISTER");
                    934: 
                    935:        InitHelpFileName ();
                    936: }
                    937: 
                    938: void InitHelpFileName (void)
                    939: {
                    940:        char *lpszTmp;
1.1       root      941: 
                    942:        GetModuleFileName (NULL, szHelpFile, sizeof (szHelpFile));
                    943:        lpszTmp = strrchr (szHelpFile, '\\');
                    944:        if (lpszTmp)
                    945:        {
1.1.1.7   root      946:                char szTemp[TC_MAX_PATH];
1.1       root      947: 
1.1.1.7   root      948:                // Primary file name
                    949:                if (strcmp (GetPreferredLangId(), "en") == 0
                    950:                        || GetPreferredLangId() == NULL)
1.1       root      951:                {
1.1.1.7   root      952:                        strcpy (++lpszTmp, "TrueCrypt User Guide.pdf");
1.1       root      953:                }
                    954:                else
1.1.1.7   root      955:                {
                    956:                        sprintf (szTemp, "TrueCrypt User Guide.%s.pdf", GetPreferredLangId());
                    957:                        strcpy (++lpszTmp, szTemp);
                    958:                }
                    959: 
                    960:                // Secondary file name (used when localized documentation is not found).
                    961:                GetModuleFileName (NULL, szHelpFile2, sizeof (szHelpFile2));
                    962:                lpszTmp = strrchr (szHelpFile2, '\\');
                    963:                if (lpszTmp)
                    964:                {
                    965:                        strcpy (++lpszTmp, "TrueCrypt User Guide.pdf");
                    966:                }
1.1       root      967:        }
                    968: }
                    969: 
                    970: BOOL
                    971: OpenDevice (char *lpszPath, OPEN_TEST_STRUCT * driver)
                    972: {
                    973:        DWORD dwResult;
                    974:        BOOL bResult;
                    975: 
                    976:        strcpy ((char *) &driver->wszFileName[0], lpszPath);
1.1.1.7   root      977:        ToUNICODE ((char *) &driver->wszFileName[0]);
1.1       root      978: 
                    979:        bResult = DeviceIoControl (hDriver, OPEN_TEST,
                    980:                                   driver, sizeof (OPEN_TEST_STRUCT),
1.1.1.7   root      981:                                   NULL, 0,
1.1       root      982:                                   &dwResult, NULL);
                    983: 
                    984:        if (bResult == FALSE)
                    985:        {
                    986:                dwResult = GetLastError ();
1.1.1.7   root      987: 
1.1       root      988:                if (dwResult == ERROR_SHARING_VIOLATION)
                    989:                        return TRUE;
                    990:                else
                    991:                        return FALSE;
                    992:        }
1.1.1.7   root      993:                
                    994:        return TRUE;
1.1       root      995: }
                    996: 
                    997: 
                    998: int
                    999: GetAvailableFixedDisks (HWND hComboBox, char *lpszRootPath)
                   1000: {
                   1001:        int i, n;
                   1002:        int line = 0;
1.1.1.5   root     1003:        LVITEM LvItem;
1.1.1.6   root     1004:        __int64 deviceSize = 0;
1.1       root     1005: 
                   1006:        for (i = 0; i < 64; i++)
                   1007:        {
                   1008:                BOOL drivePresent = FALSE;
1.1.1.6   root     1009:                BOOL removable = FALSE;
1.1       root     1010: 
1.1.1.5   root     1011:                for (n = 0; n <= 32; n++)
1.1       root     1012:                {
1.1.1.7   root     1013:                        char szTmp[TC_MAX_PATH];
                   1014:                        wchar_t size[100] = {0}, partTypeStr[1024] = {0}, *partType = partTypeStr;
1.1       root     1015:                        OPEN_TEST_STRUCT driver;
                   1016: 
                   1017:                        sprintf (szTmp, lpszRootPath, i, n);
1.1.1.7   root     1018:                        if (OpenDevice (szTmp, &driver))
1.1       root     1019:                        {
                   1020:                                int nDosLinkCreated;
                   1021:                                HANDLE dev;
                   1022:                                DWORD dwResult;
                   1023:                                BOOL bResult;
                   1024:                                PARTITION_INFORMATION diskInfo;
1.1.1.6   root     1025:                                DISK_GEOMETRY driveInfo;
1.1       root     1026:                                char szDosDevice[TC_MAX_PATH], szCFDevice[TC_MAX_PATH];
                   1027: 
                   1028:                                drivePresent = TRUE;
                   1029: 
1.1.1.7   root     1030:                                nDosLinkCreated = FakeDosNameForDevice (szTmp, szDosDevice,
                   1031:                                        szCFDevice, FALSE);
1.1       root     1032: 
1.1.1.7   root     1033:                                dev = CreateFile (szCFDevice, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE , NULL, OPEN_EXISTING, FILE_ATTRIBUTE_READONLY, NULL);
1.1.1.6   root     1034: 
1.1.1.7   root     1035:                                bResult = DeviceIoControl (dev, IOCTL_DISK_GET_PARTITION_INFO, NULL, 0,
                   1036:                                        &diskInfo, sizeof (diskInfo), &dwResult, NULL);
1.1       root     1037: 
1.1.1.7   root     1038:                                // Test if device is removable
                   1039:                                if (n == 0 && DeviceIoControl (dev, IOCTL_DISK_GET_DRIVE_GEOMETRY, NULL, 0,
                   1040:                                        &driveInfo, sizeof (driveInfo), &dwResult, NULL))
                   1041:                                        removable = driveInfo.MediaType == RemovableMedia;
1.1       root     1042: 
1.1.1.7   root     1043:                                RemoveFakeDosName(szTmp, szDosDevice);
                   1044:                                CloseHandle(dev);
1.1.1.6   root     1045: 
1.1.1.7   root     1046:                                if (bResult)
                   1047:                                {
1.1.1.6   root     1048: 
1.1.1.7   root     1049:                                        // System creates a virtual partition1 for some storage devices without
                   1050:                                        // partition table. We try to detect this case by comparing sizes of
                   1051:                                        // partition0 and partition1. If they match, no partition of the device
                   1052:                                        // is displayed to the user to avoid confusion. Drive letter assigned by
                   1053:                                        // system to partition1 is displayed as subitem of partition0
1.1.1.6   root     1054: 
1.1.1.7   root     1055:                                        if (n == 1 && diskInfo.PartitionLength.QuadPart == deviceSize)
                   1056:                                        {
                   1057:                                                char drive[] = { 0, ':', 0 };
                   1058:                                                char device[MAX_PATH * 2];
                   1059:                                                int driveNo;
                   1060: 
                   1061:                                                // Drive letter
                   1062:                                                strcpy (device, szTmp);
                   1063:                                                ToUNICODE (device);
                   1064:                                                driveNo = GetDiskDeviceDriveLetter ((PWSTR) device);
                   1065:                                                drive[0] = driveNo == -1 ? 0 : 'A' + driveNo;
                   1066: 
                   1067:                                                LvItem.iSubItem = 1;
                   1068:                                                LvItem.pszText = drive;
                   1069:                                                SendMessage (hComboBox,LVM_SETITEM,0,(LPARAM)&LvItem);
1.1       root     1070: 
1.1.1.7   root     1071:                                                break;
                   1072:                                        }
1.1       root     1073: 
1.1.1.7   root     1074:                                        switch(diskInfo.PartitionType)
                   1075:                                        {
                   1076:                                        case PARTITION_ENTRY_UNUSED:    partType = GetString ("EMPTY_UNUSED"); break;
                   1077:                                        case PARTITION_XINT13_EXTENDED:
                   1078:                                        case PARTITION_EXTENDED:                partType = L"Extended"; break;
                   1079:                                        case PARTITION_HUGE:                    wsprintfW (partTypeStr, L"%s (0x%02X)", GetString ("UNFORMATTED"), diskInfo.PartitionType); partType = partTypeStr; break;
                   1080:                                        case PARTITION_FAT_12:                  partType = L"FAT12"; break;
                   1081:                                        case PARTITION_FAT_16:                  partType = L"FAT16"; break;
                   1082:                                        case PARTITION_FAT32:           
                   1083:                                        case PARTITION_FAT32_XINT13:    partType = L"FAT32"; break;
                   1084:                                        case 0x08:                                              partType = L"DELL (spanning)"; break;
                   1085:                                        case 0x12:                                              partType = L"Config/diagnostics"; break;
                   1086:                                        case 0x11:
                   1087:                                        case 0x14:
                   1088:                                        case 0x16:
                   1089:                                        case 0x1b:
                   1090:                                        case 0x1c:
                   1091:                                        case 0x1e:                                              partType = L"Hidden FAT"; break;
                   1092:                                        case PARTITION_IFS:                             partType = L"NTFS"; break;
                   1093:                                        case 0x17:                                              partType = L"Hidden NTFS"; break;
                   1094:                                        case 0x3c:                                              partType = L"PMagic recovery"; break;
                   1095:                                        case 0x3d:                                              partType = L"Hidden NetWare"; break;
                   1096:                                        case 0x41:                                              partType = L"Linux/MINIX"; break;
                   1097:                                        case 0x42:                                              partType = L"SFS/LDM/Linux Swap"; break;
                   1098:                                        case 0x51:
                   1099:                                        case 0x64:
                   1100:                                        case 0x65:
                   1101:                                        case 0x66:
                   1102:                                        case 0x67:
                   1103:                                        case 0x68:
                   1104:                                        case 0x69:                                              partType = L"Novell"; break;
                   1105:                                        case 0x55:                                              partType = L"EZ-Drive"; break;
                   1106:                                        case PARTITION_OS2BOOTMGR:              partType = L"OS/2 BM"; break;
                   1107:                                        case PARTITION_XENIX_1:
                   1108:                                        case PARTITION_XENIX_2:                 partType = L"Xenix"; break;
                   1109:                                        case PARTITION_UNIX:                    partType = L"UNIX"; break;
                   1110:                                        case 0x74:                                              partType = L"Scramdisk"; break;
                   1111:                                        case 0x78:                                              partType = L"XOSL FS"; break;
                   1112:                                        case 0x80:
                   1113:                                        case 0x81:                                              partType = L"MINIX"; break;
                   1114:                                        case 0x82:                                              partType = L"Linux Swap"; break;
                   1115:                                        case 0x43:
                   1116:                                        case 0x83:                                              partType = L"Linux"; break;
                   1117:                                        case 0xc2:
                   1118:                                        case 0x93:                                              partType = L"Hidden Linux"; break;
                   1119:                                        case 0x86:
                   1120:                                        case 0x87:                                              partType = L"NTFS volume set"; break;
                   1121:                                        case 0x9f:                                              partType = L"BSD/OS"; break;
                   1122:                                        case 0xa0:
                   1123:                                        case 0xa1:                                              partType = L"Hibernation"; break;
                   1124:                                        case 0xa5:                                              partType = L"BSD"; break;
                   1125:                                        case 0xa8:                                              partType = L"Mac OS-X"; break;
                   1126:                                        case 0xa9:                                              partType = L"NetBSD"; break;
                   1127:                                        case 0xab:                                              partType = L"Mac OS-X Boot"; break;
                   1128:                                        case 0xb8:                                              partType = L"BSDI BSD/386 swap"; break;
                   1129:                                        case 0xc3:                                              partType = L"Hidden Linux swap"; break;
                   1130:                                        case 0xfb:                                              partType = L"VMware"; break;
                   1131:                                        case 0xfc:                                              partType = L"VMware swap"; break;
                   1132:                                        case 0xfd:                                              partType = L"Linux RAID"; break;
                   1133:                                        case 0xfe:                                              partType = L"WinNT hidden"; break;
                   1134:                                        default:                                                wsprintfW (partTypeStr, L"0x%02X", diskInfo.PartitionType); partType = partTypeStr; break;
1.1       root     1135:                                        }
                   1136: 
1.1.1.7   root     1137:                                        GetSizeString (diskInfo.PartitionLength.QuadPart, size);
1.1.1.6   root     1138:                                }
1.1.1.5   root     1139: 
1.1.1.7   root     1140: 
1.1.1.5   root     1141:                                memset (&LvItem,0,sizeof(LvItem));
                   1142:                                LvItem.mask = LVIF_TEXT;   
                   1143:                                LvItem.iItem = line++;   
                   1144: 
                   1145:                                // Device Name
1.1.1.7   root     1146:                                if (n == 0)
                   1147:                                {
                   1148:                                        wchar_t s[1024];
                   1149:                                        deviceSize = diskInfo.PartitionLength.QuadPart;
                   1150:                                        wsprintfW (s, L"Harddisk %d:", i);
                   1151:                                        ListItemAddW (hComboBox, LvItem.iItem, s);
                   1152:                                }
                   1153:                                else
                   1154:                                {
                   1155:                                        LvItem.pszText = szTmp;
                   1156:                                        SendMessage (hComboBox,LVM_INSERTITEM,0,(LPARAM)&LvItem);
                   1157:                                }
1.1.1.5   root     1158: 
                   1159:                                // Size
                   1160:                                LvItem.iSubItem = 2;
1.1.1.7   root     1161:                                LvItem.pszText = (char *) size;
                   1162:                                SendMessageW (hComboBox,LVM_SETITEMW,0,(LPARAM)&LvItem); 
1.1.1.5   root     1163: 
1.1.1.6   root     1164:                                // Device type removable
                   1165:                                if (n == 0 && removable)
                   1166:                                {
1.1.1.7   root     1167:                                        ListSubItemSetW (hComboBox, LvItem.iItem, 3, GetString ("REMOVABLE"));
1.1.1.6   root     1168:                                }
                   1169: 
1.1.1.5   root     1170:                                if (n > 0)
                   1171:                                {
                   1172:                                        char drive[] = { 0, ':', 0 };
                   1173:                                        char device[MAX_PATH * 2];
                   1174:                                        int driveNo;
                   1175: 
                   1176:                                        // Drive letter
                   1177:                                        strcpy (device, szTmp);
                   1178:                                        ToUNICODE (device);
                   1179:                                        driveNo = GetDiskDeviceDriveLetter ((PWSTR) device);
                   1180:                                        drive[0] = driveNo == -1 ? 0 : 'A' + driveNo;
                   1181: 
                   1182:                                        LvItem.iSubItem = 1;
                   1183:                                        LvItem.pszText = drive;
                   1184:                                        SendMessage (hComboBox,LVM_SETITEM,0,(LPARAM)&LvItem);
                   1185: 
                   1186:                                        // Partition type
1.1.1.7   root     1187:                                        ListSubItemSetW (hComboBox, LvItem.iItem, 3, partType);
1.1.1.5   root     1188:                                }
1.1       root     1189: 
1.1.1.6   root     1190:                                // Mark device with partitions, removable drives are not marked to allow
                   1191:                                // users silent overwrite of existing partitions as system does not
                   1192:                                // support partition management of removable drives
                   1193: 
                   1194:                                if (n == 1 && !removable)
1.1.1.5   root     1195:                                {
                   1196:                                        LvItem.iItem = line - 2;
                   1197:                                        LvItem.iSubItem = 3;
                   1198:                                        LvItem.pszText = " ";
                   1199:                                        SendMessage (hComboBox,LVM_SETITEM,0,(LPARAM)&LvItem);
                   1200:                                }
                   1201:                        }
1.1.1.6   root     1202:                        else if (n == 0)
                   1203:                                break;
1.1.1.2   root     1204:                }
                   1205: 
1.1.1.5   root     1206:                if (drivePresent)
1.1.1.2   root     1207:                {
1.1.1.5   root     1208:                        memset (&LvItem,0,sizeof(LvItem));
                   1209:                        LvItem.mask = LVIF_TEXT;   
                   1210:                        LvItem.iItem = line++;   
1.1.1.2   root     1211: 
1.1.1.5   root     1212:                        LvItem.pszText = "";
                   1213:                        SendMessage (hComboBox,LVM_INSERTITEM,0,(LPARAM)&LvItem);
1.1       root     1214:                }
                   1215:        }
                   1216: 
                   1217:        i = SendMessage (hComboBox, LVM_GETITEMCOUNT, 0, 0);
                   1218:        if (i != CB_ERR)
                   1219:                return i;
                   1220:        else
                   1221:                return 0;
                   1222: }
                   1223: 
                   1224: int
                   1225: GetAvailableRemovables (HWND hComboBox, char *lpszRootPath)
                   1226: {
                   1227:        char szTmp[TC_MAX_PATH];
                   1228:        int i;
                   1229:        LVITEM LvItem;
                   1230: 
                   1231:        if (lpszRootPath);      /* Remove unused parameter warning */
                   1232: 
1.1.1.5   root     1233:        memset (&LvItem,0,sizeof(LvItem));
1.1       root     1234:        LvItem.mask = LVIF_TEXT;   
                   1235:        LvItem.iItem = SendMessage (hComboBox, LVM_GETITEMCOUNT, 0, 0)+1;   
                   1236: 
                   1237:        if (QueryDosDevice ("A:", szTmp, sizeof (szTmp)) != 0)
                   1238:        {
1.1.1.5   root     1239:                LvItem.pszText = "\\Device\\Floppy0";
                   1240:                LvItem.iItem = SendMessage (hComboBox,LVM_INSERTITEM,0,(LPARAM)&LvItem);
                   1241: 
                   1242:                LvItem.iSubItem = 1;
                   1243:                LvItem.pszText = "A:";
                   1244:                SendMessage (hComboBox,LVM_SETITEM,0,(LPARAM)&LvItem);
                   1245: 
1.1       root     1246:        }
                   1247:        if (QueryDosDevice ("B:", szTmp, sizeof (szTmp)) != 0)
                   1248:        {
1.1.1.5   root     1249:                LvItem.pszText = "\\Device\\Floppy1";
                   1250:                LvItem.iSubItem = 0;
                   1251:                LvItem.iItem = SendMessage (hComboBox, LVM_GETITEMCOUNT, 0, 0)+1;   
                   1252:                LvItem.iItem = SendMessage (hComboBox,LVM_INSERTITEM,0,(LPARAM)&LvItem);
                   1253: 
                   1254:                LvItem.iSubItem = 1;
                   1255:                LvItem.pszText = "B:";
                   1256:                SendMessage (hComboBox,LVM_SETITEM,0,(LPARAM)&LvItem);
1.1       root     1257:        }
                   1258: 
                   1259:        i = SendMessage (hComboBox, LVM_GETITEMCOUNT, 0, 0);
                   1260:        if (i != CB_ERR)
                   1261:                return i;
                   1262:        else
                   1263:                return 0;
                   1264: }
                   1265: 
1.1.1.7   root     1266: BOOL WINAPI LegalNoticesDlgProc (HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam)
1.1       root     1267: {
                   1268:        WORD lw = LOWORD (wParam);
                   1269:        if (lParam);            /* remove warning */
                   1270: 
                   1271:        switch (msg)
                   1272:        {
1.1.1.7   root     1273: 
1.1       root     1274:        case WM_INITDIALOG:
                   1275:                {
1.1.1.7   root     1276:                        LocalizeDialog (hwndDlg, "IDD_LEGAL_NOTICES_DLG");
                   1277:                        SetWindowText (GetDlgItem (hwndDlg, IDC_LEGAL_NOTICES), GetLegalNotices ());
                   1278:                        return 1;
                   1279:                }
1.1       root     1280: 
1.1.1.7   root     1281:        case WM_COMMAND:
                   1282:                if (lw == IDOK || lw == IDCANCEL)
                   1283:                {
                   1284:                        EndDialog (hwndDlg, 0);
                   1285:                        return 1;
                   1286:                }
1.1       root     1287: 
1.1.1.7   root     1288:                // Disallow modification
                   1289:                if (HIWORD (wParam) == EN_UPDATE)
                   1290:                {
                   1291:                        SendMessage (hwndDlg, WM_INITDIALOG, 0, 0);
                   1292:                        return 1;
                   1293:                }
                   1294: 
                   1295:                return 0;
                   1296: 
                   1297:        case WM_CLOSE:
                   1298:                EndDialog (hwndDlg, 0);
                   1299:                return 1;
                   1300:        }
                   1301: 
                   1302:        return 0;
                   1303: }
                   1304: 
                   1305: 
                   1306: char * GetLegalNotices ()
                   1307: {
                   1308:        static char *resource;
                   1309: 
                   1310:        if (resource == NULL)
                   1311:                resource = MapResource ("Text", IDR_LICENSE, NULL);
                   1312: 
                   1313:        return resource;
                   1314: }
                   1315: 
                   1316: 
                   1317: BOOL WINAPI
                   1318: RawDevicesDlgProc (HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam)
                   1319: {
                   1320:        static char *lpszFileName;
                   1321:        WORD lw = LOWORD (wParam);
                   1322: 
                   1323:        if (lParam);            /* remove warning */
                   1324: 
                   1325:        switch (msg)
                   1326:        {
                   1327:        case WM_INITDIALOG:
                   1328:                {
                   1329:                        int nCount;
                   1330:                        LVCOLUMNW LvCol;
                   1331:                        HWND hList = GetDlgItem (hwndDlg, IDC_DEVICELIST);
                   1332: 
                   1333:                        LocalizeDialog (hwndDlg, "IDD_RAWDEVICES_DLG");
                   1334: 
                   1335:                        SendMessage (hList,LVM_SETEXTENDEDLISTVIEWSTYLE,0,
                   1336:                                LVS_EX_FULLROWSELECT|LVS_EX_HEADERDRAGDROP|LVS_EX_TWOCLICKACTIVATE|LVS_EX_LABELTIP 
                   1337:                                ); 
                   1338: 
                   1339:                        memset (&LvCol,0,sizeof(LvCol));               
                   1340:                        LvCol.mask = LVCF_TEXT|LVCF_WIDTH|LVCF_SUBITEM|LVCF_FMT;  
                   1341:                        LvCol.pszText = GetString ("DEVICE");
                   1342:                        LvCol.cx = 154;
                   1343:                        LvCol.fmt = LVCFMT_LEFT;
                   1344:                        SendMessage (hList,LVM_INSERTCOLUMNW,0,(LPARAM)&LvCol);
1.1.1.5   root     1345: 
1.1.1.7   root     1346:                        LvCol.pszText = GetString ("DRIVE");  
1.1.1.8 ! root     1347:                        LvCol.cx = 49;           
        !          1348:                        LvCol.fmt = LVCFMT_RIGHT;
1.1.1.7   root     1349:                        SendMessage (hList,LVM_INSERTCOLUMNW,1,(LPARAM)&LvCol);
1.1       root     1350: 
1.1.1.7   root     1351:                        LvCol.pszText = GetString ("SIZE");  
1.1.1.8 ! root     1352:                        LvCol.cx = 66;           
1.1       root     1353:                        LvCol.fmt = LVCFMT_RIGHT;
1.1.1.7   root     1354:                        SendMessage (hList,LVM_INSERTCOLUMNW,2,(LPARAM)&LvCol);
1.1       root     1355: 
1.1.1.7   root     1356:                        LvCol.pszText = GetString ("TYPE");  
1.1.1.5   root     1357:                        LvCol.cx = 112;
1.1       root     1358:                        LvCol.fmt = LVCFMT_LEFT;
1.1.1.7   root     1359:                        SendMessage (hList,LVM_INSERTCOLUMNW,3,(LPARAM)&LvCol);
1.1       root     1360: 
                   1361:                        nCount = GetAvailableFixedDisks (hList, "\\Device\\Harddisk%d\\Partition%d");
                   1362:                        nCount += GetAvailableRemovables (hList, "\\Device\\Floppy%d");
                   1363: 
                   1364:                        if (nCount == 0)
                   1365:                        {
                   1366:                                handleWin32Error (hwndDlg);
1.1.1.7   root     1367:                                MessageBoxW (hwndDlg, GetString ("RAWDEVICES"), lpszTitle, ICON_HAND);
1.1       root     1368:                                EndDialog (hwndDlg, IDCANCEL);
                   1369:                        }
                   1370: 
                   1371:                        lpszFileName = (char *) lParam;
                   1372:                        return 1;
                   1373:                }
                   1374: 
                   1375:        case WM_COMMAND:
                   1376:        case WM_NOTIFY:
                   1377:                // catch non-device line selected
                   1378:                if (msg == WM_NOTIFY && ((LPNMHDR) lParam)->code == LVN_ITEMCHANGED && (((LPNMLISTVIEW) lParam)->uNewState & LVIS_FOCUSED ))
                   1379:                {
                   1380:                        LVITEM LvItem;
                   1381:                        memset(&LvItem,0,sizeof(LvItem));
                   1382:                        LvItem.mask = LVIF_TEXT;   
                   1383:                        LvItem.iItem = ((LPNMLISTVIEW) lParam)->iItem;
                   1384:                        LvItem.pszText = lpszFileName;
                   1385:                        LvItem.cchTextMax = TC_MAX_PATH;
                   1386: 
                   1387:                        SendMessage (GetDlgItem (hwndDlg, IDC_DEVICELIST), LVM_GETITEMTEXT, LvItem.iItem, (LPARAM) &LvItem);
                   1388:                        EnableWindow (GetDlgItem ((HWND) hwndDlg, IDOK), lpszFileName[0] != 0 && lpszFileName[0] != ' ');
                   1389:                        return 1;
                   1390:                }
                   1391: 
                   1392:                if (msg == WM_COMMAND && lw == IDOK || msg == WM_NOTIFY && ((NMHDR *)lParam)->code == LVN_ITEMACTIVATE)
                   1393:                {
                   1394:                        LVITEM LvItem;
1.1.1.5   root     1395:                        memset (&LvItem,0,sizeof(LvItem));
1.1       root     1396:                        LvItem.mask = LVIF_TEXT;   
                   1397:                        LvItem.iItem =  SendMessage (GetDlgItem (hwndDlg, IDC_DEVICELIST), LVM_GETSELECTIONMARK, 0, 0);
                   1398:                        LvItem.pszText = lpszFileName;
                   1399:                        LvItem.cchTextMax = TC_MAX_PATH;
                   1400: 
                   1401:                        SendMessage (GetDlgItem (hwndDlg, IDC_DEVICELIST), LVM_GETITEMTEXT, LvItem.iItem, (LPARAM) &LvItem);
                   1402: 
1.1.1.5   root     1403:                        if (lpszFileName[0] == 'H')
                   1404:                        {
                   1405:                                // Whole device selected
                   1406:                                int driveNo;
                   1407: 
                   1408:                                sscanf (lpszFileName, "Harddisk %d", &driveNo);
                   1409:                                sprintf (lpszFileName, "\\Device\\Harddisk%d\\Partition0", driveNo);
                   1410: 
                   1411: #ifdef VOLFORMAT
                   1412:                                // Warn if device contains partitions
                   1413:                                {
1.1.1.7   root     1414:                                        LVITEMW LvItemW;
                   1415:                                        wchar_t tmp[64];
                   1416:                                        memset (&LvItemW, 0, sizeof(LvItemW));
                   1417:                                        LvItemW.mask = LVIF_TEXT;   
                   1418:                                        LvItemW.iItem = LvItem.iItem;
                   1419:                                        LvItemW.iSubItem = 3;
                   1420:                                        LvItemW.pszText = tmp;
                   1421:                                        LvItemW.cchTextMax = sizeof (tmp);
                   1422:                                        SendMessageW (GetDlgItem (hwndDlg, IDC_DEVICELIST), LVM_GETITEMTEXTW, LvItemW.iItem, (LPARAM) &LvItemW);
                   1423: 
                   1424:                                        if (wcscmp (tmp, GetString ("REMOVABLE")) != 0)
                   1425:                                        {
                   1426:                                                // Device with partitions
                   1427:                                                if (wcscmp (tmp, L" ") == 0 &&
                   1428:                                                        AskWarnCancelOk ("DEVICE_PARTITIONS_WARN") == IDCANCEL)
                   1429:                                                        return 1;
                   1430: 
                   1431:                                                if (AskWarnCancelOk ("WHOLE_DEVICE_WARNING") == IDCANCEL)
                   1432:                                                        return 1;
                   1433: 
                   1434:                                                Warning ("WHOLE_DEVICE_NOTE");
                   1435:                                        }
1.1.1.5   root     1436:                                }
                   1437: #endif
                   1438:                        }
                   1439: 
                   1440:                        if (lpszFileName[0] == 0)
1.1.1.7   root     1441:                                return 1; // non-device line selected
1.1       root     1442: 
                   1443:                        EndDialog (hwndDlg, IDOK);
1.1.1.7   root     1444:                        return 1;
1.1       root     1445:                }
                   1446: 
                   1447:                if (lw == IDCANCEL)
                   1448:                {
                   1449:                        EndDialog (hwndDlg, IDCANCEL);
1.1.1.7   root     1450:                        return 1;
1.1       root     1451:                }
                   1452:                return 0;
                   1453:        }
                   1454:        return 0;
                   1455: }
                   1456: 
1.1.1.6   root     1457: 
                   1458: // Install and start driver service and mark it for removal (non-install mode)
                   1459: static int DriverLoad ()
                   1460: {
                   1461:        HANDLE file;
                   1462:        WIN32_FIND_DATA find;
                   1463:        SC_HANDLE hManager, hService = NULL;
                   1464:        char driverPath[TC_MAX_PATH*2];
                   1465:        BOOL res;
                   1466:        char *tmp;
                   1467: 
                   1468:        GetModuleFileName (NULL, driverPath, sizeof (driverPath));
                   1469:        tmp = strrchr (driverPath, '\\');
                   1470:        if (!tmp)
                   1471:        {
                   1472:                strcpy (driverPath, ".");
                   1473:                tmp = driverPath + 1;
                   1474:        }
                   1475: 
1.1.1.7   root     1476:        strcpy (tmp, !Is64BitOs () ? "\\truecrypt.sys" : "\\truecrypt-x64.sys");
1.1.1.6   root     1477: 
                   1478:        file = FindFirstFile (driverPath, &find);
                   1479: 
                   1480:        if (file == INVALID_HANDLE_VALUE)
                   1481:        {
1.1.1.7   root     1482:                MessageBoxW (0, GetString ("DRIVER_NOT_FOUND"), lpszTitle, ICON_HAND);
1.1.1.6   root     1483:                return ERR_DONT_REPORT;
                   1484:        }
                   1485: 
                   1486:        FindClose (file);
                   1487: 
                   1488:        hManager = OpenSCManager (NULL, NULL, SC_MANAGER_ALL_ACCESS);
                   1489:        if (hManager == NULL)
1.1.1.7   root     1490:        {
                   1491:                if (GetLastError () == ERROR_ACCESS_DENIED)
                   1492:                {
                   1493:                        MessageBoxW (0, GetString ("ADMIN_PRIVILEGES_DRIVER"), lpszTitle, ICON_HAND);
                   1494:                        return ERR_DONT_REPORT;
                   1495:                }
                   1496: 
1.1.1.6   root     1497:                return ERR_OS_ERROR;
1.1.1.7   root     1498:        }
1.1.1.6   root     1499: 
                   1500:        hService = CreateService (hManager, "truecrypt", "truecrypt",
                   1501:                SERVICE_ALL_ACCESS, SERVICE_KERNEL_DRIVER, SERVICE_DEMAND_START, SERVICE_ERROR_NORMAL,
                   1502:                driverPath, NULL, NULL, NULL, NULL, NULL);
                   1503: 
                   1504:        if (hService == NULL)
                   1505:        {
                   1506:                CloseServiceHandle (hManager);
                   1507:                return ERR_OS_ERROR;
                   1508:        }
                   1509: 
                   1510:        res = StartService (hService, 0, NULL);
                   1511: 
                   1512:        DeleteService (hService);
                   1513:        CloseServiceHandle (hManager);
                   1514:        CloseServiceHandle (hService);
                   1515: 
                   1516:        return !res ? ERR_OS_ERROR : ERROR_SUCCESS;
                   1517: }
                   1518: 
                   1519: 
1.1.1.7   root     1520: BOOL DriverUnload ()
                   1521: {
                   1522:        MOUNT_LIST_STRUCT driver;
                   1523:        int refCount;
                   1524:        DWORD dwResult;
                   1525:        BOOL bResult;
                   1526: 
                   1527:        SC_HANDLE hManager, hService = NULL;
                   1528:        BOOL bRet;
                   1529:        SERVICE_STATUS status;
                   1530:        int x;
                   1531: 
                   1532:        if (hDriver == INVALID_HANDLE_VALUE)
                   1533:                return TRUE;
                   1534: 
                   1535:        // Test for mounted volumes
                   1536:        bResult = DeviceIoControl (hDriver, MOUNT_LIST, &driver, sizeof (driver), &driver,
                   1537:                sizeof (driver), &dwResult, NULL);
                   1538: 
                   1539:        if (bResult)
                   1540:        {
                   1541:                if (driver.ulMountedDrives != 0)
                   1542:                        return FALSE;
                   1543:        }
                   1544:        else
                   1545:                return TRUE;
                   1546: 
                   1547:        // Test for any applications attached to driver
                   1548:        refCount = GetDriverRefCount ();
                   1549: 
                   1550:        if (refCount > 1)
                   1551:                return FALSE;
                   1552: 
                   1553:        CloseHandle (hDriver);
                   1554: 
                   1555:        // Stop driver service
                   1556: 
                   1557:        hManager = OpenSCManager (NULL, NULL, SC_MANAGER_ALL_ACCESS);
                   1558:        if (hManager == NULL)
                   1559:                goto error;
                   1560: 
                   1561:        hService = OpenService (hManager, "truecrypt", SERVICE_ALL_ACCESS);
                   1562:        if (hService == NULL)
                   1563:                goto error;
                   1564: 
                   1565:        bRet = QueryServiceStatus (hService, &status);
                   1566:        if (bRet != TRUE)
                   1567:                goto error;
                   1568: 
                   1569:        if (status.dwCurrentState != SERVICE_STOPPED)
                   1570:        {
                   1571:                ControlService (hService, SERVICE_CONTROL_STOP, &status);
                   1572: 
                   1573:                for (x = 0; x < 5; x++)
                   1574:                {
                   1575:                        bRet = QueryServiceStatus (hService, &status);
                   1576:                        if (bRet != TRUE)
                   1577:                                goto error;
                   1578: 
                   1579:                        if (status.dwCurrentState == SERVICE_STOPPED)
                   1580:                                break;
                   1581: 
                   1582:                        Sleep (200);
                   1583:                }
                   1584:        }
                   1585: 
                   1586: error:
                   1587:        if (hService != NULL)
                   1588:                CloseServiceHandle (hService);
                   1589: 
                   1590:        if (hManager != NULL)
                   1591:                CloseServiceHandle (hManager);
                   1592: 
                   1593:        if (status.dwCurrentState == SERVICE_STOPPED)
                   1594:        {
                   1595:                hDriver = INVALID_HANDLE_VALUE;
                   1596:                return TRUE;
                   1597:        }
                   1598: 
                   1599:        return FALSE;
                   1600: }
                   1601: 
                   1602: 
1.1       root     1603: int
                   1604: DriverAttach (void)
                   1605: {
1.1.1.6   root     1606:        /* Try to open a handle to the device driver. It will be closed later. */
1.1       root     1607: 
1.1.1.8 ! root     1608: #ifndef SETUP
        !          1609: retry:
        !          1610: #endif
1.1.1.6   root     1611:        hDriver = CreateFile (WIN32_ROOT_PREFIX, 0, 0, NULL, OPEN_EXISTING, 0, NULL);
1.1       root     1612: 
                   1613:        if (hDriver == INVALID_HANDLE_VALUE)
                   1614:        {
1.1.1.6   root     1615: #ifndef SETUP
1.1.1.8 ! root     1616:                SC_HANDLE hManager, hService = NULL;
        !          1617:                SERVICE_STATUS status;
        !          1618: 
        !          1619:                // Windows lets users log in before all services scheduled to start on
        !          1620:                // boot are started. Retry if the system is running only for a few minutes
        !          1621:                // and the driver is not available.
        !          1622: 
        !          1623:                if ((hManager = OpenSCManager (NULL, NULL, SC_MANAGER_ENUMERATE_SERVICE))
        !          1624:                        && (hService = OpenService (hManager, "truecrypt", SERVICE_QUERY_STATUS))
        !          1625:                        && QueryServiceStatus (hService, &status)
        !          1626:                        && status.dwCurrentState != SERVICE_RUNNING
        !          1627:                        && GetTickCount () < 300 * 1000)
        !          1628:                {
        !          1629:                        if (hService != NULL) CloseServiceHandle (hService);
        !          1630:                        if (hManager != NULL) CloseServiceHandle (hManager);
        !          1631: 
        !          1632:                        Sleep (2000);
        !          1633:                        goto retry;
        !          1634:                }
        !          1635: 
        !          1636:                if (hService != NULL) CloseServiceHandle (hService);
        !          1637:                if (hManager != NULL) CloseServiceHandle (hManager);
        !          1638: 
1.1.1.6   root     1639:                // Attempt to load driver (non-install mode)
1.1.1.8 ! root     1640:                {
        !          1641:                        BOOL res = DriverLoad ();
1.1.1.6   root     1642: 
1.1.1.8 ! root     1643:                        if (res != ERROR_SUCCESS)
        !          1644:                                return res;
1.1.1.6   root     1645: 
1.1.1.8 ! root     1646:                        hDriver = CreateFile (WIN32_ROOT_PREFIX, 0, 0, NULL, OPEN_EXISTING, 0, NULL);
        !          1647:                }
1.1.1.6   root     1648: #endif
                   1649:                if (hDriver == INVALID_HANDLE_VALUE)
                   1650:                        return ERR_OS_ERROR;
1.1       root     1651:        }
1.1.1.7   root     1652: #ifndef SETUP // Don't check version during setup to allow removal of another version
                   1653: 
                   1654:        if (hDriver != INVALID_HANDLE_VALUE)
1.1       root     1655:        {
                   1656:                LONG driver = 0;
                   1657:                DWORD dwResult;
                   1658: 
                   1659:                BOOL bResult = DeviceIoControl (hDriver, DRIVER_VERSION,
                   1660:                                   &driver, 4, &driver, 4, &dwResult, NULL);
                   1661: 
                   1662:                if (bResult == FALSE)
                   1663:                        return ERR_OS_ERROR;
                   1664:                else if (driver != VERSION_NUM)
                   1665:                        return ERR_DRIVER_VERSION;
                   1666:        }
                   1667: #endif
                   1668: 
                   1669:        return 0;
                   1670: }
                   1671: 
1.1.1.5   root     1672: 
                   1673: // Sets file pointer to hidden volume header
                   1674: BOOL SeekHiddenVolHeader (HFILE dev, unsigned __int64 volSize, BOOL deviceFlag)
                   1675: {
                   1676:        LARGE_INTEGER offset, offsetNew;
                   1677: 
                   1678:        if (deviceFlag)
                   1679:        {
                   1680:                // Partition/device
                   1681: 
                   1682:                offset.QuadPart = volSize - HIDDEN_VOL_HEADER_OFFSET;
                   1683: 
                   1684:                if (SetFilePointerEx ((HANDLE) dev, offset, &offsetNew, FILE_BEGIN) == 0)
                   1685:                        return FALSE;
                   1686: 
                   1687:                if (offsetNew.QuadPart != offset.QuadPart)
                   1688:                        return FALSE;
                   1689:        }
                   1690:        else
                   1691:        {
                   1692:                // File-hosted volume
                   1693: 
                   1694:                offset.QuadPart = - HIDDEN_VOL_HEADER_OFFSET;
                   1695: 
                   1696:                if (SetFilePointerEx ((HANDLE) dev, offset, &offsetNew, FILE_END) == 0)
                   1697:                        return FALSE;
                   1698:        }
                   1699: 
                   1700:        return TRUE;
                   1701: }
                   1702: 
1.1       root     1703: BOOL
1.1.1.8 ! root     1704: BrowseFiles (HWND hwndDlg, char *stringId, char *lpszFileName, BOOL keepHistory, BOOL saveMode)
1.1       root     1705: {
1.1.1.7   root     1706:        OPENFILENAMEW ofn;
                   1707:        wchar_t file[TC_MAX_PATH] = { 0 };
                   1708:        wchar_t filter[1024];
                   1709: 
1.1       root     1710:        ZeroMemory (&ofn, sizeof (OPENFILENAME));
                   1711: 
1.1.1.7   root     1712:        *lpszFileName = 0;
                   1713:        ofn.lStructSize = OPENFILENAME_SIZE_VERSION_400;
1.1       root     1714:        ofn.hwndOwner = hwndDlg;
1.1.1.7   root     1715:        wsprintfW (filter, L"%ls (*.*)%c*.*%c%ls (*.tc)%c*.tc%c",
                   1716:                GetString ("ALL_FILES"), 0, 0, GetString ("TC_VOLUMES"), 0, 0);
                   1717:        ofn.lpstrFilter = filter;
1.1       root     1718:        ofn.lpstrCustomFilter = NULL;
                   1719:        ofn.nFilterIndex = 1;
1.1.1.7   root     1720:        ofn.lpstrFile = file;
1.1       root     1721:        ofn.nMaxFile = TC_MAX_PATH;
1.1.1.7   root     1722:        ofn.lpstrFileTitle = NULL;
1.1       root     1723:        ofn.nMaxFileTitle = TC_MAX_PATH;
                   1724:        ofn.lpstrInitialDir = NULL;
1.1.1.7   root     1725:        ofn.lpstrTitle = GetString (stringId);
1.1.1.8 ! root     1726:        ofn.Flags = OFN_HIDEREADONLY
        !          1727:                | OFN_PATHMUSTEXIST
        !          1728:                | (keepHistory ? 0 : OFN_DONTADDTORECENT)
        !          1729:                | (saveMode ? OFN_OVERWRITEPROMPT : 0);
        !          1730:        
        !          1731:        if (!saveMode)
1.1.1.7   root     1732:        {
1.1.1.8 ! root     1733:                if (!GetOpenFileNameW (&ofn))
        !          1734:                        return FALSE;
1.1.1.7   root     1735:        }
1.1       root     1736:        else
1.1.1.7   root     1737:        {
1.1.1.8 ! root     1738:                if (!GetSaveFileNameW (&ofn))
        !          1739:                        return FALSE;
1.1.1.7   root     1740:        }
1.1.1.8 ! root     1741: 
        !          1742:        if (!keepHistory)
        !          1743:                CleanLastVisitedMRU ();
        !          1744: 
        !          1745:        WideCharToMultiByte (CP_ACP, 0, file, -1, lpszFileName, MAX_PATH, NULL, NULL);
        !          1746:        return TRUE;
1.1       root     1747: }
                   1748: 
                   1749: 
1.1.1.7   root     1750: static int CALLBACK
                   1751: BrowseCallbackProc(HWND hwnd,UINT uMsg,LPARAM lp, LPARAM pData) 
1.1.1.6   root     1752: {
1.1.1.7   root     1753:        switch(uMsg) {
                   1754:        case BFFM_INITIALIZED: 
                   1755:        {
                   1756:          /* WParam is TRUE since we are passing a path.
                   1757:           It would be FALSE if we were passing a pidl. */
                   1758:           SendMessage (hwnd,BFFM_SETSELECTION,TRUE,(LPARAM)pData);
                   1759:           break;
                   1760:        }
                   1761: 
                   1762:        case BFFM_SELCHANGED: 
                   1763:        {
                   1764:                char szDir[TC_MAX_PATH];
                   1765: 
                   1766:           /* Set the status window to the currently selected path. */
                   1767:           if (SHGetPathFromIDList((LPITEMIDLIST) lp ,szDir)) 
                   1768:           {
                   1769:                  SendMessage (hwnd,BFFM_SETSTATUSTEXT,0,(LPARAM)szDir);
                   1770:           }
                   1771:           break;
                   1772:        }
                   1773: 
                   1774:        default:
                   1775:           break;
                   1776:        }
                   1777: 
                   1778:        return 0;
                   1779: }
1.1.1.6   root     1780: 
                   1781: 
1.1.1.7   root     1782: BOOL
                   1783: BrowseDirectories (HWND hwndDlg, char *lpszTitle, char *dirName)
                   1784: {
                   1785:        BROWSEINFOW bi;
                   1786:        LPITEMIDLIST pidl;
                   1787:        LPMALLOC pMalloc;
                   1788:        BOOL bOK  = FALSE;
1.1.1.6   root     1789: 
1.1.1.7   root     1790:        if (SUCCEEDED(SHGetMalloc(&pMalloc))) 
1.1.1.6   root     1791:        {
1.1.1.7   root     1792:                ZeroMemory(&bi,sizeof(bi));
                   1793:                bi.hwndOwner = hwndDlg;
                   1794:                bi.pszDisplayName = 0;
                   1795:                bi.lpszTitle = GetString (lpszTitle);
                   1796:                bi.pidlRoot = 0;
                   1797:                bi.ulFlags = BIF_RETURNONLYFSDIRS | BIF_STATUSTEXT /*| BIF_EDITBOX*/;
                   1798:                bi.lpfn = BrowseCallbackProc;
                   1799:                bi.lParam = (LPARAM)dirName;
1.1.1.6   root     1800: 
1.1.1.7   root     1801:                pidl = SHBrowseForFolderW (&bi);
                   1802:                if (pidl!=NULL) 
                   1803:                {
                   1804:                        if (SHGetPathFromIDList(pidl, dirName)) 
                   1805:                        {
                   1806:                                bOK = TRUE;
                   1807:                        }
                   1808: 
                   1809:                        pMalloc->lpVtbl->Free(pMalloc,pidl);
                   1810:                        pMalloc->lpVtbl->Release(pMalloc);
                   1811:                }
1.1.1.6   root     1812:        }
                   1813: 
1.1.1.7   root     1814:        return bOK;
1.1.1.6   root     1815: }
                   1816: 
                   1817: 
1.1       root     1818: void
                   1819: handleError (HWND hwndDlg, int code)
                   1820: {
1.1.1.7   root     1821:        WCHAR szTmp[1024];
                   1822: 
                   1823:        if (Silent) return;
1.1       root     1824: 
                   1825:        switch (code)
                   1826:        {
                   1827:        case ERR_OS_ERROR:
                   1828:                handleWin32Error (hwndDlg);
                   1829:                break;
                   1830:        case ERR_OUTOFMEMORY:
1.1.1.7   root     1831:                MessageBoxW (hwndDlg, GetString ("OUTOFMEMORY"), lpszTitle, ICON_HAND);
1.1       root     1832:                break;
1.1.1.7   root     1833: 
1.1       root     1834:        case ERR_PASSWORD_WRONG:
1.1.1.7   root     1835:                wsprintfW (szTmp, GetString (KeyFilesEnable ? "PASSWORD_OR_KEYFILE_WRONG" : "PASSWORD_WRONG"));
                   1836:                if (CheckCapsLock (hwndDlg, TRUE))
                   1837:                        wcscat (szTmp, GetString ("PASSWORD_WRONG_CAPSLOCK_ON"));
                   1838: 
                   1839:                MessageBoxW (hwndDlg, szTmp, lpszTitle, MB_ICONWARNING);
1.1       root     1840:                break;
1.1.1.7   root     1841: 
1.1       root     1842:        case ERR_VOL_FORMAT_BAD:
1.1.1.7   root     1843:                MessageBoxW (hwndDlg, GetString ("VOL_FORMAT_BAD"), lpszTitle, ICON_HAND);
1.1       root     1844:                break;
                   1845:        case ERR_BAD_DRIVE_LETTER:
1.1.1.7   root     1846:                MessageBoxW (hwndDlg, GetString ("BAD_DRIVE_LETTER"), lpszTitle, ICON_HAND);
1.1       root     1847:                break;
                   1848:        case ERR_DRIVE_NOT_FOUND:
1.1.1.7   root     1849:                MessageBoxW (hwndDlg, GetString ("NOT_FOUND"), lpszTitle, ICON_HAND);
1.1       root     1850:                break;
                   1851:        case ERR_FILES_OPEN:
1.1.1.7   root     1852:                MessageBoxW (hwndDlg, GetString ("OPENFILES_DRIVER"), lpszTitle, ICON_HAND);
1.1       root     1853:                break;
                   1854:        case ERR_FILES_OPEN_LOCK:
1.1.1.7   root     1855:                MessageBoxW (hwndDlg, GetString ("OPENFILES_LOCK"), lpszTitle, ICON_HAND);
1.1       root     1856:                break;
                   1857:        case ERR_VOL_SIZE_WRONG:
1.1.1.7   root     1858:                MessageBoxW (hwndDlg, GetString ("VOL_SIZE_WRONG"), lpszTitle, ICON_HAND);
1.1       root     1859:                break;
                   1860:        case ERR_COMPRESSION_NOT_SUPPORTED:
1.1.1.7   root     1861:                MessageBoxW (hwndDlg, GetString ("COMPRESSION_NOT_SUPPORTED"), lpszTitle, ICON_HAND);
1.1       root     1862:                break;
                   1863:        case ERR_PASSWORD_CHANGE_VOL_TYPE:
1.1.1.7   root     1864:                MessageBoxW (hwndDlg, GetString ("WRONG_VOL_TYPE"), lpszTitle, ICON_HAND);
1.1       root     1865:                break;
                   1866:        case ERR_PASSWORD_CHANGE_VOL_VERSION:
1.1.1.7   root     1867:                MessageBoxW (hwndDlg, GetString ("WRONG_VOL_VERSION"), lpszTitle, ICON_HAND);
1.1       root     1868:                break;
                   1869:        case ERR_VOL_SEEKING:
1.1.1.7   root     1870:                MessageBoxW (hwndDlg, GetString ("VOL_SEEKING"), lpszTitle, ICON_HAND);
1.1       root     1871:                break;
                   1872:        case ERR_VOL_WRITING:
1.1.1.7   root     1873:                MessageBoxW (hwndDlg, GetString ("VOL_WRITING"), lpszTitle, ICON_HAND);
1.1       root     1874:                break;
                   1875:        case ERR_VOL_READING:
1.1.1.7   root     1876:                MessageBoxW (hwndDlg, GetString ("VOL_READING"), lpszTitle, ICON_HAND);
                   1877:                break;
                   1878:        case ERR_CIPHER_INIT_FAILURE:
                   1879:                MessageBoxW (hwndDlg, GetString ("ERR_CIPHER_INIT_FAILURE"), lpszTitle, ICON_HAND);
                   1880:                break;
                   1881:        case ERR_CIPHER_INIT_WEAK_KEY:
                   1882:                MessageBoxW (hwndDlg, GetString ("ERR_CIPHER_INIT_WEAK_KEY"), lpszTitle, ICON_HAND);
1.1       root     1883:                break;
                   1884:        case ERR_VOL_ALREADY_MOUNTED:
1.1.1.7   root     1885:                MessageBoxW (hwndDlg, GetString ("VOL_ALREADY_MOUNTED"), lpszTitle, ICON_HAND);
1.1       root     1886:                break;
                   1887:        case ERR_FILE_OPEN_FAILED:
1.1.1.7   root     1888:                MessageBoxW (hwndDlg, GetString ("FILE_OPEN_FAILED"), lpszTitle, ICON_HAND);
1.1       root     1889:                break;
                   1890:        case ERR_VOL_MOUNT_FAILED:
1.1.1.7   root     1891:                MessageBoxW (hwndDlg, GetString  ("VOL_MOUNT_FAILED"), lpszTitle, ICON_HAND);
1.1       root     1892:                break;
                   1893:        case ERR_NO_FREE_DRIVES:
1.1.1.7   root     1894:                MessageBoxW (hwndDlg, GetString ("NO_FREE_DRIVES"), lpszTitle, ICON_HAND);
1.1       root     1895:                break;
                   1896:        case ERR_INVALID_DEVICE:
1.1.1.7   root     1897:                MessageBoxW (hwndDlg, GetString ("INVALID_DEVICE"), lpszTitle, ICON_HAND);
1.1       root     1898:                break;
                   1899:        case ERR_ACCESS_DENIED:
1.1.1.7   root     1900:                MessageBoxW (hwndDlg, GetString ("ACCESS_DENIED"), lpszTitle, ICON_HAND);
1.1       root     1901:                break;
                   1902: 
                   1903:        case ERR_DRIVER_VERSION:
1.1.1.7   root     1904:                wsprintfW (szTmp, GetString ("DRIVER_VERSION"), VERSION_STRING);
                   1905:                MessageBoxW (hwndDlg, szTmp, lpszTitle, ICON_HAND);
1.1       root     1906:                break;
                   1907: 
                   1908:        case ERR_NEW_VERSION_REQUIRED:
1.1.1.7   root     1909:                MessageBoxW (hwndDlg, GetString ("NEW_VERSION_REQUIRED"), lpszTitle, ICON_HAND);
                   1910:                break;
                   1911: 
                   1912:        case ERR_SELF_TESTS_FAILED:
                   1913:                Error ("ERR_SELF_TESTS_FAILED");
1.1       root     1914:                break;
                   1915: 
1.1.1.6   root     1916:        case ERR_DONT_REPORT:
                   1917:                break;
                   1918: 
1.1       root     1919:        default:
1.1.1.7   root     1920:                wsprintfW (szTmp, GetString ("ERR_UNKNOWN"), code);
                   1921:                MessageBoxW (hwndDlg, szTmp, lpszTitle, ICON_HAND);
1.1       root     1922:        }
                   1923: }
                   1924: 
1.1.1.7   root     1925: static BOOL CALLBACK LocalizeDialogEnum( HWND hwnd, LPARAM font)
1.1       root     1926: {
1.1.1.7   root     1927:        // Localization of controls
1.1       root     1928: 
1.1.1.7   root     1929:        if (LocalizationActive)
1.1       root     1930:        {
1.1.1.7   root     1931:                int ctrlId = GetDlgCtrlID (hwnd);
                   1932:                if (ctrlId != 0)
                   1933:                {
                   1934:                        char name[10] = { 0 };
                   1935:                        GetClassName (hwnd, name, sizeof (name));
1.1.1.5   root     1936: 
1.1.1.7   root     1937:                        if (_stricmp (name, "Button") == 0 || _stricmp (name, "Static") == 0)
                   1938:                        {
                   1939:                                wchar_t *str = GetDictionaryValueByInt (ctrlId);
                   1940:                                if (str != NULL)
                   1941:                                        SetWindowTextW (hwnd, str);
                   1942:                        }
                   1943:                }
                   1944:        }
1.1       root     1945: 
1.1.1.7   root     1946:        // Font
                   1947:        SendMessage (hwnd, WM_SETFONT, (WPARAM) font, 0);
                   1948:        
                   1949:        return TRUE;
                   1950: }
1.1       root     1951: 
1.1.1.7   root     1952: void LocalizeDialog (HWND hwnd, char *stringId)
                   1953: {
                   1954:        SetWindowLongPtr (hwnd, GWLP_USERDATA, (LONG_PTR) 'TRUE');
1.1.1.8 ! root     1955:        SendMessage (hwnd, WM_SETFONT, (WPARAM) hUserFont, 0);
1.1.1.5   root     1956: 
1.1.1.7   root     1957:        if (LocalizationActive && stringId != NULL)
                   1958:                SetWindowTextW (hwnd, GetString (stringId));
1.1       root     1959: 
1.1.1.7   root     1960:        if (hUserFont != 0)
                   1961:                EnumChildWindows (hwnd, LocalizeDialogEnum, (LPARAM) hUserFont);
1.1.1.5   root     1962: }
                   1963: 
                   1964: void OpenVolumeExplorerWindow (int driveNo)
                   1965: {
                   1966:        char dosName[5];
                   1967:        SHFILEINFO fInfo;
                   1968: 
                   1969:        sprintf (dosName, "%c:\\", (char) driveNo + 'A');
                   1970: 
                   1971:        // Force explorer to discover the drive
                   1972:        SHGetFileInfo (dosName, 0, &fInfo, sizeof (fInfo), 0);
                   1973: 
                   1974:        ShellExecute (NULL, "open", dosName, NULL, NULL, SW_SHOWNORMAL);
                   1975: }
                   1976: 
                   1977: static BOOL explorerCloseSent;
                   1978: 
                   1979: static BOOL CALLBACK CloseVolumeExplorerWindowsEnum( HWND hwnd, LPARAM driveNo)
                   1980: {
                   1981:        char get[128], driveStr[10];
                   1982:        HWND h;
                   1983: 
                   1984:        GetClassName (hwnd, get, sizeof get);
                   1985: 
                   1986:        if (strcmp (get, "CabinetWClass") == 0)
                   1987:        {
                   1988:                sprintf (driveStr, "%c:\\", driveNo + 'A');
                   1989: 
                   1990:                // Title bar
                   1991:                GetWindowText (hwnd, get, sizeof get);
                   1992:                if (strstr (get, driveStr) == get)
                   1993:                {
                   1994:                        PostMessage (hwnd, WM_CLOSE, 0, 0);
                   1995:                        explorerCloseSent = TRUE;
                   1996:                        return TRUE;
                   1997:                }
                   1998: 
                   1999:                // URL edit box
                   2000:                h = FindWindowEx (hwnd, 0, "WorkerW", 0);
                   2001:                if (!h) return TRUE;
                   2002:                h = FindWindowEx (h, 0, "ReBarWindow32", 0);
                   2003:                if (!h) return TRUE;
                   2004:                h = FindWindowEx (h, 0, "ComboBoxEx32", 0);
                   2005:                if (h)
                   2006:                {
                   2007:                        SendMessage (h, WM_GETTEXT, sizeof get, (LPARAM) get);
                   2008:                        if (strstr (get, driveStr) == get)
                   2009:                        {
                   2010:                                PostMessage (hwnd, WM_CLOSE, 0, 0);
                   2011:                                explorerCloseSent = TRUE;
                   2012:                        }
                   2013:                }
                   2014:        }
                   2015:        return TRUE;
                   2016: }
                   2017: 
                   2018: BOOL CloseVolumeExplorerWindows (HWND hwnd, int driveNo)
                   2019: {
                   2020:        explorerCloseSent = FALSE;
                   2021:        EnumWindows (CloseVolumeExplorerWindowsEnum, (LPARAM) driveNo);
                   2022: 
                   2023:        return explorerCloseSent;
                   2024: }
                   2025: 
1.1.1.7   root     2026: void GetSizeString (unsigned __int64 size, wchar_t *str)
                   2027: {
                   2028:        static wchar_t *b, *kb, *mb, *gb, *tb, *pb;
                   2029:        static int serNo;
                   2030: 
                   2031:        if (b == NULL || serNo != LocalizationSerialNo)
                   2032:        {
                   2033:                serNo = LocalizationSerialNo;
                   2034:                kb = GetString ("KB");
                   2035:                mb = GetString ("MB");
                   2036:                gb = GetString ("GB");
                   2037:                tb = GetString ("TB");
                   2038:                pb = GetString ("PB");
                   2039:                b = GetString ("BYTE");
                   2040:        }
                   2041: 
                   2042:        if (size > 1024I64*1024*1024*1024*1024*99)
                   2043:                swprintf (str, L"%I64d %s", size/1024/1024/1024/1024/1024, pb);
                   2044:        else if (size > 1024I64*1024*1024*1024*1024)
                   2045:                swprintf (str, L"%.1f %s",(double)(size/1024.0/1024/1024/1024/1024), pb);
                   2046:        else if (size > 1024I64*1024*1024*1024*99)
                   2047:                swprintf (str, L"%I64d %s",size/1024/1024/1024/1024, tb);
                   2048:        else if (size > 1024I64*1024*1024*1024)
                   2049:                swprintf (str, L"%.1f %s",(double)(size/1024.0/1024/1024/1024), tb);
                   2050:        else if (size > 1024I64*1024*1024*99)
                   2051:                swprintf (str, L"%I64d %s",size/1024/1024/1024, gb);
                   2052:        else if (size > 1024I64*1024*1024)
                   2053:                swprintf (str, L"%.1f %s",(double)(size/1024.0/1024/1024), gb);
                   2054:        else if (size > 1024I64*1024*99)
                   2055:                swprintf (str, L"%I64d %s", size/1024/1024, mb);
                   2056:        else if (size > 1024I64*1024)
                   2057:                swprintf (str, L"%.1f %s",(double)(size/1024.0/1024), mb);
                   2058:        else if (size > 1024I64)
                   2059:                swprintf (str, L"%I64d %s", size/1024, kb);
                   2060:        else
                   2061:                swprintf (str, L"%I64d %s", size, b);
                   2062: }
1.1.1.5   root     2063: 
                   2064: #ifndef SETUP
1.1.1.7   root     2065: void GetSpeedString (unsigned __int64 speed, wchar_t *str)
1.1.1.5   root     2066: {
1.1.1.7   root     2067:        static wchar_t *b, *kb, *mb, *gb, *tb, *pb;
                   2068:        static int serNo;
                   2069:        
                   2070:        if (b == NULL || serNo != LocalizationSerialNo)
                   2071:        {
                   2072:                serNo = LocalizationSerialNo;
                   2073:                kb = GetString ("KB_PER_SEC");
                   2074:                mb = GetString ("MB_PER_SEC");
                   2075:                gb = GetString ("GB_PER_SEC");
                   2076:                tb = GetString ("TB_PER_SEC");
                   2077:                pb = GetString ("PB_PER_SEC");
                   2078:                b = GetString ("B_PER_SEC");
                   2079:        }
                   2080: 
1.1.1.5   root     2081:        if (speed > 1024I64*1024*1024*1024*1024*99)
1.1.1.7   root     2082:                swprintf (str, L"%I64d %s", speed/1024/1024/1024/1024/1024, pb);
1.1.1.5   root     2083:        else if (speed > 1024I64*1024*1024*1024*1024)
1.1.1.7   root     2084:                swprintf (str, L"%.1f %s",(double)(speed/1024.0/1024/1024/1024/1024), pb);
1.1.1.5   root     2085:        else if (speed > 1024I64*1024*1024*1024*99)
1.1.1.7   root     2086:                swprintf (str, L"%I64d %s",speed/1024/1024/1024/1024, tb);
1.1.1.5   root     2087:        else if (speed > 1024I64*1024*1024*1024)
1.1.1.7   root     2088:                swprintf (str, L"%.1f %s",(double)(speed/1024.0/1024/1024/1024), tb);
1.1.1.5   root     2089:        else if (speed > 1024I64*1024*1024*99)
1.1.1.7   root     2090:                swprintf (str, L"%I64d %s",speed/1024/1024/1024, gb);
1.1.1.5   root     2091:        else if (speed > 1024I64*1024*1024)
1.1.1.7   root     2092:                swprintf (str, L"%.1f %s",(double)(speed/1024.0/1024/1024), gb);
1.1.1.5   root     2093:        else if (speed > 1024I64*1024*99)
1.1.1.7   root     2094:                swprintf (str, L"%I64d %s", speed/1024/1024, mb);
1.1.1.5   root     2095:        else if (speed > 1024I64*1024)
1.1.1.7   root     2096:                swprintf (str, L"%.1f %s",(double)(speed/1024.0/1024), mb);
                   2097:        else if (speed > 1024I64)
                   2098:                swprintf (str, L"%I64d %s", speed/1024, kb);
1.1.1.5   root     2099:        else
1.1.1.7   root     2100:                swprintf (str, L"%I64d %s", speed, b);
1.1.1.5   root     2101: }
                   2102: 
                   2103: static void DisplayBenchmarkResults (HWND hwndDlg)
                   2104: {
1.1.1.7   root     2105:        wchar_t item1[100]={0};
                   2106:        LVITEMW LvItem;
1.1.1.5   root     2107:        HWND hList = GetDlgItem (hwndDlg, IDC_RESULTS);
                   2108:        int ea, i;
                   2109:        BOOL unsorted = TRUE;
1.1.1.7   root     2110:        BENCHMARK_REC tmp_line;
1.1.1.5   root     2111: 
                   2112:        /* Sort the list */
                   2113: 
                   2114:        switch (benchmarkSortMethod)
                   2115:        {
                   2116:        case BENCHMARK_SORT_BY_SPEED:
                   2117: 
                   2118:                while (unsorted)
                   2119:                {
                   2120:                        unsorted = FALSE;
                   2121:                        for (i = 0; i < benchmarkTotalItems - 1; i++)
                   2122:                        {
                   2123:                                if (benchmarkTable[i].meanBytesPerSec < benchmarkTable[i+1].meanBytesPerSec)
                   2124:                                {
                   2125:                                        unsorted = TRUE;
                   2126:                                        memcpy (&tmp_line, &benchmarkTable[i], sizeof(BENCHMARK_REC));
                   2127:                                        memcpy (&benchmarkTable[i], &benchmarkTable[i+1], sizeof(BENCHMARK_REC));
                   2128:                                        memcpy (&benchmarkTable[i+1], &tmp_line, sizeof(BENCHMARK_REC));
                   2129:                                }
                   2130:                        }
                   2131:                }
                   2132:                break;
                   2133: 
                   2134:        case BENCHMARK_SORT_BY_NAME:
                   2135: 
                   2136:                while (unsorted)
                   2137:                {
                   2138:                        unsorted = FALSE;
                   2139:                        for (i = 0; i < benchmarkTotalItems - 1; i++)
                   2140:                        {
                   2141:                                if (benchmarkTable[i].id > benchmarkTable[i+1].id)
                   2142:                                {
                   2143:                                        unsorted = TRUE;
                   2144:                                        memcpy (&tmp_line, &benchmarkTable[i], sizeof(BENCHMARK_REC));
                   2145:                                        memcpy (&benchmarkTable[i], &benchmarkTable[i+1], sizeof(BENCHMARK_REC));
                   2146:                                        memcpy (&benchmarkTable[i+1], &tmp_line, sizeof(BENCHMARK_REC));
                   2147:                                }
                   2148:                        }
                   2149:                }
                   2150:                break;
                   2151:        }
                   2152:   
                   2153:        /* Render the results */
                   2154: 
                   2155:        SendMessage (hList,LVM_DELETEALLITEMS,0,(LPARAM)&LvItem);
                   2156: 
                   2157:        for (i = 0; i < benchmarkTotalItems; i++)
                   2158:        {
                   2159:                ea = benchmarkTable[i].id;
                   2160: 
                   2161:                memset (&LvItem,0,sizeof(LvItem));
                   2162:                LvItem.mask = LVIF_TEXT;
1.1.1.7   root     2163:                LvItem.iItem = i;
1.1.1.5   root     2164:                LvItem.iSubItem = 0;
1.1.1.7   root     2165:                LvItem.pszText = (LPWSTR) benchmarkTable[i].name;
                   2166:                SendMessageW (hList, LVM_INSERTITEM, 0, (LPARAM)&LvItem); 
1.1.1.5   root     2167: 
1.1.1.7   root     2168: #if PKCS5_BENCHMARKS
                   2169:                wcscpy (item1, L"-");
                   2170: #else
                   2171:                GetSpeedString ((unsigned __int64) (benchmarkLastBufferSize / ((float) benchmarkTable[i].encSpeed / benchmarkPerformanceFrequency.QuadPart)), item1);
                   2172: #endif
1.1.1.5   root     2173:                LvItem.iSubItem = 1;
                   2174:                LvItem.pszText = item1;
                   2175: 
1.1.1.7   root     2176:                SendMessageW (hList, LVM_SETITEMW, 0, (LPARAM)&LvItem); 
                   2177: 
                   2178: #if PKCS5_BENCHMARKS
                   2179:                wcscpy (item1, L"-");
                   2180: #else
                   2181:                GetSpeedString ((unsigned __int64) (benchmarkLastBufferSize / ((float) benchmarkTable[i].decSpeed / benchmarkPerformanceFrequency.QuadPart)), item1);
                   2182: #endif
1.1.1.5   root     2183:                LvItem.iSubItem = 2;
                   2184:                LvItem.pszText = item1;
                   2185: 
1.1.1.7   root     2186:                SendMessageW (hList, LVM_SETITEMW, 0, (LPARAM)&LvItem); 
1.1.1.5   root     2187: 
1.1.1.7   root     2188: #if PKCS5_BENCHMARKS
                   2189:                swprintf (item1, L"%d t", benchmarkTable[i].encSpeed);
                   2190: #else
                   2191:                GetSpeedString (benchmarkTable[i].meanBytesPerSec, item1);
                   2192: #endif
1.1.1.5   root     2193:                LvItem.iSubItem = 3;
                   2194:                LvItem.pszText = item1;
                   2195: 
1.1.1.7   root     2196:                SendMessageW (hList, LVM_SETITEMW, 0, (LPARAM)&LvItem); 
1.1.1.5   root     2197:        }
                   2198: }
                   2199: 
                   2200: static BOOL PerformBenchmark(HWND hwndDlg)
                   2201: {
                   2202:     LARGE_INTEGER performanceCountStart, performanceCountEnd;
                   2203:        BYTE *lpTestBuffer;
1.1.1.8 ! root     2204:        PCRYPTO_INFO ci = NULL;
1.1.1.7   root     2205: #if !(PKCS5_BENCHMARKS || HASH_FNC_BENCHMARKS)
1.1.1.8 ! root     2206:        ci = crypto_open ();
        !          2207:        if (!ci)
        !          2208:                return FALSE;
1.1.1.7   root     2209: #endif
1.1.1.5   root     2210: 
                   2211:        if (QueryPerformanceFrequency (&benchmarkPerformanceFrequency) == 0)
                   2212:        {
1.1.1.7   root     2213:                MessageBoxW (hwndDlg, GetString ("ERR_PERF_COUNTER"), lpszTitle, ICON_HAND);
1.1.1.5   root     2214:                return FALSE;
                   2215:        }
                   2216: 
                   2217:        ArrowWaitCursor ();
                   2218: 
                   2219:        lpTestBuffer = malloc(benchmarkBufferSize - (benchmarkBufferSize % 16));
                   2220:        if (lpTestBuffer == NULL)
                   2221:        {
                   2222:                NormalCursor ();
1.1.1.7   root     2223:                MessageBoxW (hwndDlg, GetString ("ERR_MEM_ALLOC"), lpszTitle, ICON_HAND);
1.1.1.5   root     2224:                return FALSE;
                   2225:        }
                   2226:        VirtualLock (lpTestBuffer, benchmarkBufferSize - (benchmarkBufferSize % 16));
                   2227: 
                   2228:        benchmarkTotalItems = 0;
1.1.1.7   root     2229: 
                   2230: #if HASH_FNC_BENCHMARKS
                   2231: 
                   2232:        /* Measures the speed at which each of the hash algorithms processes the message to produce
                   2233:           a single digest. 
                   2234: 
                   2235:           The hash algorithm benchmarks are included here for development purposes only. Do not enable 
                   2236:           them when building a public release (the benchmark GUI strings wouldn't make sense). */
                   2237: 
                   2238:        {
                   2239:                BYTE *digest [MAX_DIGESTSIZE];
                   2240:                WHIRLPOOL_CTX   wctx;
                   2241:                RMD160_CTX              rctx;
                   2242:                sha1_ctx                sctx;
                   2243:                int hid;
                   2244: 
                   2245:                for (hid = 1; hid <= LAST_PRF_ID; hid++) 
                   2246:                {
                   2247:                        if (QueryPerformanceCounter (&performanceCountStart) == 0)
                   2248:                                goto counter_error;
                   2249: 
                   2250:                        switch (hid)
                   2251:                        {
                   2252:                        case SHA1:
                   2253:                                sha1_begin (&sctx);
                   2254:                                sha1_hash (lpTestBuffer, benchmarkBufferSize, &sctx);
                   2255:                                sha1_end ((unsigned char *) digest, &sctx);
                   2256:                                break;
                   2257: 
                   2258:                        case RIPEMD160:
                   2259:                                RMD160Init(&rctx);
                   2260:                                RMD160Update(&rctx, lpTestBuffer, benchmarkBufferSize);
                   2261:                                RMD160Final((unsigned char *) digest, &rctx);
                   2262:                                break;
                   2263: 
                   2264:                        case WHIRLPOOL:
                   2265:                                WHIRLPOOL_init (&wctx);
                   2266:                                WHIRLPOOL_add (lpTestBuffer, benchmarkBufferSize * 8, &wctx);
                   2267:                                WHIRLPOOL_finalize (&wctx, (unsigned char *) digest);
                   2268:                                break;
                   2269:                        }
                   2270: 
                   2271:                        if (QueryPerformanceCounter (&performanceCountEnd) == 0)
                   2272:                                goto counter_error;
                   2273: 
                   2274:                        benchmarkTable[benchmarkTotalItems].encSpeed = performanceCountEnd.QuadPart - performanceCountStart.QuadPart;
                   2275: 
                   2276:                        benchmarkTable[benchmarkTotalItems].decSpeed = benchmarkTable[benchmarkTotalItems].encSpeed;
                   2277:                        benchmarkTable[benchmarkTotalItems].id = hid;
                   2278:                        benchmarkTable[benchmarkTotalItems].meanBytesPerSec = ((unsigned __int64) (benchmarkBufferSize / ((float) benchmarkTable[benchmarkTotalItems].encSpeed / benchmarkPerformanceFrequency.QuadPart)) + (unsigned __int64) (benchmarkBufferSize / ((float) benchmarkTable[benchmarkTotalItems].decSpeed / benchmarkPerformanceFrequency.QuadPart))) / 2;
                   2279:                        sprintf (benchmarkTable[benchmarkTotalItems].name, "%s", get_hash_algo_name(hid));
                   2280: 
                   2281:                        benchmarkTotalItems++;
                   2282:                }
                   2283:        }
                   2284: 
                   2285: #elif PKCS5_BENCHMARKS // #if HASH_FNC_BENCHMARKS
                   2286: 
                   2287:        /* Measures the time that it takes for the PKCS-5 routine to derive a header key using
                   2288:           each of the implemented PRF algorithms. 
                   2289: 
                   2290:           The PKCS-5 benchmarks are included here for development purposes only. Do not enable 
                   2291:           them when building a public release (the benchmark GUI strings wouldn't make sense). */
                   2292:        {
                   2293:                int thid, i;
                   2294:                char dk[HEADER_DISKKEY];
                   2295:                char *tmp_salt = {"\x00\x11\x22\x33\x44\x55\x66\x77\x88\x99\xAA\xBB\xCC\xDD\xEE\xFF\x01\x23\x45\x67\x89\xAB\xCD\xEF\x00\x11\x22\x33\x44\x55\x66\x77\x88\x99\xAA\xBB\xCC\xDD\xEE\xFF\x01\x23\x45\x67\x89\xAB\xCD\xEF\x00\x11\x22\x33\x44\x55\x66\x77\x88\x99\xAA\xBB\xCC\xDD\xEE\xFF"};
                   2296: 
                   2297:                for (thid = 1; thid <= LAST_PRF_ID; thid++) 
                   2298:                {
                   2299:                        if (QueryPerformanceCounter (&performanceCountStart) == 0)
                   2300:                                goto counter_error;
                   2301: 
                   2302:                        for (i = 1; i <= 5; i++) 
                   2303:                        {
                   2304:                                switch (thid)
                   2305:                                {
                   2306:                                case SHA1:
                   2307:                                        /* PKCS-5 test with HMAC-SHA-1 used as the PRF */
                   2308:                                        derive_key_sha1 ("passphrase-1234567890", 21, tmp_salt, 64, get_pkcs5_iteration_count(thid), dk, HEADER_DISKKEY);
                   2309:                                        break;
                   2310: 
                   2311:                                case RIPEMD160:
                   2312:                                        /* PKCS-5 test with HMAC-RIPEMD-160 used as the PRF */
                   2313:                                        derive_key_ripemd160 ("passphrase-1234567890", 21, tmp_salt, 64, get_pkcs5_iteration_count(thid), dk, HEADER_DISKKEY);
                   2314:                                        break;
                   2315: 
                   2316:                                case WHIRLPOOL:
                   2317:                                        /* PKCS-5 test with HMAC-Whirlpool used as the PRF */
                   2318:                                        derive_key_whirlpool ("passphrase-1234567890", 21, tmp_salt, 64, get_pkcs5_iteration_count(thid), dk, HEADER_DISKKEY);
                   2319:                                        break;
                   2320:                                }
                   2321:                        }
                   2322: 
                   2323:                        if (QueryPerformanceCounter (&performanceCountEnd) == 0)
                   2324:                                goto counter_error;
                   2325: 
                   2326:                        benchmarkTable[benchmarkTotalItems].encSpeed = performanceCountEnd.QuadPart - performanceCountStart.QuadPart;
                   2327:                        benchmarkTable[benchmarkTotalItems].id = thid;
                   2328:                        sprintf (benchmarkTable[benchmarkTotalItems].name, "%s", get_pkcs5_prf_name (thid));
                   2329: 
                   2330:                        benchmarkTotalItems++;
                   2331:                }
                   2332:        }
                   2333: 
                   2334: #else  // #elif PKCS5_BENCHMARKS
                   2335: 
                   2336:        /* Encryption algorithm benchmarks */
1.1.1.8 ! root     2337:                
        !          2338:        for (ci->ea = EAGetFirst(); ci->ea != 0; ci->ea = EAGetNext(ci->ea))
1.1.1.5   root     2339:        {
1.1.1.8 ! root     2340:                if (EAGetFirstMode (ci->ea) != LRW)
        !          2341:                        continue;
        !          2342: 
        !          2343:                EAInit (ci->ea, ci->master_key, ci->ks);
        !          2344: 
        !          2345:                ci->mode = LRW;
        !          2346:                if (!EAInitMode (ci))
        !          2347:                        break;
        !          2348: 
1.1.1.5   root     2349:                if (QueryPerformanceCounter (&performanceCountStart) == 0)
                   2350:                        goto counter_error;
                   2351: 
1.1.1.8 ! root     2352:                EncryptBuffer ((unsigned __int32 *) lpTestBuffer, (unsigned __int64) benchmarkBufferSize, ci);
1.1.1.5   root     2353: 
                   2354:                if (QueryPerformanceCounter (&performanceCountEnd) == 0)
                   2355:                        goto counter_error;
                   2356: 
                   2357:                benchmarkTable[benchmarkTotalItems].encSpeed = performanceCountEnd.QuadPart - performanceCountStart.QuadPart;
                   2358: 
                   2359:                if (QueryPerformanceCounter (&performanceCountStart) == 0)
                   2360:                        goto counter_error;
                   2361: 
1.1.1.8 ! root     2362:                DecryptBuffer ((unsigned __int32 *) lpTestBuffer, (unsigned __int64) benchmarkBufferSize, ci);
        !          2363: 
1.1.1.5   root     2364:                if (QueryPerformanceCounter (&performanceCountEnd) == 0)
                   2365:                        goto counter_error;
                   2366: 
                   2367:                benchmarkTable[benchmarkTotalItems].decSpeed = performanceCountEnd.QuadPart - performanceCountStart.QuadPart;
1.1.1.8 ! root     2368:                benchmarkTable[benchmarkTotalItems].id = ci->ea;
1.1.1.5   root     2369:                benchmarkTable[benchmarkTotalItems].meanBytesPerSec = ((unsigned __int64) (benchmarkBufferSize / ((float) benchmarkTable[benchmarkTotalItems].encSpeed / benchmarkPerformanceFrequency.QuadPart)) + (unsigned __int64) (benchmarkBufferSize / ((float) benchmarkTable[benchmarkTotalItems].decSpeed / benchmarkPerformanceFrequency.QuadPart))) / 2;
1.1.1.8 ! root     2370:                EAGetName (benchmarkTable[benchmarkTotalItems].name, ci->ea);
1.1.1.5   root     2371: 
                   2372:                benchmarkTotalItems++;
                   2373:        }
                   2374: 
1.1.1.7   root     2375: #endif // #elif PKCS5_BENCHMARKS (#else)
                   2376: 
1.1.1.8 ! root     2377:        if (ci)
        !          2378:                crypto_close (ci);
        !          2379: 
1.1.1.5   root     2380:        VirtualUnlock (lpTestBuffer, benchmarkBufferSize - (benchmarkBufferSize % 16));
                   2381: 
                   2382:        free(lpTestBuffer);
                   2383: 
                   2384:        benchmarkLastBufferSize = benchmarkBufferSize;
                   2385: 
                   2386:        DisplayBenchmarkResults(hwndDlg);
                   2387: 
1.1.1.7   root     2388:        EnableWindow (GetDlgItem (hwndDlg, IDC_PERFORM_BENCHMARK), TRUE);
1.1.1.5   root     2389:        EnableWindow (GetDlgItem (hwndDlg, IDCLOSE), TRUE);
                   2390: 
                   2391:        NormalCursor ();
                   2392:        return TRUE;
                   2393: 
                   2394: counter_error:
1.1.1.8 ! root     2395:        
        !          2396:        if (ci)
        !          2397:                crypto_close (ci);
1.1.1.5   root     2398: 
                   2399:        VirtualUnlock (lpTestBuffer, benchmarkBufferSize - (benchmarkBufferSize % 16));
                   2400: 
                   2401:        free(lpTestBuffer);
                   2402: 
                   2403:        NormalCursor ();
                   2404: 
1.1.1.7   root     2405:        EnableWindow (GetDlgItem (hwndDlg, IDC_PERFORM_BENCHMARK), TRUE);
1.1.1.5   root     2406:        EnableWindow (GetDlgItem (hwndDlg, IDCLOSE), TRUE);
                   2407: 
1.1.1.7   root     2408:        MessageBoxW (hwndDlg, GetString ("ERR_PERF_COUNTER"), lpszTitle, ICON_HAND);
1.1.1.5   root     2409:        return FALSE;
                   2410: }
                   2411: 
                   2412: 
                   2413: BOOL WINAPI BenchmarkDlgProc (HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam)
                   2414: {
                   2415:        WORD lw = LOWORD (wParam);
                   2416:        LPARAM nIndex;
                   2417:        HWND hCboxSortMethod = GetDlgItem (hwndDlg, IDC_BENCHMARK_SORT_METHOD);
                   2418:        HWND hCboxBufferSize = GetDlgItem (hwndDlg, IDC_BENCHMARK_BUFFER_SIZE);
                   2419: 
                   2420:        switch (msg)
                   2421:        {
                   2422:        case WM_INITDIALOG:
                   2423:                {
1.1.1.7   root     2424:                        LVCOLUMNW LvCol;
                   2425:                        wchar_t s[128];
1.1.1.5   root     2426:                        HWND hList = GetDlgItem (hwndDlg, IDC_RESULTS);
                   2427: 
1.1.1.7   root     2428:                        LocalizeDialog (hwndDlg, "IDD_BENCHMARK_DLG");
1.1.1.5   root     2429: 
                   2430:                        benchmarkBufferSize = BENCHMARK_DEFAULT_BUF_SIZE;
                   2431:                        benchmarkSortMethod = BENCHMARK_SORT_BY_SPEED;
                   2432: 
                   2433:                        SendMessage (hList,LVM_SETEXTENDEDLISTVIEWSTYLE,0,
1.1.1.7   root     2434:                                LVS_EX_FULLROWSELECT|LVS_EX_HEADERDRAGDROP|LVS_EX_LABELTIP 
1.1.1.5   root     2435:                                ); 
                   2436: 
                   2437:                        memset (&LvCol,0,sizeof(LvCol));               
                   2438:                        LvCol.mask = LVCF_TEXT|LVCF_WIDTH|LVCF_SUBITEM|LVCF_FMT;  
1.1.1.7   root     2439:                        LvCol.pszText = GetString ("ALGORITHM");                           
                   2440:                        LvCol.cx = 114;
1.1.1.5   root     2441:                        LvCol.fmt = LVCFMT_LEFT;
1.1.1.7   root     2442:                        SendMessage (hList,LVM_INSERTCOLUMNW,0,(LPARAM)&LvCol);
1.1.1.5   root     2443: 
1.1.1.7   root     2444:                        LvCol.pszText = GetString ("ENCRYPTION");  
1.1.1.5   root     2445:                        LvCol.cx = 80;           
                   2446:                        LvCol.fmt = LVCFMT_RIGHT;
1.1.1.7   root     2447:                        SendMessageW (hList,LVM_INSERTCOLUMNW,1,(LPARAM)&LvCol);
1.1.1.5   root     2448: 
1.1.1.7   root     2449:                        LvCol.pszText = GetString ("DECRYPTION");  
1.1.1.5   root     2450:                        LvCol.cx = 80;
                   2451:                        LvCol.fmt = LVCFMT_RIGHT;
1.1.1.7   root     2452:                        SendMessageW (hList,LVM_INSERTCOLUMNW,2,(LPARAM)&LvCol);
1.1.1.5   root     2453: 
1.1.1.7   root     2454:                        LvCol.pszText = GetString ("MEAN");  
1.1.1.5   root     2455:                        LvCol.cx = 80;
                   2456:                        LvCol.fmt = LVCFMT_RIGHT;
1.1.1.7   root     2457:                        SendMessageW (hList,LVM_INSERTCOLUMNW,3,(LPARAM)&LvCol);
1.1.1.5   root     2458: 
                   2459:                        /* Combo boxes */
                   2460: 
                   2461:                        // Sort method
                   2462: 
                   2463:                        SendMessage (hCboxSortMethod, CB_RESETCONTENT, 0, 0);
                   2464: 
1.1.1.7   root     2465:                        nIndex = SendMessageW (hCboxSortMethod, CB_ADDSTRING, 0, (LPARAM) GetString ("ALPHABETICAL_CATEGORIZED"));
1.1.1.5   root     2466:                        SendMessage (hCboxSortMethod, CB_SETITEMDATA, nIndex, (LPARAM) 0);
                   2467: 
1.1.1.7   root     2468:                        nIndex = SendMessageW (hCboxSortMethod, CB_ADDSTRING, 0, (LPARAM) GetString ("MEAN_SPEED"));
1.1.1.5   root     2469:                        SendMessage (hCboxSortMethod, CB_SETITEMDATA, nIndex, (LPARAM) 0);
                   2470: 
                   2471:                        SendMessage (hCboxSortMethod, CB_SETCURSEL, 1, 0);              // Default sort method
                   2472: 
                   2473:                        // Buffer size
                   2474: 
                   2475:                        SendMessage (hCboxBufferSize, CB_RESETCONTENT, 0, 0);
                   2476: 
1.1.1.7   root     2477:                        swprintf (s, L"5 %s", GetString ("KB"));
                   2478:                        nIndex = SendMessageW (hCboxBufferSize, CB_ADDSTRING, 0, (LPARAM) s);
1.1.1.5   root     2479:                        SendMessage (hCboxBufferSize, CB_SETITEMDATA, nIndex, (LPARAM) 5 * BYTES_PER_KB);
                   2480: 
1.1.1.7   root     2481:                        swprintf (s, L"100 %s", GetString ("KB"));
                   2482:                        nIndex = SendMessageW (hCboxBufferSize, CB_ADDSTRING, 0, (LPARAM) s);
1.1.1.5   root     2483:                        SendMessage (hCboxBufferSize, CB_SETITEMDATA, nIndex, (LPARAM) 100 * BYTES_PER_KB);
                   2484: 
1.1.1.7   root     2485:                        swprintf (s, L"500 %s", GetString ("KB"));
                   2486:                        nIndex = SendMessageW (hCboxBufferSize, CB_ADDSTRING, 0, (LPARAM) s);
1.1.1.5   root     2487:                        SendMessage (hCboxBufferSize, CB_SETITEMDATA, nIndex, (LPARAM) 500 * BYTES_PER_KB);
                   2488: 
1.1.1.7   root     2489:                        swprintf (s, L"1 %s", GetString ("MB"));
                   2490:                        nIndex = SendMessageW (hCboxBufferSize, CB_ADDSTRING, 0, (LPARAM) s);
1.1.1.5   root     2491:                        SendMessage (hCboxBufferSize, CB_SETITEMDATA, nIndex, (LPARAM) 1 * BYTES_PER_MB);
                   2492: 
1.1.1.7   root     2493:                        swprintf (s, L"5 %s", GetString ("MB"));
                   2494:                        nIndex = SendMessageW (hCboxBufferSize, CB_ADDSTRING, 0, (LPARAM) s);
1.1.1.5   root     2495:                        SendMessage (hCboxBufferSize, CB_SETITEMDATA, nIndex, (LPARAM) 5 * BYTES_PER_MB);
                   2496: 
1.1.1.7   root     2497:                        swprintf (s, L"10 %s", GetString ("MB"));
                   2498:                        nIndex = SendMessageW (hCboxBufferSize, CB_ADDSTRING, 0, (LPARAM) s);
1.1.1.5   root     2499:                        SendMessage (hCboxBufferSize, CB_SETITEMDATA, nIndex, (LPARAM) 10 * BYTES_PER_MB);
                   2500: 
1.1.1.7   root     2501:                        swprintf (s, L"50 %s", GetString ("MB"));
                   2502:                        nIndex = SendMessageW (hCboxBufferSize, CB_ADDSTRING, 0, (LPARAM) s);
1.1.1.5   root     2503:                        SendMessage (hCboxBufferSize, CB_SETITEMDATA, nIndex, (LPARAM) 50 * BYTES_PER_MB);
                   2504: 
1.1.1.7   root     2505:                        swprintf (s, L"100 %s", GetString ("MB"));
                   2506:                        nIndex = SendMessageW (hCboxBufferSize, CB_ADDSTRING, 0, (LPARAM) s);
1.1.1.5   root     2507:                        SendMessage (hCboxBufferSize, CB_SETITEMDATA, nIndex, (LPARAM) 100 * BYTES_PER_MB);
                   2508: 
1.1.1.7   root     2509:                        swprintf (s, L"200 %s", GetString ("MB"));
                   2510:                        nIndex = SendMessageW (hCboxBufferSize, CB_ADDSTRING, 0, (LPARAM) s);
1.1.1.5   root     2511:                        SendMessage (hCboxBufferSize, CB_SETITEMDATA, nIndex, (LPARAM) 200 * BYTES_PER_MB);
                   2512: 
1.1.1.7   root     2513:                        swprintf (s, L"500 %s", GetString ("MB"));
                   2514:                        nIndex = SendMessageW (hCboxBufferSize, CB_ADDSTRING, 0, (LPARAM) s);
1.1.1.5   root     2515:                        SendMessage (hCboxBufferSize, CB_SETITEMDATA, nIndex, (LPARAM) 500 * BYTES_PER_MB);
                   2516: 
1.1.1.7   root     2517:                        swprintf (s, L"1 %s", GetString ("GB"));
                   2518:                        nIndex = SendMessageW (hCboxBufferSize, CB_ADDSTRING, 0, (LPARAM) s);
1.1.1.5   root     2519:                        SendMessage (hCboxBufferSize, CB_SETITEMDATA, nIndex, (LPARAM) 1 * BYTES_PER_GB);
                   2520: 
                   2521:                        SendMessage (hCboxBufferSize, CB_SETCURSEL, 3, 0);              // Default size
                   2522: 
                   2523:                        return 1;
                   2524:                }
                   2525:                break;
                   2526: 
                   2527:        case WM_COMMAND:
                   2528:        case WM_NOTIFY:
                   2529: 
                   2530:                if (lw == IDC_BENCHMARK_SORT_METHOD)
                   2531:                {
                   2532:                        nIndex = SendMessage (hCboxSortMethod, CB_GETCURSEL, 0, 0);
                   2533:                        if (nIndex != benchmarkSortMethod)
                   2534:                        {
                   2535:                                benchmarkSortMethod = nIndex;
                   2536:                                DisplayBenchmarkResults (hwndDlg);
                   2537:                        }
1.1.1.7   root     2538:                        return 1;
1.1.1.5   root     2539:                }
                   2540: 
1.1.1.7   root     2541:                if (lw == IDC_PERFORM_BENCHMARK)
1.1.1.5   root     2542:                {
                   2543:                        nIndex = SendMessage (hCboxBufferSize, CB_GETCURSEL, 0, 0);
                   2544:                        benchmarkBufferSize = SendMessage (hCboxBufferSize, CB_GETITEMDATA, nIndex, 0);
                   2545: 
                   2546:                        if (PerformBenchmark(hwndDlg) == FALSE)
                   2547:                        {
                   2548:                                EndDialog (hwndDlg, IDCLOSE);
                   2549:                        }
1.1.1.7   root     2550:                        return 1;
1.1.1.5   root     2551:                }
1.1.1.7   root     2552:                if (lw == IDCLOSE || lw == IDCANCEL)
1.1.1.5   root     2553:                {
                   2554:                        EndDialog (hwndDlg, IDCLOSE);
1.1.1.7   root     2555:                        return 1;
1.1.1.5   root     2556:                }
                   2557:                return 0;
                   2558: 
                   2559:                break;
                   2560: 
                   2561:        case WM_CLOSE:
                   2562:                EndDialog (hwndDlg, IDCLOSE);
1.1.1.7   root     2563:                return 1;
1.1.1.5   root     2564: 
                   2565:                break;
                   2566: 
                   2567:        }
                   2568:        return 0;
                   2569: }
                   2570: 
                   2571: 
1.1.1.7   root     2572: 
                   2573: /* Except in response to the WM_INITDIALOG message, the dialog box procedure
                   2574:    should return nonzero if it processes the message, and zero if it does
                   2575:    not. - see DialogProc */
                   2576: BOOL WINAPI
                   2577: KeyfileGeneratorDlgProc (HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam)
                   2578: {
                   2579:        WORD lw = LOWORD (wParam);
                   2580:        WORD hw = HIWORD (wParam);
                   2581:        static unsigned char randPool [RNG_POOL_SIZE];
                   2582:        static unsigned char lastRandPool [RNG_POOL_SIZE];
                   2583:        static char outputDispBuffer [RNG_POOL_SIZE*3+34];
                   2584:        static bDisplayPoolContents = TRUE;
                   2585:        int hash_algo = RandGetHashFunction();
                   2586:        int hid;
                   2587: 
                   2588:        if (lParam);            /* remove warning */
                   2589: 
                   2590:        switch (msg)
                   2591:        {
                   2592:        case WM_INITDIALOG:
1.1.1.5   root     2593:                {
1.1.1.7   root     2594:                        HWND hComboBox = GetDlgItem (hwndDlg, IDC_PRF_ID);
                   2595: 
                   2596:                        VirtualLock (randPool, sizeof(randPool));
                   2597:                        VirtualLock (lastRandPool, sizeof(lastRandPool));
                   2598:                        VirtualLock (outputDispBuffer, sizeof(outputDispBuffer));
                   2599: 
                   2600:                        LocalizeDialog (hwndDlg, "IDD_KEYFILE_GENERATOR_DLG");
                   2601: 
                   2602:                        SendMessage (hComboBox, CB_RESETCONTENT, 0, 0);
                   2603:                        for (hid = 1; hid <= LAST_PRF_ID; hid++)
                   2604:                        {
                   2605:                                AddComboPair (hComboBox, get_hash_algo_name(hid), hid);
                   2606:                        }
                   2607:                        SelectAlgo (hComboBox, &hash_algo);
                   2608: 
                   2609:                        SetCheckBox (hwndDlg, IDC_DISPLAY_POOL_CONTENTS, bDisplayPoolContents);
                   2610: 
                   2611: #ifndef VOLFORMAT                      
                   2612:                        if (Randinit ()) 
                   2613:                        {
                   2614:                                Error ("INIT_RAND");
                   2615:                                EndDialog (hwndDlg, IDCLOSE);
                   2616:                        }
                   2617: #endif
                   2618:                        SetTimer (hwndDlg, 0xfd, RANDOM_POOL_DISPLAY_REFRESH_INTERVAL, NULL);
                   2619:                        SendMessage (GetDlgItem (hwndDlg, IDC_POOL_CONTENTS), WM_SETFONT, (WPARAM) hFixedDigitFont, (LPARAM) TRUE);
                   2620:                        return 1;
                   2621:                }
                   2622: 
                   2623:        case WM_TIMER:
                   2624:                {
                   2625:                        char tmp[4];
                   2626:                        int col, row;
                   2627: 
                   2628:                        if (bDisplayPoolContents)
                   2629:                        {
                   2630:                                RandpeekBytes (randPool, sizeof (randPool));
                   2631: 
                   2632:                                if (memcmp (lastRandPool, randPool, sizeof(lastRandPool)) != 0)
                   2633:                                {
                   2634:                                        outputDispBuffer[0] = 0;
                   2635: 
                   2636:                                        for (row = 0; row < 16; row++)
                   2637:                                        {
                   2638:                                                for (col = 0; col < 20; col++)
                   2639:                                                {
                   2640:                                                        sprintf (tmp, "%02X ", randPool[row * 20 + col]);
                   2641:                                                        strcat (outputDispBuffer, tmp);
                   2642:                                                }
                   2643:                                                strcat (outputDispBuffer, "\n");
                   2644:                                        }
                   2645:                                        SetWindowText (GetDlgItem (hwndDlg, IDC_POOL_CONTENTS), outputDispBuffer);
                   2646: 
                   2647:                                        memcpy (lastRandPool, randPool, sizeof(lastRandPool));
                   2648:                                }
                   2649:                        }
                   2650:                        return 1;
                   2651:                }
                   2652: 
                   2653:        case WM_COMMAND:
                   2654: 
                   2655:                if (lw == IDCLOSE || lw == IDCANCEL)
                   2656:                {
                   2657:                        goto exit;
                   2658:                }
                   2659: 
                   2660:                if (lw == IDC_PRF_ID && hw == CBN_SELCHANGE)
                   2661:                {
                   2662:                        hid = (int) SendMessage (GetDlgItem (hwndDlg, IDC_PRF_ID), CB_GETCURSEL, 0, 0);
                   2663:                        hash_algo = (int) SendMessage (GetDlgItem (hwndDlg, IDC_PRF_ID), CB_GETITEMDATA, hid, 0);
                   2664:                        RandSetHashFunction (hash_algo);
                   2665:                        return 1;
                   2666:                }
                   2667: 
                   2668:                if (lw == IDC_DISPLAY_POOL_CONTENTS)
                   2669:                {
                   2670:                        if (!(bDisplayPoolContents = GetCheckBox (hwndDlg, IDC_DISPLAY_POOL_CONTENTS)))
                   2671:                                SetWindowText (GetDlgItem (hwndDlg, IDC_POOL_CONTENTS), "");
                   2672:                }
                   2673: 
                   2674:                if (lw == IDC_GENERATE_AND_SAVE_KEYFILE)
                   2675:                {
                   2676:                        char szFileName [TC_MAX_PATH];
                   2677:                        unsigned char keyfile [MAX_PASSWORD];
                   2678:                        int fhKeyfile = -1;
                   2679: 
                   2680:                        /* Select filename */
1.1.1.8 ! root     2681:                        if (!BrowseFiles (hwndDlg, "OPEN_TITLE", szFileName, FALSE, TRUE))
1.1.1.7   root     2682:                                return 1;
                   2683: 
                   2684:                        /* Conceive the file */
                   2685:                        if ((fhKeyfile = _open(szFileName, _O_CREAT|_O_TRUNC|_O_WRONLY|_O_BINARY, _S_IREAD|_S_IWRITE)) == -1)
                   2686:                        {
                   2687:                                Error ("ERROR_CANNOT_MAKE");
                   2688:                                return 1;
                   2689:                        }
                   2690: 
                   2691:                        /* Generate the keyfile */ 
                   2692:                        WaitCursor();
                   2693:                        RandgetBytes (keyfile, sizeof(keyfile), TRUE);
                   2694:                        NormalCursor();
                   2695: 
                   2696:                        /* Write the keyfile */
                   2697:                        if (_write (fhKeyfile, keyfile, sizeof(keyfile)) == -1)
                   2698:                                handleWin32Error (hwndDlg);
                   2699:                        else
                   2700:                                Info("KEYFILE_CREATED");
                   2701: 
                   2702:                        memset (keyfile, 0, sizeof(keyfile));
                   2703:                        _close (fhKeyfile);
                   2704:                        return 1;
                   2705:                }
                   2706:                return 0;
                   2707: 
                   2708:        case WM_CLOSE:
                   2709:                {
                   2710:                        char tmp[RNG_POOL_SIZE+1];
                   2711: exit:
                   2712:                        KillTimer (hwndDlg, 0xfd);
                   2713: 
                   2714: #ifndef VOLFORMAT                      
                   2715:                        Randfree ();
                   2716: #endif
                   2717:                        /* Cleanup */
                   2718: 
                   2719:                        memset (randPool, 0, sizeof(randPool));
                   2720:                        memset (lastRandPool, 0, sizeof(lastRandPool));
                   2721:                        memset (outputDispBuffer, 0, sizeof(outputDispBuffer));
                   2722: 
                   2723:                        // Attempt to wipe the pool contents in the GUI text area
                   2724:                        memset (tmp, 'X', RNG_POOL_SIZE);
                   2725:                        tmp [RNG_POOL_SIZE] = 0;
                   2726:                        SetWindowText (GetDlgItem (hwndDlg, IDC_POOL_CONTENTS), tmp);
                   2727: 
                   2728:                        EndDialog (hwndDlg, IDCLOSE);
                   2729:                        return 1;
1.1.1.5   root     2730:                }
                   2731:        }
1.1.1.7   root     2732:        return 0;
1.1.1.5   root     2733: }
                   2734: 
                   2735: 
1.1.1.7   root     2736: 
                   2737: /* Except in response to the WM_INITDIALOG message, the dialog box procedure
                   2738: should return nonzero if it processes the message, and zero if it does
                   2739: not. - see DialogProc */
                   2740: BOOL CALLBACK
                   2741: CipherTestDialogProc (HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
1.1.1.5   root     2742: {
1.1.1.8 ! root     2743:        static int idTestCipher = -1;           /* Currently selected cipher for the test vector facility (none = -1). */
        !          2744:        static BOOL bLRWTestEnabled = TRUE;
        !          2745: 
        !          2746:        PCRYPTO_INFO ci;
1.1.1.7   root     2747:        WORD lw = LOWORD (wParam);
                   2748:        WORD hw = HIWORD (wParam);
                   2749: 
                   2750:        if (lParam);            /* Remove unused parameter warning */
                   2751: 
                   2752:        switch (uMsg)
1.1.1.5   root     2753:        {
1.1.1.7   root     2754:        case WM_INITDIALOG:
                   2755:                {
                   2756:                        int ea;
                   2757:                        char buf[100];
                   2758: 
                   2759:                        LocalizeDialog (hwndDlg, "IDD_CIPHER_TEST_DLG");
                   2760: 
                   2761:                        SendMessage(GetDlgItem(hwndDlg, IDC_TESTS_MESSAGE), WM_SETFONT, (WPARAM)hBoldFont, MAKELPARAM(TRUE,0));
                   2762:                        SendMessage(GetDlgItem(hwndDlg, IDC_KEY), EM_LIMITTEXT, 128,0);
                   2763:                        SendMessage(GetDlgItem(hwndDlg, IDC_KEY), WM_SETFONT, (WPARAM)hFixedDigitFont, MAKELPARAM(1,0));
                   2764:                        SendMessage(GetDlgItem(hwndDlg, IDC_PLAINTEXT), EM_LIMITTEXT,128,0);
                   2765:                        SendMessage(GetDlgItem(hwndDlg, IDC_PLAINTEXT), WM_SETFONT, (WPARAM)hFixedDigitFont, MAKELPARAM(1,0));
                   2766:                        SendMessage(GetDlgItem(hwndDlg, IDC_CIPHERTEXT), EM_LIMITTEXT,128,0);
                   2767:                        SendMessage(GetDlgItem(hwndDlg, IDC_CIPHERTEXT), WM_SETFONT, (WPARAM)hFixedDigitFont, MAKELPARAM(1,0));
1.1.1.8 ! root     2768:                        SendMessage(GetDlgItem(hwndDlg, IDC_LRW_KEY), EM_LIMITTEXT,128,0);
        !          2769:                        SendMessage(GetDlgItem(hwndDlg, IDC_LRW_KEY), WM_SETFONT, (WPARAM)hFixedDigitFont, MAKELPARAM(1,0));
        !          2770:                        SendMessage(GetDlgItem(hwndDlg, IDC_LRW_BLOCK_INDEX), EM_LIMITTEXT,128,0);
        !          2771:                        SendMessage(GetDlgItem(hwndDlg, IDC_LRW_BLOCK_INDEX), WM_SETFONT, (WPARAM)hFixedDigitFont, MAKELPARAM(1,0));
        !          2772:                        SetCheckBox (hwndDlg, IDC_LRW_MODE_ENABLED, bLRWTestEnabled);
        !          2773:                        EnableWindow (GetDlgItem (hwndDlg, IDC_LRW_KEY), bLRWTestEnabled);
        !          2774:                        EnableWindow (GetDlgItem (hwndDlg, IDT_LRW_KEY), bLRWTestEnabled);
        !          2775:                        EnableWindow (GetDlgItem (hwndDlg, IDC_LRW_BLOCK_INDEX), bLRWTestEnabled);
        !          2776:                        EnableWindow (GetDlgItem (hwndDlg, IDT_LRW_BLOCK_INDEX), bLRWTestEnabled);
1.1.1.7   root     2777: 
                   2778:                        if (idTestCipher == -1)
                   2779:                                idTestCipher = (int) lParam;
                   2780: 
                   2781:                        SendMessage (GetDlgItem (hwndDlg, IDC_CIPHER), CB_RESETCONTENT, 0, 0);
                   2782:                        for (ea = EAGetFirst (); ea != 0; ea = EAGetNext (ea))
                   2783:                        {
1.1.1.8 ! root     2784:                                if (EAGetCipherCount (ea) == 1 && EAGetFirstMode (ea) == LRW)
1.1.1.7   root     2785:                                        AddComboPair (GetDlgItem (hwndDlg, IDC_CIPHER), EAGetName (buf, ea), ea);
                   2786:                        }
                   2787: 
                   2788:                        ResetCipherTest(hwndDlg, idTestCipher);
                   2789: 
                   2790:                        SelectAlgo (GetDlgItem (hwndDlg, IDC_CIPHER), &idTestCipher);
                   2791: 
                   2792:                        return 1;
                   2793:                }
                   2794: 
                   2795:        case WM_COMMAND:
                   2796: 
                   2797:                if (hw == CBN_SELCHANGE && lw == IDC_CIPHER)
                   2798:                {
                   2799:                        idTestCipher = (int) SendMessage (GetDlgItem (hwndDlg, IDC_CIPHER), CB_GETITEMDATA, SendMessage (GetDlgItem (hwndDlg, IDC_CIPHER), CB_GETCURSEL, 0, 0), 0);
                   2800:                        ResetCipherTest(hwndDlg, idTestCipher);
                   2801:                        SendMessage (hwndDlg, WM_INITDIALOG, 0, 0);
                   2802:                        return 1;
                   2803:                }
                   2804: 
                   2805:                if (hw == CBN_SELCHANGE && lw == IDC_KEY_SIZE)
                   2806:                {
                   2807:                        // NOP
                   2808:                        return 1;
                   2809:                }
                   2810: 
                   2811:                if (lw == IDC_RESET)
                   2812:                {
                   2813:                        ResetCipherTest(hwndDlg, idTestCipher);
                   2814: 
                   2815:                        return 1;
                   2816:                }
                   2817: 
                   2818:                if (lw == IDC_AUTO)
                   2819:                {
                   2820:                        if (!AutoTestAlgorithms())
                   2821:                        {
                   2822:                                ShowWindow(GetDlgItem(hwndDlg, IDC_TESTS_MESSAGE), SW_SHOWNORMAL);
                   2823:                                SetWindowTextW(GetDlgItem(hwndDlg, IDC_TESTS_MESSAGE), GetString ("TESTS_FAILED"));
                   2824:                        } 
                   2825:                        else
                   2826:                        {
                   2827:                                ShowWindow(GetDlgItem(hwndDlg, IDC_TESTS_MESSAGE), SW_SHOWNORMAL);
                   2828:                                SetWindowTextW(GetDlgItem(hwndDlg, IDC_TESTS_MESSAGE), GetString ("TESTS_PASSED"));
                   2829:                                ShowWindow(GetDlgItem(hwndDlg, IDC_REDTICK), SW_SHOWNORMAL);
                   2830:                        }
                   2831: 
                   2832:                        return 1;
                   2833: 
                   2834:                }
                   2835: 
1.1.1.8 ! root     2836:                if (lw == IDC_LRW_MODE_ENABLED)
        !          2837:                {
        !          2838:                        bLRWTestEnabled = GetCheckBox (hwndDlg, IDC_LRW_MODE_ENABLED);
        !          2839:                        EnableWindow (GetDlgItem (hwndDlg, IDC_LRW_KEY), bLRWTestEnabled);
        !          2840:                        EnableWindow (GetDlgItem (hwndDlg, IDT_LRW_KEY), bLRWTestEnabled);
        !          2841:                        EnableWindow (GetDlgItem (hwndDlg, IDT_LRW_BLOCK_INDEX), bLRWTestEnabled);
        !          2842:                        EnableWindow (GetDlgItem (hwndDlg, IDC_LRW_BLOCK_INDEX), bLRWTestEnabled);
        !          2843:                }
        !          2844: 
1.1.1.7   root     2845:                if (lw == IDOK || lw == IDC_ENCRYPT || lw == IDC_DECRYPT)
                   2846:                {
1.1.1.8 ! root     2847:                        char key[128], inputtext[128], lrwKey[16], lrwIndex[16], szTmp[128];
1.1.1.7   root     2848:                        int ks, pt, n;
                   2849:                        BOOL bEncrypt;
                   2850: 
                   2851:                        ShowWindow(GetDlgItem(hwndDlg, IDC_TESTS_MESSAGE), SW_HIDE);
                   2852:                        ShowWindow(GetDlgItem(hwndDlg, IDC_REDTICK), SW_HIDE);
                   2853: 
                   2854:                        ks = (int) SendMessage(GetDlgItem(hwndDlg, IDC_KEY_SIZE), CB_GETCURSEL, 0,0);
                   2855:                        ks = (int) SendMessage(GetDlgItem(hwndDlg, IDC_KEY_SIZE), CB_GETITEMDATA, ks,0);
                   2856:                        pt = (int) SendMessage(GetDlgItem(hwndDlg, IDC_PLAINTEXT_SIZE), CB_GETITEMDATA, 0,0);
                   2857: 
                   2858:                        bEncrypt = lw == IDC_ENCRYPT;
                   2859: 
                   2860:                        memset(key,0,sizeof(key));
                   2861:                        memset(szTmp,0,sizeof(szTmp));
                   2862:                        n = GetWindowText(GetDlgItem(hwndDlg, IDC_KEY), szTmp, sizeof(szTmp));
                   2863:                        if (n != ks * 2)
                   2864:                        {
                   2865:                                MessageBoxW (hwndDlg, GetString ("TEST_KEY_SIZE"), lpszTitle, ICON_HAND);
                   2866:                                return 1;
                   2867:                        }
                   2868: 
                   2869:                        for (n = 0; n < ks; n ++)
                   2870:                        {
                   2871:                                char szTmp2[3], *ptr;
                   2872:                                long x;
                   2873: 
                   2874:                                szTmp2[2] = 0;
                   2875:                                szTmp2[0] = szTmp[n * 2];
                   2876:                                szTmp2[1] = szTmp[n * 2 + 1];
                   2877: 
                   2878:                                x = strtol(szTmp2, &ptr, 16);
                   2879: 
                   2880:                                key[n] = (char) x;
                   2881:                        }
                   2882: 
1.1.1.8 ! root     2883:                        memset(inputtext, 0, sizeof(inputtext));
        !          2884:                        memset(lrwKey, 0, sizeof(lrwKey));
        !          2885:                        memset(lrwIndex, 0, sizeof(lrwIndex));
        !          2886:                        memset(szTmp, 0, sizeof(szTmp));
1.1.1.7   root     2887: 
                   2888:                        if (bEncrypt)
                   2889:                        {
                   2890:                                n = GetWindowText(GetDlgItem(hwndDlg, IDC_PLAINTEXT), szTmp, sizeof(szTmp));
                   2891:                        }
                   2892:                        else
                   2893:                        {
                   2894:                                n = GetWindowText(GetDlgItem(hwndDlg, IDC_CIPHERTEXT), szTmp, sizeof(szTmp));
                   2895:                        }
                   2896: 
                   2897:                        if (n != pt * 2)
                   2898:                        {
                   2899:                                if (bEncrypt)
                   2900:                                {
                   2901:                                        MessageBoxW (hwndDlg, GetString ("TEST_PLAINTEXT_SIZE"), lpszTitle, ICON_HAND);
                   2902:                                        return 1;
                   2903:                                }
                   2904:                                else
                   2905:                                {
                   2906:                                        MessageBoxW (hwndDlg, GetString ("TEST_CIPHERTEXT_SIZE"), lpszTitle, ICON_HAND);
                   2907:                                        return 1;
                   2908:                                }
                   2909:                        }
1.1.1.8 ! root     2910: 
1.1.1.7   root     2911:                        for (n = 0; n < pt; n ++)
                   2912:                        {
                   2913:                                char szTmp2[3], *ptr;
                   2914:                                long x;
                   2915: 
                   2916:                                szTmp2[2] = 0;
                   2917:                                szTmp2[0] = szTmp[n * 2];
                   2918:                                szTmp2[1] = szTmp[n * 2 + 1];
                   2919: 
                   2920:                                x = strtol(szTmp2, &ptr, 16);
                   2921: 
                   2922:                                inputtext[n] = (char) x;
                   2923:                        }
                   2924:                        
1.1.1.8 ! root     2925:                        // LRW
        !          2926:                        if (bLRWTestEnabled)
1.1.1.7   root     2927:                        {
1.1.1.8 ! root     2928:                                if (GetWindowText(GetDlgItem(hwndDlg, IDC_LRW_KEY), szTmp, sizeof(szTmp)) != pt * 2)
        !          2929:                                {
        !          2930:                                        Warning ("TEST_INCORRECT_LRW_KEY_SIZE");
        !          2931:                                        return 1;
        !          2932:                                }
1.1.1.7   root     2933: 
1.1.1.8 ! root     2934:                                // LRW key
1.1.1.7   root     2935: 
1.1.1.8 ! root     2936:                                for (n = 0; n < pt; n ++)
1.1.1.7   root     2937:                                {
1.1.1.8 ! root     2938:                                        char szTmp2[3], *ptr;
        !          2939:                                        long x;
        !          2940: 
        !          2941:                                        szTmp2[2] = 0;
        !          2942:                                        szTmp2[0] = szTmp[n * 2];
        !          2943:                                        szTmp2[1] = szTmp[n * 2 + 1];
        !          2944: 
        !          2945:                                        x = strtol(szTmp2, &ptr, 16);
        !          2946: 
        !          2947:                                        lrwKey[n] = (char) x;
1.1.1.7   root     2948:                                }
                   2949: 
1.1.1.8 ! root     2950:                                // LRW block index
1.1.1.7   root     2951: 
1.1.1.8 ! root     2952:                                if (GetWindowText(GetDlgItem(hwndDlg, IDC_LRW_BLOCK_INDEX), szTmp, sizeof(szTmp)) != pt * 2)
1.1.1.7   root     2953:                                {
1.1.1.8 ! root     2954:                                        Warning ("TEST_INCORRECT_LRW_INDEX_SIZE");
        !          2955:                                        return 1;
1.1.1.7   root     2956:                                }
1.1.1.8 ! root     2957:                                for (n = 0; n < pt; n ++)
1.1.1.7   root     2958:                                {
1.1.1.8 ! root     2959:                                        char szTmp2[3], *ptr;
        !          2960:                                        long x;
        !          2961: 
        !          2962:                                        szTmp2[2] = 0;
        !          2963:                                        szTmp2[0] = szTmp[n * 2];
        !          2964:                                        szTmp2[1] = szTmp[n * 2 + 1];
        !          2965: 
        !          2966:                                        x = strtol(szTmp2, &ptr, 16);
        !          2967: 
        !          2968:                                        lrwIndex[n] = (char) x;
1.1.1.7   root     2969:                                }
1.1.1.8 ! root     2970:                        }
1.1.1.7   root     2971: 
1.1.1.8 ! root     2972:                        
        !          2973:                        /* Perform the actual tests */
        !          2974: 
        !          2975:                        if (ks != CB_ERR && pt != CB_ERR) 
        !          2976:                        {
        !          2977:                                char tmp[128];
        !          2978: 
        !          2979:                                /* Copy the plain/ciphertext */
        !          2980:                                memcpy(tmp,inputtext, pt);
        !          2981: 
        !          2982:                                if (bLRWTestEnabled)
1.1.1.7   root     2983:                                {
1.1.1.8 ! root     2984:                                        /* LRW mode */
        !          2985: 
        !          2986:                                        ci = crypto_open ();
        !          2987:                                        if (!ci)
        !          2988:                                                return 1;
        !          2989: 
        !          2990:                                        ci->mode = LRW;
        !          2991:                                        ci->ea = idTestCipher;
        !          2992: 
        !          2993:                                        if (EAInit (ci->ea, key, ci->ks) != 0)
        !          2994:                                                return 1;
        !          2995: 
        !          2996:                                        memcpy (&ci->iv, lrwKey, sizeof (lrwKey));
        !          2997:                                        if (!EAInitMode (ci))
        !          2998:                                                return 1;
        !          2999: 
        !          3000:                                        if (pt == 16)
        !          3001:                                        {
        !          3002:                                                if (((unsigned __int64 *)lrwIndex)[0])
        !          3003:                                                {
        !          3004:                                                        Error ("TEST_LRW_INDEX_OVERRUN");
        !          3005:                                                        return 1;
        !          3006:                                                }
        !          3007: 
        !          3008:                                                if (bEncrypt)
        !          3009:                                                        EncryptBufferLRW128 (tmp, pt, BE64(((unsigned __int64 *)lrwIndex)[1]), ci);
        !          3010:                                                else
        !          3011:                                                        DecryptBufferLRW128 (tmp, pt, BE64(((unsigned __int64 *)lrwIndex)[1]), ci);
        !          3012:                                        }
        !          3013:                                        else if (pt == 8)
        !          3014:                                        {
        !          3015:                                                if (bEncrypt)
        !          3016:                                                        EncryptBufferLRW64 (tmp, pt, BE64(((unsigned __int64 *)lrwIndex)[0]), ci);
        !          3017:                                                else
        !          3018:                                                        DecryptBufferLRW64 (tmp, pt, BE64(((unsigned __int64 *)lrwIndex)[0]), ci);
        !          3019:                                        }
        !          3020:                                        crypto_close (ci);
1.1.1.7   root     3021:                                }
1.1.1.8 ! root     3022:                                else
        !          3023:                                {
        !          3024:                                        if (idTestCipher == BLOWFISH)
        !          3025:                                        {
        !          3026:                                                /* Convert to little-endian, this is needed here and not in
        !          3027:                                                above auto-tests because BF_ecb_encrypt above correctly converts
        !          3028:                                                from big to little endian, and EncipherBlock does not! */
        !          3029:                                                LongReverse((void*)tmp, pt);
        !          3030:                                        }
        !          3031: 
        !          3032:                                        CipherInit2(idTestCipher, key, ks_tmp, ks);
        !          3033: 
        !          3034:                                        if (bEncrypt)
        !          3035:                                        {
        !          3036:                                                EncipherBlock(idTestCipher, tmp, ks_tmp);
        !          3037:                                        }
        !          3038:                                        else
        !          3039:                                        {
        !          3040:                                                DecipherBlock(idTestCipher, tmp, ks_tmp);
        !          3041:                                        }
1.1.1.7   root     3042: 
1.1.1.8 ! root     3043:                                        if (idTestCipher == BLOWFISH)
        !          3044:                                        {
        !          3045:                                                /* Convert back to big-endian */
        !          3046:                                                LongReverse((void*)tmp, pt);
        !          3047:                                        }
        !          3048:                                }
1.1.1.7   root     3049:                                *szTmp = 0;
                   3050: 
1.1.1.8 ! root     3051:                                for (n = 0; n < pt; n ++)
1.1.1.7   root     3052:                                {
                   3053:                                        char szTmp2[3];
                   3054:                                        sprintf(szTmp2, "%02x", (int)((unsigned char)tmp[n]));
                   3055:                                        strcat(szTmp, szTmp2);
                   3056:                                }
                   3057: 
                   3058:                                if (bEncrypt)
                   3059:                                        SetWindowText(GetDlgItem(hwndDlg,IDC_CIPHERTEXT), szTmp);
                   3060:                                else
                   3061:                                        SetWindowText(GetDlgItem(hwndDlg,IDC_PLAINTEXT), szTmp);
                   3062:                        }
1.1.1.8 ! root     3063: 
1.1.1.7   root     3064:                        return 1;
                   3065:                }
                   3066: 
                   3067:                if (lw == IDCLOSE || lw == IDCANCEL)
                   3068:                {
                   3069:                        idTestCipher = -1;
                   3070:                        EndDialog (hwndDlg, 0);
                   3071:                        return 1;
                   3072:                }
                   3073:                break;
                   3074: 
                   3075:        case WM_CLOSE:
                   3076:                idTestCipher = -1;
                   3077:                EndDialog (hwndDlg, 0);
                   3078:                return 1;
1.1.1.5   root     3079:        }
1.1.1.7   root     3080: 
                   3081:        return 0;
                   3082: }
                   3083: 
                   3084: void 
                   3085: ResetCipherTest(HWND hwndDlg, int idTestCipher)
                   3086: {
                   3087:        int ndx;
                   3088: 
                   3089:        ShowWindow(GetDlgItem(hwndDlg, IDC_TESTS_MESSAGE), SW_HIDE);
                   3090:        ShowWindow(GetDlgItem(hwndDlg, IDC_REDTICK), SW_HIDE);
                   3091: 
                   3092:        if (idTestCipher == BLOWFISH)
                   3093:                EnableWindow(GetDlgItem(hwndDlg,IDC_KEY_SIZE), TRUE);
                   3094:        else
                   3095:                EnableWindow(GetDlgItem(hwndDlg,IDC_KEY_SIZE), FALSE);
                   3096: 
                   3097:        /* Setup the keysize and plaintext sizes for the selected cipher */
                   3098: 
                   3099:        SendMessage (GetDlgItem(hwndDlg, IDC_PLAINTEXT_SIZE), CB_RESETCONTENT, 0,0);
                   3100:        SendMessage (GetDlgItem(hwndDlg, IDC_KEY_SIZE), CB_RESETCONTENT, 0,0);
                   3101: 
                   3102:        ndx = SendMessage (GetDlgItem(hwndDlg, IDC_PLAINTEXT_SIZE), CB_ADDSTRING, 0,(LPARAM) "64");
                   3103:        SendMessage(GetDlgItem(hwndDlg, IDC_PLAINTEXT_SIZE), CB_SETITEMDATA, ndx,(LPARAM) 8);
                   3104:        SendMessage(GetDlgItem(hwndDlg, IDC_PLAINTEXT_SIZE), CB_SETCURSEL, ndx,0);
                   3105: 
1.1.1.8 ! root     3106:        SetWindowText(GetDlgItem(hwndDlg, IDC_LRW_KEY), "0000000000000000");
        !          3107:        SetWindowText(GetDlgItem(hwndDlg, IDC_LRW_BLOCK_INDEX), "0000000000000000");
        !          3108: 
1.1.1.7   root     3109:        if (idTestCipher == BLOWFISH)
                   3110:        {
                   3111:                ndx = SendMessage (GetDlgItem(hwndDlg, IDC_KEY_SIZE), CB_ADDSTRING, 0,(LPARAM) "448");
                   3112:                SendMessage(GetDlgItem(hwndDlg, IDC_KEY_SIZE), CB_SETITEMDATA, ndx,(LPARAM) 56);
                   3113:                ndx = SendMessage (GetDlgItem(hwndDlg, IDC_KEY_SIZE), CB_ADDSTRING, 0,(LPARAM) "256");
                   3114:                SendMessage(GetDlgItem(hwndDlg, IDC_KEY_SIZE), CB_SETITEMDATA, ndx,(LPARAM) 32);
                   3115:                ndx = SendMessage (GetDlgItem(hwndDlg, IDC_KEY_SIZE), CB_ADDSTRING, 0,(LPARAM) "128");
                   3116:                SendMessage(GetDlgItem(hwndDlg, IDC_KEY_SIZE), CB_SETITEMDATA, ndx,(LPARAM) 16);
                   3117:                ndx = SendMessage (GetDlgItem(hwndDlg, IDC_KEY_SIZE), CB_ADDSTRING, 0,(LPARAM) "64");
                   3118:                SendMessage(GetDlgItem(hwndDlg, IDC_KEY_SIZE), CB_SETITEMDATA, ndx,(LPARAM) 8);
                   3119:                SendMessage(GetDlgItem(hwndDlg, IDC_KEY_SIZE), CB_SETCURSEL, 0,0);
                   3120:                SetWindowText(GetDlgItem(hwndDlg, IDC_KEY), "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000");
                   3121:        } 
                   3122: 
                   3123: 
                   3124:        if (idTestCipher == CAST)
                   3125:        {
                   3126:                ndx = SendMessage (GetDlgItem(hwndDlg, IDC_KEY_SIZE), CB_ADDSTRING, 0,(LPARAM) "128");
                   3127:                SendMessage(GetDlgItem(hwndDlg, IDC_KEY_SIZE), CB_SETITEMDATA, ndx,(LPARAM) 16);
                   3128:                SendMessage(GetDlgItem(hwndDlg, IDC_KEY_SIZE), CB_SETCURSEL, ndx,0);
                   3129:                SetWindowText(GetDlgItem(hwndDlg, IDC_KEY), "00000000000000000000000000000000");
                   3130:        }
                   3131: 
                   3132:        if (idTestCipher == TRIPLEDES)
                   3133:        {
                   3134:                ndx = SendMessage (GetDlgItem(hwndDlg, IDC_KEY_SIZE), CB_ADDSTRING, 0,(LPARAM) "168");
                   3135:                SendMessage(GetDlgItem(hwndDlg, IDC_KEY_SIZE), CB_SETITEMDATA, ndx,(LPARAM) 24);
                   3136:                SendMessage(GetDlgItem(hwndDlg, IDC_KEY_SIZE), CB_SETCURSEL, ndx,0);
                   3137:                SetWindowText(GetDlgItem(hwndDlg, IDC_KEY), "000000000000000000000000000000000000000000000000");
                   3138:        }
                   3139: 
                   3140:        if (idTestCipher == DES56)
                   3141:        {
                   3142:                ndx = SendMessage (GetDlgItem(hwndDlg, IDC_KEY_SIZE), CB_ADDSTRING, 0,(LPARAM) "56");
                   3143:                SendMessage(GetDlgItem(hwndDlg, IDC_KEY_SIZE), CB_SETITEMDATA, ndx,(LPARAM) 7);
                   3144:                SendMessage(GetDlgItem(hwndDlg, IDC_KEY_SIZE), CB_SETCURSEL, 0,0);
                   3145:                SetWindowText(GetDlgItem(hwndDlg, IDC_KEY), "0000000000000000");
                   3146:        }
                   3147:        
                   3148:        SetWindowText(GetDlgItem(hwndDlg, IDC_PLAINTEXT), "0000000000000000");
                   3149:        SetWindowText(GetDlgItem(hwndDlg, IDC_CIPHERTEXT), "0000000000000000");
                   3150: 
                   3151:        if (idTestCipher == AES || idTestCipher == SERPENT || idTestCipher == TWOFISH)
                   3152:        {
                   3153:                ndx = SendMessage (GetDlgItem(hwndDlg, IDC_KEY_SIZE), CB_ADDSTRING, 0,(LPARAM) "256");
                   3154:                SendMessage(GetDlgItem(hwndDlg, IDC_KEY_SIZE), CB_SETITEMDATA, ndx,(LPARAM) 32);
                   3155:                SendMessage(GetDlgItem(hwndDlg, IDC_KEY_SIZE), CB_SETCURSEL, ndx,0);
                   3156: 
                   3157:                SendMessage (GetDlgItem(hwndDlg, IDC_PLAINTEXT_SIZE), CB_RESETCONTENT, 0,0);
                   3158:                ndx = SendMessage (GetDlgItem(hwndDlg, IDC_PLAINTEXT_SIZE), CB_ADDSTRING, 0,(LPARAM) "128");
                   3159:                SendMessage(GetDlgItem(hwndDlg, IDC_PLAINTEXT_SIZE), CB_SETITEMDATA, ndx,(LPARAM) 16);
                   3160:                SendMessage(GetDlgItem(hwndDlg, IDC_PLAINTEXT_SIZE), CB_SETCURSEL, ndx,0);
                   3161: 
                   3162:                SetWindowText(GetDlgItem(hwndDlg, IDC_KEY), "0000000000000000000000000000000000000000000000000000000000000000");
                   3163:                SetWindowText(GetDlgItem(hwndDlg, IDC_PLAINTEXT), "00000000000000000000000000000000");
                   3164:                SetWindowText(GetDlgItem(hwndDlg, IDC_CIPHERTEXT), "00000000000000000000000000000000");
1.1.1.8 ! root     3165: 
        !          3166:                SetWindowText(GetDlgItem(hwndDlg, IDC_LRW_KEY), "00000000000000000000000000000000");
        !          3167:                SetWindowText(GetDlgItem(hwndDlg, IDC_LRW_BLOCK_INDEX), "00000000000000000000000000000000");
1.1.1.7   root     3168:        }
                   3169: }
                   3170: 
                   3171: #endif // #ifndef SETUP
                   3172: 
                   3173: 
                   3174: BOOL CheckCapsLock (HWND hwnd, BOOL quiet)
                   3175: {
                   3176:        if ((GetKeyState(VK_CAPITAL) & 1) != 0) 
                   3177:        {
                   3178:                if (!quiet)
                   3179:                {
                   3180:                        MessageBoxW (hwnd, GetString ("CAPSLOCK_ON"), lpszTitle, MB_ICONEXCLAMATION);
                   3181:                }
                   3182:                return TRUE;
                   3183:        }
                   3184:        return FALSE;
1.1.1.5   root     3185: }
                   3186: 
                   3187: 
                   3188: int GetFirstAvailableDrive ()
                   3189: {
                   3190:        DWORD dwUsedDrives = GetLogicalDrives();
                   3191:        int i;
                   3192: 
                   3193:        for (i = 3; i < 26; i++)
                   3194:        {
                   3195:                if (!(dwUsedDrives & 1 << i))
                   3196:                        return i;
                   3197:        }
                   3198: 
                   3199:        return -1;
                   3200: }
                   3201: 
                   3202: 
                   3203: int GetLastAvailableDrive ()
                   3204: {
                   3205:        DWORD dwUsedDrives = GetLogicalDrives();
                   3206:        int i;
                   3207: 
                   3208:        for (i = 25; i > 2; i--)
                   3209:        {
                   3210:                if (!(dwUsedDrives & 1 << i))
                   3211:                        return i;
                   3212:        }
                   3213: 
                   3214:        return -1;
                   3215: }
                   3216: 
                   3217: 
                   3218: BOOL IsDriveAvailable (int driveNo)
                   3219: {
                   3220:        return (GetLogicalDrives() & (1 << driveNo)) == 0;
                   3221: }
                   3222: 
                   3223: 
                   3224: int DriverUnmountVolume (HWND hwndDlg, int nDosDriveNo, BOOL forced)
                   3225: {
                   3226:        UNMOUNT_STRUCT unmount;
                   3227:        DWORD dwResult;
                   3228: 
                   3229:        BOOL bResult;
                   3230:        
                   3231:        unmount.nDosDriveNo = nDosDriveNo;
                   3232:        unmount.ignoreOpenFiles = forced;
                   3233: 
                   3234:        bResult = DeviceIoControl (hDriver, UNMOUNT, &unmount,
                   3235:                sizeof (unmount), &unmount, sizeof (unmount), &dwResult, NULL);
                   3236: 
                   3237:        if (bResult == FALSE)
                   3238:        {
                   3239:                handleWin32Error (hwndDlg);
                   3240:                return 1;
                   3241:        }
                   3242: 
                   3243:        return unmount.nReturnCode;
                   3244: }
                   3245: 
                   3246: 
1.1.1.6   root     3247: void BroadcastDeviceChange (WPARAM message, int nDosDriveNo, DWORD driveMap)
1.1.1.5   root     3248: {
                   3249:        DEV_BROADCAST_VOLUME dbv;
1.1.1.7   root     3250:        char root[] = {0, ':', '\\', 0 };
1.1.1.6   root     3251:        DWORD dwResult;
1.1.1.7   root     3252:        LONG event = 0;
                   3253:        int i;
1.1.1.6   root     3254: 
1.1.1.7   root     3255:        if (message == DBT_DEVICEARRIVAL) event = SHCNE_DRIVEADD;
                   3256:        if (message == DBT_DEVICEREMOVECOMPLETE) event = SHCNE_DRIVEREMOVED;
1.1.1.5   root     3257: 
1.1.1.7   root     3258:        if (driveMap == 0)
                   3259:        {
                   3260:                root[0] = nDosDriveNo + 'A';
                   3261:                SHChangeNotify(event, SHCNF_PATH, root, NULL);
                   3262:        }
                   3263:        else
                   3264:        {
                   3265:                for (i = 0; i < 26; i++)
                   3266:                {
                   3267:                        if (driveMap & (1 << i))
                   3268:                        {
                   3269:                                root[0] = i + 'A';
                   3270:                                SHChangeNotify(event, SHCNF_PATH, root, NULL);
                   3271:                        }
                   3272:                }
                   3273:        }
1.1.1.5   root     3274: 
                   3275:        dbv.dbcv_size = sizeof(dbv); 
                   3276:        dbv.dbcv_devicetype = DBT_DEVTYP_VOLUME; 
                   3277:        dbv.dbcv_reserved = 0;
1.1.1.6   root     3278:        dbv.dbcv_unitmask = (driveMap != 0) ? driveMap : (1 << nDosDriveNo);
1.1.1.5   root     3279:        dbv.dbcv_flags = 0; 
                   3280: 
1.1.1.6   root     3281:        SendMessageTimeout (HWND_BROADCAST, WM_DEVICECHANGE, message, (LPARAM)(&dbv), 0, 500, &dwResult);
1.1.1.5   root     3282: }
                   3283: 
                   3284: 
1.1.1.7   root     3285: // Use only cached passwords if password = NULL
                   3286: //
1.1.1.5   root     3287: // Returns:
                   3288: // -1 = user aborted mount / error
                   3289: // 0  = mount failed
                   3290: // 1  = mount OK
                   3291: // 2  = mount OK in shared mode
                   3292: 
                   3293: int MountVolume (HWND hwndDlg,
                   3294:                                 int driveNo,
                   3295:                                 char *volumePath,
1.1.1.7   root     3296:                                 Password *password,
1.1.1.5   root     3297:                                 BOOL cachePassword,
                   3298:                                 BOOL sharedAccess,
1.1.1.6   root     3299:                                 MountOptions *mountOptions,
1.1.1.7   root     3300:                                 BOOL quiet,
                   3301:                                 BOOL bReportWrongPassword)
1.1.1.5   root     3302: {
1.1.1.7   root     3303:        MOUNT_STRUCT mount;
1.1.1.5   root     3304:        DWORD dwResult;
                   3305:        BOOL bResult, bDevice;
                   3306: 
                   3307:        if (IsMountedVolume (volumePath))
                   3308:        {
                   3309:                if (!quiet)
1.1.1.7   root     3310:                        MessageBoxW(0, GetString ("ALREADY_MOUNTED"), lpszTitle, MB_ICONASTERISK);
1.1.1.5   root     3311:                return -1;
                   3312:        }
                   3313: 
                   3314:        if (!IsDriveAvailable (driveNo))
                   3315:                return -1;
                   3316: 
                   3317:        // If using cached passwords, check cache status first
1.1.1.7   root     3318:        if (password == NULL && IsPasswordCacheEmpty ())
1.1.1.5   root     3319:                return 0;
                   3320: 
1.1.1.7   root     3321:        ZeroMemory (&mount, sizeof (mount));
                   3322:        mount.bExclusiveAccess = sharedAccess ? FALSE : TRUE;
1.1.1.5   root     3323: retry:
1.1.1.7   root     3324:        mount.nDosDriveNo = driveNo;
                   3325:        mount.bCache = cachePassword;
                   3326: 
                   3327:        if (password != NULL)
                   3328:                mount.VolumePassword = *password;
                   3329:        else
                   3330:                mount.VolumePassword.Length = 0;
                   3331: 
                   3332:        if (!mountOptions->ReadOnly && mountOptions->ProtectHiddenVolume)
                   3333:        {
                   3334:                mount.ProtectedHidVolPassword = mountOptions->ProtectedHidVolPassword;
                   3335:                mount.bProtectHiddenVolume = TRUE;
                   3336:        }
                   3337:        else
                   3338:                mount.bProtectHiddenVolume = FALSE;
                   3339: 
                   3340:        mount.bMountReadOnly = mountOptions->ReadOnly;
                   3341:        mount.bMountRemovable = mountOptions->Removable;
                   3342:        mount.bPreserveTimestamp = mountOptions->PreserveTimestamp;
                   3343: 
                   3344:        mount.bMountManager = TRUE;
1.1.1.5   root     3345: 
                   3346:        // Windows 2000 mount manager causes problems with remounted volumes
                   3347:        if (CurrentOSMajor == 5 && CurrentOSMinor == 0)
1.1.1.7   root     3348:                mount.bMountManager = FALSE;
1.1.1.5   root     3349: 
1.1.1.7   root     3350:        CreateFullVolumePath ((char *) mount.wszVolume, volumePath, &bDevice);
1.1.1.5   root     3351: 
1.1.1.7   root     3352:        // UNC path
                   3353:        if (volumePath[0] == '\\' && volumePath[1] == '\\')
                   3354:        {
                   3355:                _snprintf ((char *)mount.wszVolume, MAX_PATH, "UNC%s", volumePath + 1);
                   3356:                mount.bUserContext = TRUE;
                   3357:        }
                   3358: 
                   3359:        // Network drive
                   3360:        if (volumePath[1] == ':')
                   3361:        {
                   3362:                char root[] = { volumePath[0], ':', '\\', 0 };
1.1.1.5   root     3363: 
1.1.1.7   root     3364:                if (GetDriveType (root) == DRIVE_REMOTE)
                   3365:                        mount.bUserContext = TRUE;
                   3366:        }
                   3367: 
                   3368:        ToUNICODE ((char *) mount.wszVolume);
1.1.1.5   root     3369: 
1.1.1.7   root     3370:        bResult = DeviceIoControl (hDriver, MOUNT, &mount,
                   3371:                sizeof (mount), &mount, sizeof (mount), &dwResult, NULL);
                   3372: 
                   3373:        burn (&mount.VolumePassword, sizeof (mount.VolumePassword));
                   3374:        burn (&mount.ProtectedHidVolPassword, sizeof (mount.ProtectedHidVolPassword));
1.1.1.5   root     3375: 
                   3376:        if (bResult == FALSE)
                   3377:        {
                   3378:                // Volume already open by another process
1.1.1.8 ! root     3379:                if (GetLastError () == ERROR_SHARING_VIOLATION)
1.1.1.5   root     3380:                {
1.1.1.7   root     3381:                        if (mount.bExclusiveAccess == FALSE)
1.1.1.5   root     3382:                        {
                   3383:                                if (!quiet)
1.1.1.7   root     3384:                                        MessageBoxW (hwndDlg, GetString ("FILE_IN_USE_FAILED"),
1.1.1.5   root     3385:                                                lpszTitle, MB_ICONSTOP);
                   3386: 
                   3387:                                return -1;
                   3388:                        }
                   3389:                        else
                   3390:                        {
                   3391:                                if (quiet)
                   3392:                                {
1.1.1.7   root     3393:                                        mount.bExclusiveAccess = FALSE;
1.1.1.5   root     3394:                                        goto retry;
                   3395:                                }
                   3396: 
                   3397:                                // Ask user 
1.1.1.7   root     3398:                                if (IDYES == MessageBoxW (hwndDlg, GetString ("FILE_IN_USE"),
1.1.1.5   root     3399:                                        lpszTitle, MB_YESNO | MB_DEFBUTTON2 | MB_ICONEXCLAMATION))
                   3400:                                {
1.1.1.7   root     3401:                                        mount.bExclusiveAccess = FALSE;
1.1.1.5   root     3402:                                        goto retry;
                   3403:                                }
                   3404:                        }
                   3405: 
                   3406:                        return -1;
                   3407:                }
                   3408: 
1.1.1.8 ! root     3409:                // Mount failed in kernel space => retry in user process context
        !          3410:                if (!mount.bUserContext)
        !          3411:                {
        !          3412:                        mount.bUserContext = TRUE;
        !          3413:                        goto retry;
        !          3414:                }
        !          3415: 
1.1.1.5   root     3416:                if (!quiet)
                   3417:                        handleWin32Error (hwndDlg);
                   3418: 
                   3419:                return -1;
                   3420:        }
                   3421: 
1.1.1.7   root     3422:        if (mount.nReturnCode != 0)
1.1.1.5   root     3423:        {
1.1.1.7   root     3424:                if (mount.nReturnCode == ERR_PASSWORD_WRONG)
                   3425:                {
                   3426:                        // Do not report wrong password, if not instructed to 
                   3427:                        if (bReportWrongPassword)
                   3428:                                handleError (hwndDlg, mount.nReturnCode);
                   3429: 
                   3430:                        return 0;
                   3431:                }
1.1.1.5   root     3432: 
1.1.1.7   root     3433:                if (!quiet)
                   3434:                        handleError (hwndDlg, mount.nReturnCode);
1.1.1.5   root     3435: 
                   3436:                return 0;
                   3437:        }
                   3438: 
1.1.1.6   root     3439:        BroadcastDeviceChange (DBT_DEVICEARRIVAL, driveNo, 0);
1.1.1.5   root     3440: 
1.1.1.7   root     3441:        if (mount.bExclusiveAccess == FALSE)
1.1.1.5   root     3442:                return 2;
                   3443: 
                   3444:        return 1;
                   3445: }
                   3446: 
                   3447: 
                   3448: BOOL UnmountVolume (HWND hwndDlg , int nDosDriveNo, BOOL forceUnmount)
                   3449: {
                   3450:        int result;
                   3451:        BOOL forced = forceUnmount;
1.1.1.6   root     3452:        int dismountMaxRetries = UNMOUNT_MAX_AUTO_RETRIES;
1.1.1.5   root     3453: 
1.1.1.6   root     3454:        //BroadcastDeviceChange (DBT_DEVICEREMOVEPENDING, nDosDriveNo);
1.1.1.5   root     3455: 
                   3456: retry:
1.1.1.6   root     3457:        do
                   3458:        {
                   3459:                result = DriverUnmountVolume (hwndDlg, nDosDriveNo, forced);
                   3460: 
                   3461:                if (result == ERR_FILES_OPEN)
                   3462:                        Sleep (UNMOUNT_AUTO_RETRY_DELAY);
                   3463:                else
                   3464:                        break;
                   3465: 
                   3466:        } while (--dismountMaxRetries > 0);
1.1.1.5   root     3467: 
                   3468:        if (result != 0)
                   3469:        {
1.1.1.7   root     3470:                if (result == ERR_FILES_OPEN && !Silent)
1.1.1.5   root     3471:                {
1.1.1.7   root     3472:                        if (IDYES == AskWarnNoYes("UNMOUNT_LOCK_FAILED"))
1.1.1.5   root     3473:                        {
                   3474:                                forced = TRUE;
                   3475:                                goto retry;
                   3476:                        }
                   3477: 
                   3478:                        return FALSE;
                   3479:                }
                   3480: 
1.1.1.7   root     3481:                Error ("UNMOUNT_FAILED");
1.1.1.5   root     3482: 
                   3483:                return FALSE;
                   3484:        } 
                   3485:        
1.1.1.6   root     3486:        BroadcastDeviceChange (DBT_DEVICEREMOVECOMPLETE, nDosDriveNo, 0);
1.1.1.5   root     3487: 
                   3488:        return TRUE;
                   3489: }
                   3490: 
                   3491: 
                   3492: BOOL IsPasswordCacheEmpty (void)
                   3493: {
                   3494:        DWORD dw;
                   3495:        return !DeviceIoControl (hDriver, CACHE_STATUS, 0, 0, 0, 0, &dw, 0);
                   3496: }
                   3497: 
                   3498: BOOL IsMountedVolume (char *volname)
                   3499: {
                   3500:        MOUNT_LIST_STRUCT mlist;
                   3501:        DWORD dwResult;
                   3502:        int i;
                   3503:        char volume[TC_MAX_PATH*2+16];
                   3504: 
                   3505:        strcpy (volume, volname);
1.1.1.7   root     3506: 
                   3507:        if (strstr (volname, "\\Device\\") != volname)
                   3508:                sprintf(volume, "\\??\\%s", volname);
                   3509:        ToUNICODE (volume);
1.1.1.5   root     3510: 
                   3511:        memset (&mlist, 0, sizeof (mlist));
                   3512:        DeviceIoControl (hDriver, MOUNT_LIST, &mlist,
                   3513:                sizeof (mlist), &mlist, sizeof (mlist), &dwResult,
                   3514:                NULL);
                   3515: 
                   3516:        for (i=0 ; i<26; i++)
1.1.1.7   root     3517:                if (0 == wcscmp (mlist.wszVolume[i], (WCHAR *)volume))
1.1.1.5   root     3518:                        return TRUE;
                   3519: 
                   3520:        return FALSE;
                   3521: }
                   3522: 
                   3523: 
                   3524: BOOL IsAdmin (void)
                   3525: {
                   3526:        HANDLE hAccessToken;
                   3527:        UCHAR InfoBuffer[1024];
                   3528:        PTOKEN_GROUPS ptgGroups = (PTOKEN_GROUPS) InfoBuffer;
                   3529:        DWORD dwInfoBufferSize;
                   3530:        PSID psidAdministrators;
                   3531:        SID_IDENTIFIER_AUTHORITY siaNtAuthority = SECURITY_NT_AUTHORITY;
                   3532:        BOOL bSuccess;
                   3533:        UINT x;
                   3534: 
                   3535:        if (!OpenThreadToken (GetCurrentThread (), TOKEN_QUERY, TRUE,
                   3536:                              &hAccessToken))
                   3537:        {
                   3538:                if (GetLastError ()!= ERROR_NO_TOKEN)
                   3539:                        return FALSE;
                   3540: 
                   3541:                /* Retry against process token if no thread token exists */
                   3542:                if (!OpenProcessToken (GetCurrentProcess (), TOKEN_QUERY,
                   3543:                                       &hAccessToken))
                   3544:                        return FALSE;
                   3545:        }
                   3546: 
                   3547:        bSuccess = GetTokenInformation (hAccessToken, TokenGroups, InfoBuffer,
                   3548:                                        1024, &dwInfoBufferSize);
                   3549: 
                   3550:        CloseHandle (hAccessToken);
                   3551: 
                   3552:        if (!bSuccess)
                   3553:                return FALSE;
                   3554: 
                   3555:        if (!AllocateAndInitializeSid (&siaNtAuthority, 2,
                   3556:                                       SECURITY_BUILTIN_DOMAIN_RID,
                   3557:                                       DOMAIN_ALIAS_RID_ADMINS,
                   3558:                                       0, 0, 0, 0, 0, 0,
                   3559:                                       &psidAdministrators))
                   3560:                return FALSE;
                   3561:  
                   3562:        /* Assume that we don't find the admin SID. */
                   3563:        bSuccess = FALSE;
                   3564: 
                   3565:        for (x = 0; x < ptgGroups->GroupCount; x++)
                   3566:        {
                   3567:                if (EqualSid (psidAdministrators, ptgGroups->Groups[x].Sid))
                   3568:                {
                   3569:                        bSuccess = TRUE;
                   3570:                        break;
                   3571:                }
                   3572: 
                   3573:        }
                   3574: 
                   3575:        FreeSid (psidAdministrators);
                   3576:        return bSuccess;
                   3577: }
                   3578: 
                   3579: 
                   3580: BOOL ResolveSymbolicLink (PWSTR symLinkName, PWSTR targetName)
                   3581: {
                   3582:        BOOL bResult;
                   3583:        DWORD dwResult;
                   3584:        RESOLVE_SYMLINK_STRUCT resolve;
                   3585: 
                   3586:        memset (&resolve, 0, sizeof(resolve));
                   3587:        wcscpy ((PWSTR) &resolve.symLinkName, symLinkName);
                   3588: 
                   3589:        bResult = DeviceIoControl (hDriver, RESOLVE_SYMLINK, &resolve,
                   3590:                sizeof (resolve), &resolve, sizeof (resolve), &dwResult,
                   3591:                NULL);
                   3592: 
                   3593:        wcscpy (targetName, (PWSTR) &resolve.targetName);
                   3594: 
                   3595:        return bResult;
                   3596: }
                   3597: 
                   3598: 
                   3599: // Returns drive letter number assigned to device (-1 if none)
                   3600: int GetDiskDeviceDriveLetter (PWSTR deviceName)
                   3601: {
                   3602:        int i;
                   3603:        WCHAR link[MAX_PATH];
                   3604:        WCHAR target[MAX_PATH];
                   3605:        WCHAR device[MAX_PATH];
                   3606: 
                   3607:        if (!ResolveSymbolicLink (deviceName, device))
                   3608:                wcscpy (device, deviceName);
                   3609: 
                   3610:        for (i = 0; i < 26; i++)
                   3611:        {
                   3612:                WCHAR drive[] = { i + 'A', ':', 0 };
                   3613: 
                   3614:                wcscpy (link, L"\\DosDevices\\");
                   3615:                wcscat (link, drive);
                   3616: 
                   3617:                ResolveSymbolicLink (link, target);
                   3618: 
                   3619:                if (wcscmp (device, target) == 0)
                   3620:                        return i;
                   3621:        }
                   3622: 
                   3623:        return -1;
                   3624: }
                   3625: 
                   3626: 
                   3627: HANDLE DismountDrive (int driveNo)
                   3628: {
                   3629:        char volMountName[32];
                   3630:        char dosName[3];
                   3631:        DWORD dwResult;
                   3632:        BOOL bResult;
                   3633:        HANDLE hVolume;
                   3634: 
                   3635:        dosName[0] = (char) (driveNo + 'A');
                   3636:        dosName[1] = ':';
                   3637:        dosName[2] = 0;
                   3638: 
                   3639:        sprintf (volMountName, "\\\\.\\%s", dosName);
                   3640: 
                   3641:        hVolume = CreateFile (volMountName, GENERIC_READ | GENERIC_WRITE,
                   3642:                FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
                   3643: 
                   3644:        bResult = DeviceIoControl (hVolume, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0, &dwResult, NULL);
                   3645:        bResult = DeviceIoControl (hVolume, FSCTL_DISMOUNT_VOLUME, NULL, 0, NULL, 0, &dwResult, NULL);
                   3646: 
                   3647:        return hVolume;
1.1.1.6   root     3648: }
                   3649: 
                   3650: // System CopyFile() copies source file attributes (like FILE_ATTRIBUTE_ENCRYPTED)
                   3651: // so we need to use our own copy function
                   3652: BOOL TCCopyFile (char *sourceFileName, char *destinationFile)
                   3653: {
                   3654:        __int8 *buffer;
                   3655:        HANDLE src, dst;
                   3656:        FILETIME fileTime;
                   3657:        DWORD bytesRead, bytesWritten;
                   3658:        BOOL res;
                   3659: 
                   3660:        src = CreateFile (sourceFileName,
                   3661:                GENERIC_READ,
                   3662:                FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
                   3663: 
                   3664:        if (src == INVALID_HANDLE_VALUE)
                   3665:                return FALSE;
                   3666: 
                   3667:        dst = CreateFile (destinationFile,
                   3668:                GENERIC_WRITE,
                   3669:                0, NULL, CREATE_ALWAYS, 0, NULL);
                   3670: 
                   3671:        if (dst == INVALID_HANDLE_VALUE)
                   3672:        {
                   3673:                CloseHandle (src);
                   3674:                return FALSE;
                   3675:        }
                   3676: 
                   3677:        buffer = malloc (64 * 1024);
                   3678:        if (!buffer)
                   3679:        {
                   3680:                CloseHandle (src);
                   3681:                CloseHandle (dst);
                   3682:                return FALSE;
                   3683:        }
                   3684: 
                   3685:        while (res = ReadFile (src, buffer, 64 * 1024, &bytesRead, NULL))
                   3686:        {
                   3687:                if (bytesRead == 0)
                   3688:                {
                   3689:                        res = 1;
                   3690:                        break;
                   3691:                }
                   3692: 
                   3693:                if (!WriteFile (dst, buffer, bytesRead, &bytesWritten, NULL)
                   3694:                        || bytesRead != bytesWritten)
                   3695:                {
                   3696:                        res = 0;
                   3697:                        break;
                   3698:                }
                   3699:        }
                   3700: 
                   3701:        GetFileTime (src, NULL, NULL, &fileTime);
                   3702:        SetFileTime (dst, NULL, NULL, &fileTime);
                   3703: 
                   3704:        CloseHandle (src);
                   3705:        CloseHandle (dst);
                   3706: 
                   3707:        free (buffer);
                   3708:        return res != 0;
                   3709: }
                   3710: 
1.1.1.7   root     3711: int BackupVolumeHeader (HWND hwndDlg, BOOL bRequireConfirmation, char *lpszVolume)
                   3712: {
                   3713:        int nDosLinkCreated = 0, nStatus;
                   3714:        char szDiskFile[TC_MAX_PATH], szCFDevice[TC_MAX_PATH];
                   3715:        char szFileName[TC_MAX_PATH];
                   3716:        char szDosDevice[TC_MAX_PATH];
                   3717:        char buffer[HEADER_SIZE];
                   3718:        void *dev = INVALID_HANDLE_VALUE;
                   3719:        diskio_f read;
                   3720:        DWORD dwError;
                   3721:        BOOL bDevice;
                   3722:        unsigned __int64 volSize = 0;
                   3723:        wchar_t szTmp[1024];
                   3724:        int volumeType;
                   3725:        int fBackup = -1;
                   3726: 
                   3727: 
                   3728:        if (IsMountedVolume (lpszVolume))
                   3729:        {
                   3730:                Warning ("DISMOUNT_FIRST");
                   3731:                return 0;
                   3732:        }
                   3733: 
                   3734:        wsprintfW (szTmp, GetString ("CONFIRM_VOL_HEADER_BAK"), lpszVolume);
                   3735: 
                   3736:        if (bRequireConfirmation 
                   3737:                && (MessageBoxW (hwndDlg, szTmp, lpszTitle, YES_NO|MB_ICONQUESTION|MB_DEFBUTTON1) == IDNO))
                   3738:                return 0;
                   3739: 
                   3740: 
                   3741:        /* Select backup file */
1.1.1.8 ! root     3742:        if (!BrowseFiles (hwndDlg, "OPEN_TITLE", szFileName, FALSE, TRUE))
1.1.1.7   root     3743:                return 0;
                   3744: 
                   3745:        /* Conceive the backup file */
                   3746:        if ((fBackup = _open(szFileName, _O_CREAT|_O_TRUNC|_O_WRONLY|_O_BINARY, _S_IREAD|_S_IWRITE)) == -1)
                   3747:                return ERROR_CANNOT_MAKE;
                   3748: 
                   3749: 
                   3750:        /* Read the volume headers and write them to the backup file */
                   3751: 
                   3752:        CreateFullVolumePath (szDiskFile, lpszVolume, &bDevice);
                   3753: 
                   3754:        read = (diskio_f) _lread;
                   3755: 
                   3756:        if (bDevice == FALSE)
                   3757:        {
                   3758:                strcpy (szCFDevice, szDiskFile);
                   3759:        }
                   3760:        else
                   3761:        {
                   3762:                nDosLinkCreated = FakeDosNameForDevice (szDiskFile, szDosDevice, szCFDevice, FALSE);
                   3763:                if (nDosLinkCreated != 0)
                   3764:                {
                   3765:                        nStatus = nDosLinkCreated;
                   3766:                        goto error0;
                   3767:                }
                   3768:        }
                   3769: 
                   3770:        dev = CreateFile (szCFDevice, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
                   3771: 
                   3772:        if (bDevice)
                   3773:        {
                   3774:                /* This is necessary to determine the hidden volume header offset */
                   3775: 
                   3776:                if (dev == INVALID_HANDLE_VALUE)
                   3777:                {
                   3778:                        nStatus = ERR_OS_ERROR;
                   3779:                        goto error0;
                   3780:                }
                   3781:                else
                   3782:                {
                   3783:                        PARTITION_INFORMATION diskInfo;
                   3784:                        DWORD dwResult;
                   3785:                        BOOL bResult;
                   3786: 
                   3787:                        bResult = DeviceIoControl (dev, IOCTL_DISK_GET_PARTITION_INFO, NULL, 0,
                   3788:                                &diskInfo, sizeof (diskInfo), &dwResult, NULL);
                   3789: 
                   3790:                        if (bResult)
                   3791:                        {
                   3792:                                volSize = diskInfo.PartitionLength.QuadPart;
                   3793:                        }
                   3794:                        else
                   3795:                        {
                   3796:                                DISK_GEOMETRY driveInfo;
                   3797: 
                   3798:                                bResult = DeviceIoControl (dev, IOCTL_DISK_GET_DRIVE_GEOMETRY, NULL, 0,
                   3799:                                        &driveInfo, sizeof (driveInfo), &dwResult, NULL);
                   3800: 
                   3801:                                if (!bResult)
                   3802:                                {
                   3803:                                        nStatus = ERR_OS_ERROR;
                   3804:                                        goto error;
                   3805:                                }
                   3806: 
                   3807:                                volSize = driveInfo.Cylinders.QuadPart * driveInfo.BytesPerSector *
                   3808:                                        driveInfo.SectorsPerTrack * driveInfo.TracksPerCylinder;
                   3809:                        }
                   3810: 
                   3811:                        if (volSize == 0)
                   3812:                        {
                   3813:                                nStatus = ERR_VOL_SIZE_WRONG;
                   3814:                                goto error;
                   3815:                        }
                   3816:                }
                   3817:        }
                   3818: 
                   3819:        if (dev == INVALID_HANDLE_VALUE) return ERR_OS_ERROR;
                   3820: 
                   3821:        for (volumeType = VOLUME_TYPE_NORMAL; volumeType < NBR_VOLUME_TYPES; volumeType++)
                   3822:        {
                   3823:                /* Read in volume header */
                   3824: 
                   3825:                if (volumeType == VOLUME_TYPE_HIDDEN)
                   3826:                {
                   3827:                        if (!SeekHiddenVolHeader ((HFILE) dev, volSize, bDevice))
                   3828:                        {
                   3829:                                nStatus = ERR_VOL_SEEKING;
                   3830:                                goto error;
                   3831:                        }
                   3832:                }
                   3833: 
                   3834:                nStatus = (*read) ((HFILE) dev, buffer, sizeof (buffer));
                   3835:                if (nStatus != sizeof (buffer))
                   3836:                {
                   3837:                        nStatus = ERR_VOL_SIZE_WRONG;
                   3838:                        goto error;
                   3839:                }
                   3840: 
                   3841:                /* Write the header to the backup file */
                   3842: 
                   3843:                if (_write (fBackup, buffer, sizeof(buffer)) == -1)
                   3844:                        goto error;
                   3845:        }
                   3846: 
                   3847:        /* Backup has been successfully created */
                   3848:        nStatus = 0;
                   3849:        Info("VOL_HEADER_BACKED_UP");
                   3850: 
                   3851: error:
                   3852: 
                   3853:        dwError = GetLastError ();
                   3854: 
                   3855:        CloseHandle ((HANDLE) dev);
                   3856:        _close (fBackup);
                   3857: 
                   3858:        if (bDevice && nDosLinkCreated != 0)
                   3859:        {
                   3860:                int x = RemoveFakeDosName (szDiskFile, szDosDevice);
                   3861:                if (x != 0)
                   3862:                {
                   3863:                        dwError = GetLastError ();
                   3864:                        nStatus = x;
                   3865:                }
                   3866:        }
                   3867: 
                   3868:        SetLastError (dwError);
                   3869: 
                   3870:        return nStatus;
                   3871: 
                   3872: error0:
                   3873: 
                   3874:        dwError = GetLastError ();
                   3875:        _close (fBackup);
                   3876:        SetLastError (dwError);
                   3877: 
                   3878:        return nStatus;
                   3879: }
                   3880: 
                   3881: int RestoreVolumeHeader (HWND hwndDlg, char *lpszVolume)
                   3882: {
                   3883:        int nDosLinkCreated = 0, nStatus = 0;
                   3884:        char szDiskFile[TC_MAX_PATH], szCFDevice[TC_MAX_PATH];
                   3885:        char szFileName[TC_MAX_PATH];
                   3886:        char szDosDevice[TC_MAX_PATH];
                   3887:        char buffer[HEADER_SIZE];
                   3888:        void *dev = INVALID_HANDLE_VALUE;
                   3889:        diskio_f write;
                   3890:        DWORD dwError;
                   3891:        BOOL bDevice;
                   3892:        unsigned __int64 volSize = 0;
                   3893:        FILETIME ftCreationTime;
                   3894:        FILETIME ftLastWriteTime;
                   3895:        FILETIME ftLastAccessTime;
                   3896:        wchar_t szTmp[1024];
                   3897:        BOOL bRestoreHiddenVolHeader = FALSE;
                   3898:        BOOL bTimeStampValid = FALSE;
                   3899:        int fBackup = -1;
                   3900: 
                   3901: 
                   3902:        if (IsMountedVolume (lpszVolume))
                   3903:        {
                   3904:                Warning ("DISMOUNT_FIRST");
                   3905:                return 0;
                   3906:        }
                   3907: 
                   3908:        wsprintfW (szTmp, GetString ("CONFIRM_VOL_HEADER_RESTORE"), lpszVolume);
                   3909: 
                   3910:        if (MessageBoxW (hwndDlg, szTmp, lpszTitle, YES_NO|MB_ICONWARNING|MB_DEFBUTTON2) == IDNO)
                   3911:                return 0;
                   3912: 
                   3913: 
                   3914:        /* Select backup file */
1.1.1.8 ! root     3915:        if (!BrowseFiles (hwndDlg, "OPEN_TITLE", szFileName, FALSE, FALSE))
1.1.1.7   root     3916:                return 0;
                   3917: 
                   3918: 
                   3919:        if (AskNoYes("CONFIRM_HIDVOL_HEADER_RESTORE") == IDYES)
                   3920:                bRestoreHiddenVolHeader = TRUE;
                   3921: 
                   3922: 
                   3923:        /* Open the backup file */
                   3924:        if ((fBackup = _open(szFileName, _O_BINARY|_O_RDONLY)) == -1)
                   3925:                return ERROR_OPEN_FAILED;
                   3926: 
                   3927: 
                   3928:        CreateFullVolumePath (szDiskFile, lpszVolume, &bDevice);
                   3929: 
                   3930:        write = (diskio_f) _lwrite;
                   3931: 
                   3932:        if (bDevice == FALSE)
                   3933:        {
                   3934:                strcpy (szCFDevice, szDiskFile);
                   3935:        }
                   3936:        else
                   3937:        {
                   3938:                nDosLinkCreated = FakeDosNameForDevice (szDiskFile, szDosDevice, szCFDevice, FALSE);
                   3939:                if (nDosLinkCreated != 0)
                   3940:                {
                   3941:                        nStatus = nDosLinkCreated;
                   3942:                        goto error0;
                   3943:                }
                   3944:        }
                   3945: 
                   3946:        dev = CreateFile (szCFDevice, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
                   3947: 
                   3948:        if (bDevice)
                   3949:        {
                   3950:                /* This is necessary to determine the hidden volume header offset */
                   3951: 
                   3952:                if (dev == INVALID_HANDLE_VALUE)
                   3953:                {
                   3954:                        nStatus = ERR_OS_ERROR;
                   3955:                        goto error0;
                   3956:                }
                   3957:                else
                   3958:                {
                   3959:                        PARTITION_INFORMATION diskInfo;
                   3960:                        DWORD dwResult;
                   3961:                        BOOL bResult;
                   3962: 
                   3963:                        bResult = DeviceIoControl (dev, IOCTL_DISK_GET_PARTITION_INFO, NULL, 0,
                   3964:                                &diskInfo, sizeof (diskInfo), &dwResult, NULL);
                   3965: 
                   3966:                        if (bResult)
                   3967:                        {
                   3968:                                volSize = diskInfo.PartitionLength.QuadPart;
                   3969:                        }
                   3970:                        else
                   3971:                        {
                   3972:                                DISK_GEOMETRY driveInfo;
                   3973: 
                   3974:                                bResult = DeviceIoControl (dev, IOCTL_DISK_GET_DRIVE_GEOMETRY, NULL, 0,
                   3975:                                        &driveInfo, sizeof (driveInfo), &dwResult, NULL);
                   3976: 
                   3977:                                if (!bResult)
                   3978:                                {
                   3979:                                        nStatus = ERR_OS_ERROR;
                   3980:                                        goto error;
                   3981:                                }
                   3982: 
                   3983:                                volSize = driveInfo.Cylinders.QuadPart * driveInfo.BytesPerSector *
                   3984:                                        driveInfo.SectorsPerTrack * driveInfo.TracksPerCylinder;
                   3985:                        }
                   3986: 
                   3987:                        if (volSize == 0)
                   3988:                        {
                   3989:                                nStatus =  ERR_VOL_SIZE_WRONG;
                   3990:                                goto error;
                   3991:                        }
                   3992:                }
                   3993:        }
                   3994: 
                   3995:        if (dev == INVALID_HANDLE_VALUE) 
                   3996:                return ERR_OS_ERROR;
                   3997: 
                   3998:        if (!bDevice && bPreserveTimestamp)
                   3999:        {
                   4000:                /* Remember the container modification/creation date and time. */
                   4001: 
                   4002:                if (GetFileTime ((HANDLE) dev, &ftCreationTime, &ftLastAccessTime, &ftLastWriteTime) == 0)
                   4003:                {
                   4004:                        bTimeStampValid = FALSE;
                   4005:                        Warning ("GETFILETIME_FAILED_GENERIC");
                   4006:                }
                   4007:                else
                   4008:                        bTimeStampValid = TRUE;
                   4009:        }
                   4010: 
                   4011:        /* Read the volume header from the backup file */
                   4012: 
                   4013:        if (_lseek(fBackup, bRestoreHiddenVolHeader ? HEADER_SIZE : 0, SEEK_SET) == -1L)
                   4014:        {
                   4015:                nStatus = ERROR_SEEK;
                   4016:                goto error;
                   4017:        }
                   4018: 
                   4019:        if (_read (fBackup, buffer, HEADER_SIZE) == -1)
                   4020:                goto error;
                   4021: 
                   4022: 
                   4023:        /* Restore/write the volume header */
                   4024: 
                   4025:        // Seek
                   4026:        if (bRestoreHiddenVolHeader)
                   4027:        {
                   4028:                if (!SeekHiddenVolHeader ((HFILE) dev, volSize, bDevice))
                   4029:                {
                   4030:                        nStatus = ERR_VOL_SEEKING;
                   4031:                        goto error;
                   4032:                }
                   4033:        }
                   4034:        else
                   4035:        {
                   4036:                nStatus = _llseek ((HFILE) dev, 0, FILE_BEGIN);
                   4037: 
                   4038:                if (nStatus != 0)
                   4039:                {
                   4040:                        nStatus = ERR_VOL_SEEKING;
                   4041:                        goto error;
                   4042:                }
                   4043:        }
                   4044: 
                   4045:        // Write
                   4046:        if (((*write) ((HFILE) dev, buffer, HEADER_SIZE)) != HEADER_SIZE)
                   4047:        {
                   4048:                nStatus = ERR_VOL_WRITING;
                   4049:                goto error;
                   4050:        }
                   4051: 
                   4052:        if (nStatus != 0)
                   4053:                goto error;
                   4054: 
                   4055: 
                   4056:        /* Volume header has been successfully restored */
                   4057: 
                   4058:        nStatus = 0;
                   4059:        Info("VOL_HEADER_RESTORED");
                   4060: 
                   4061: error:
                   4062: 
                   4063:        dwError = GetLastError ();
                   4064: 
                   4065:        if (bTimeStampValid)
                   4066:        {
                   4067:                // Restore the container timestamp (to preserve plausible deniability of possible hidden volume). 
                   4068:                if (SetFileTime (dev, &ftCreationTime, &ftLastAccessTime, &ftLastWriteTime) == 0)
                   4069:                        MessageBoxW (hwndDlg, GetString ("SETFILETIME_FAILED_PW"), L"TrueCrypt", MB_OK | MB_ICONEXCLAMATION);
                   4070:        }
                   4071: 
                   4072:        CloseHandle ((HANDLE) dev);
                   4073:        _close (fBackup);
                   4074: 
                   4075:        if (bDevice && nDosLinkCreated != 0)
                   4076:        {
                   4077:                int x = RemoveFakeDosName (szDiskFile, szDosDevice);
                   4078:                if (x != 0)
                   4079:                {
                   4080:                        dwError = GetLastError ();
                   4081:                        nStatus = x;
                   4082:                }
                   4083:        }
                   4084: 
                   4085:        SetLastError (dwError);
                   4086: 
                   4087:        return nStatus;
                   4088: 
                   4089: error0:
                   4090: 
                   4091:        dwError = GetLastError ();
                   4092:        _close (fBackup);
                   4093:        SetLastError (dwError);
                   4094: 
                   4095:        return nStatus;
                   4096: }
1.1.1.6   root     4097: 
                   4098: BOOL IsNonInstallMode ()
                   4099: {
1.1.1.7   root     4100:        static int cachedMode = -1;
1.1.1.6   root     4101: 
1.1.1.7   root     4102:        if (cachedMode == -1)
                   4103:        {
                   4104:                HANDLE fh;
                   4105:                WIN32_FIND_DATA fd;
                   4106:                char fileName[TC_MAX_PATH];
1.1.1.6   root     4107: 
1.1.1.7   root     4108:                if (!Is64BitOs ())
                   4109:                        GetSystemDirectory (fileName, sizeof (fileName));
                   4110:                else
                   4111:                        GetWindowsDirectory (fileName, sizeof (fileName));
                   4112:                strcat (fileName, !Is64BitOs () ? "\\Drivers\\truecrypt.sys" : "\\SysWOW64\\Drivers\\truecrypt.sys");
1.1.1.6   root     4113: 
1.1.1.7   root     4114:                fh = FindFirstFile (fileName, &fd);
1.1.1.6   root     4115: 
1.1.1.7   root     4116:                if (fh == INVALID_HANDLE_VALUE)
                   4117:                {
                   4118:                        cachedMode = 1;         
                   4119:                        return TRUE;
                   4120:                }
                   4121:                else
                   4122:                        cachedMode = 0;
                   4123: 
                   4124:                FindClose (fh);
                   4125:        }
                   4126: 
                   4127:        return cachedMode == 1;
1.1.1.6   root     4128: }
                   4129: 
                   4130: 
1.1.1.7   root     4131: LRESULT SetCheckBox (HWND hwndDlg, int dlgItem, BOOL state)
1.1.1.6   root     4132: {
1.1.1.7   root     4133:        return SendDlgItemMessage (hwndDlg, dlgItem, BM_SETCHECK, state ? BST_CHECKED : BST_UNCHECKED, 0);
                   4134: }
1.1.1.6   root     4135: 
1.1.1.7   root     4136: 
                   4137: BOOL GetCheckBox (HWND hwndDlg, int dlgItem)
                   4138: {
                   4139:        return IsButtonChecked (GetDlgItem (hwndDlg, dlgItem));
                   4140: }
                   4141: 
                   4142: 
                   4143: // Delete the last used Windows file selector path for TrueCrypt from the registry file
                   4144: // at HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\ComDlg32\LastVisitedMRU
                   4145: void CleanLastVisitedMRU (void)
                   4146: {
                   4147:        WCHAR exeFilename[MAX_PATH];
                   4148:        WCHAR *strToMatch;
                   4149: 
                   4150:        WCHAR strTmp[MAX_PATH*2];
                   4151:        char key[2] = {0, 0};
                   4152:        int i, len;
                   4153: 
                   4154:        GetModuleFileNameW (NULL, exeFilename, sizeof (exeFilename));
                   4155:        strToMatch = wcsrchr (exeFilename, '\\') + 1;
                   4156: 
                   4157:        for (i = 'a'; i <= 'z'; i++)
1.1.1.6   root     4158:        {
1.1.1.7   root     4159:                *strTmp = 0;
                   4160:                key [0] = i;
                   4161:                if ((len = ReadRegistryBytes ("Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\ComDlg32\\LastVisitedMRU",
                   4162:                        key, (char *) strTmp, sizeof (strTmp))) > 0)
1.1.1.6   root     4163:                {
1.1.1.7   root     4164:                        if (wcsstr (strTmp, strToMatch) != NULL) 
                   4165:                        {
                   4166:                                // Overwrite the entry with zeroes while keeping its original size
                   4167:                                memset (strTmp, 0, len);
                   4168:                                if (!WriteRegistryBytes ("Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\ComDlg32\\LastVisitedMRU",
                   4169:                                        key, (char *) strTmp, len))
                   4170:                                        MessageBoxW (NULL, GetString ("CLEAN_WINMRU_FAILED"), lpszTitle, ICON_HAND);
                   4171: 
                   4172:                                // Overwrite the entry with 4 zero bytes
                   4173:                                WriteRegistryBytes ("Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\ComDlg32\\LastVisitedMRU",
                   4174:                                        key, (char *) strTmp, 4);
                   4175:                        }
                   4176:                }
                   4177:        }
                   4178: }
1.1.1.6   root     4179: 
                   4180: 
1.1.1.7   root     4181: LRESULT ListItemAdd (HWND list, int index, char *string)
                   4182: {
                   4183:        LVITEM li;
                   4184:        memset (&li, 0, sizeof(li));
1.1.1.6   root     4185: 
1.1.1.7   root     4186:        li.mask = LVIF_TEXT;
                   4187:        li.pszText = string;
                   4188:        li.iItem = index; 
                   4189:        li.iSubItem = 0;
                   4190:        return ListView_InsertItem (list, &li);
                   4191: }
1.1.1.6   root     4192: 
1.1.1.7   root     4193: 
                   4194: LRESULT ListItemAddW (HWND list, int index, wchar_t *string)
                   4195: {
                   4196:        LVITEMW li;
                   4197:        memset (&li, 0, sizeof(li));
                   4198: 
                   4199:        li.mask = LVIF_TEXT;
                   4200:        li.pszText = string;
                   4201:        li.iItem = index; 
                   4202:        li.iSubItem = 0;
                   4203:        return SendMessageW (list, LVM_INSERTITEMW, 0, (LPARAM)(&li));
                   4204: }
                   4205: 
                   4206: 
                   4207: LRESULT ListSubItemSet (HWND list, int index, int subIndex, char *string)
                   4208: {
                   4209:        LVITEM li;
                   4210:        memset (&li, 0, sizeof(li));
                   4211: 
                   4212:        li.mask = LVIF_TEXT;
                   4213:        li.pszText = string;
                   4214:        li.iItem = index; 
                   4215:        li.iSubItem = subIndex;
                   4216:        return ListView_SetItem (list, &li);
                   4217: }
                   4218: 
                   4219: 
                   4220: LRESULT ListSubItemSetW (HWND list, int index, int subIndex, wchar_t *string)
                   4221: {
                   4222:        LVITEMW li;
                   4223:        memset (&li, 0, sizeof(li));
                   4224: 
                   4225:        li.mask = LVIF_TEXT;
                   4226:        li.pszText = string;
                   4227:        li.iItem = index; 
                   4228:        li.iSubItem = subIndex;
                   4229:        return SendMessageW (list, LVM_SETITEMW, 0, (LPARAM)(&li));
                   4230: }
                   4231: 
                   4232: 
                   4233: BOOL GetMountList (MOUNT_LIST_STRUCT *list)
                   4234: {
                   4235:        DWORD dwResult;
                   4236: 
                   4237:        memset (list, 0, sizeof (*list));
                   4238:        return DeviceIoControl (hDriver, MOUNT_LIST, list,
                   4239:                sizeof (*list), list, sizeof (*list), &dwResult,
                   4240:                NULL);
                   4241: }
                   4242: 
                   4243: 
                   4244: int GetDriverRefCount ()
                   4245: {
                   4246:        DWORD dwResult;
                   4247:        BOOL bResult;
                   4248:        int refCount;
                   4249: 
                   4250:        bResult = DeviceIoControl (hDriver, DEVICE_REFCOUNT, &refCount, sizeof (refCount), &refCount,
                   4251:                sizeof (refCount), &dwResult, NULL);
                   4252: 
                   4253:        if (bResult)
                   4254:                return refCount;
                   4255:        else
                   4256:                return -1;
                   4257: }
                   4258: 
                   4259: 
                   4260: char *LoadFile (char *fileName, DWORD *size)
                   4261: {
                   4262:        char *buf;
                   4263:        HANDLE h = CreateFile (fileName, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);
                   4264:        if (h == INVALID_HANDLE_VALUE)
                   4265:                return NULL;
                   4266: 
                   4267:        *size = GetFileSize (h, NULL);
                   4268:        buf = malloc (*size + 1);
                   4269:        ZeroMemory (buf, *size + 1);
                   4270: 
                   4271:        if (buf != NULL)
                   4272:                ReadFile (h, buf, *size, size, NULL);
                   4273: 
                   4274:        CloseHandle (h);
                   4275:        return buf;
                   4276: }
                   4277: 
                   4278: 
                   4279: char *GetConfigPath (char *fileName)
                   4280: {
                   4281:        static char path[MAX_PATH];
                   4282: 
                   4283:        if (!IsNonInstallMode ())
                   4284:        {
                   4285:                // User application data folder
                   4286:                SHGetFolderPath (NULL, CSIDL_APPDATA, NULL, 0, path);
                   4287:                strcat (path, "\\TrueCrypt\\");
                   4288:                CreateDirectory (path, NULL);
                   4289:                strcat (path, fileName);
                   4290:        }
                   4291:        else
                   4292:        {
                   4293:                // Application directory
                   4294:                GetModuleFileName (NULL, path, sizeof (path));
                   4295:                strrchr (path, '\\')[1] = 0;
                   4296:                strcat (path, fileName);
                   4297:        }
                   4298: 
                   4299:        return path;
                   4300: }
                   4301: 
                   4302: 
                   4303: int Info (char *stringId)
                   4304: {
                   4305:        if (Silent) return 0;
                   4306:        return MessageBoxW (MainDlg, GetString (stringId), lpszTitle, MB_ICONINFORMATION | MB_SETFOREGROUND | MB_TOPMOST);
                   4307: }
                   4308: 
                   4309: 
                   4310: int Warning (char *stringId)
                   4311: {
                   4312:        if (Silent) return 0;
                   4313:        return MessageBoxW (MainDlg, GetString (stringId), lpszTitle, MB_ICONWARNING | MB_SETFOREGROUND | MB_TOPMOST);
                   4314: }
                   4315: 
                   4316: 
                   4317: int Error (char *stringId)
                   4318: {
                   4319:        if (Silent) return 0;
                   4320:        return MessageBoxW (MainDlg, GetString (stringId), lpszTitle, MB_ICONERROR | MB_SETFOREGROUND | MB_TOPMOST);
                   4321: }
                   4322: 
                   4323: 
                   4324: int AskYesNo (char *stringId)
                   4325: {
                   4326:        if (Silent) return 0;
                   4327:        return MessageBoxW (MainDlg, GetString (stringId), lpszTitle, MB_ICONQUESTION | MB_YESNO | MB_DEFBUTTON1 | MB_SETFOREGROUND | MB_TOPMOST);
                   4328: }
                   4329: 
                   4330: 
                   4331: int AskNoYes (char *stringId)
                   4332: {
                   4333:        if (Silent) return 0;
                   4334:        return MessageBoxW (MainDlg, GetString (stringId), lpszTitle, MB_ICONQUESTION | MB_YESNO | MB_DEFBUTTON2 | MB_SETFOREGROUND | MB_TOPMOST);
                   4335: }
                   4336: 
                   4337: 
                   4338: int AskWarnYesNo (char *stringId)
                   4339: {
                   4340:        if (Silent) return 0;
                   4341:        return MessageBoxW (MainDlg, GetString (stringId), lpszTitle, MB_ICONWARNING | MB_YESNO | MB_DEFBUTTON1 | MB_SETFOREGROUND | MB_TOPMOST);
                   4342: }
                   4343: 
                   4344: 
                   4345: int AskWarnNoYes (char *stringId)
                   4346: {
                   4347:        if (Silent) return 0;
                   4348:        return MessageBoxW (MainDlg, GetString (stringId), lpszTitle, MB_ICONWARNING | MB_YESNO | MB_DEFBUTTON2 | MB_SETFOREGROUND | MB_TOPMOST);
                   4349: }
                   4350: 
                   4351: 
                   4352: int AskWarnCancelOk (char *stringId)
                   4353: {
                   4354:        if (Silent) return 0;
                   4355:        return MessageBoxW (MainDlg, GetString (stringId), lpszTitle, MB_ICONWARNING | MB_OKCANCEL | MB_DEFBUTTON2 | MB_SETFOREGROUND | MB_TOPMOST);
                   4356: }
                   4357: 
                   4358: 
                   4359: BOOL ConfigWriteBegin ()
                   4360: {
                   4361:        DWORD size;
                   4362:        if (ConfigFileHandle != NULL) return FALSE;
                   4363: 
                   4364:        if (ConfigBuffer == NULL)
                   4365:                ConfigBuffer = LoadFile (GetConfigPath (FILE_CONFIGURATION), &size);
                   4366: 
                   4367:        ConfigFileHandle = fopen (GetConfigPath (FILE_CONFIGURATION), "w");
                   4368:        if (ConfigFileHandle == NULL)
                   4369:        {
                   4370:                free (ConfigBuffer);
                   4371:                ConfigBuffer = NULL;
                   4372:                return FALSE;
                   4373:        }
                   4374:        XmlWriteHeader (ConfigFileHandle);
                   4375:        fputs ("\n\t<configuration>", ConfigFileHandle);
                   4376: 
                   4377:        return TRUE;
                   4378: }
                   4379: 
                   4380: 
                   4381: BOOL ConfigWriteEnd ()
                   4382: {
                   4383:        char *xml = ConfigBuffer;
                   4384:        char key[128], value[2048];
                   4385: 
                   4386:        if (ConfigFileHandle == NULL) return FALSE;
                   4387: 
                   4388:        // Write unmodified values
                   4389:        while (xml && (xml = XmlFindElement (xml, "config")))
                   4390:        {
                   4391:                XmlAttribute (xml, "key", key, sizeof (key));
                   4392:                XmlNodeText (xml, value, sizeof (value));
                   4393: 
                   4394:                fprintf (ConfigFileHandle, "\n\t\t<config key=\"%s\">%s</config>", key, value);
                   4395:                xml++;
                   4396:        }
                   4397: 
                   4398:        fputs ("\n\t</configuration>", ConfigFileHandle);
                   4399:        XmlWriteFooter (ConfigFileHandle);
                   4400: 
                   4401:        fclose (ConfigFileHandle);
                   4402:        ConfigFileHandle = NULL;
                   4403: 
                   4404:        if (ConfigBuffer != NULL)
                   4405:        {
                   4406:                DWORD size;
                   4407:                free (ConfigBuffer);
                   4408:                ConfigBuffer = LoadFile (GetConfigPath (FILE_CONFIGURATION), &size);
                   4409:        }
                   4410: 
                   4411:        return TRUE;
                   4412: }
                   4413: 
                   4414: 
                   4415: BOOL ConfigWriteString (char *configKey, char *configValue)
                   4416: {
                   4417:        char *c;
                   4418:        if (ConfigFileHandle == NULL)
                   4419:                return FALSE;
                   4420: 
                   4421:        // Mark previous config value as updated
                   4422:        if (ConfigBuffer != NULL)
                   4423:        {
                   4424:                c = XmlFindElementByAttributeValue (ConfigBuffer, "config", "key", configKey);
                   4425:                if (c != NULL)
                   4426:                        c[1] = '!';
                   4427:        }
                   4428: 
                   4429:        return 0 != fprintf (
                   4430:                ConfigFileHandle, "\n\t\t<config key=\"%s\">%s</config>",
                   4431:                configKey, configValue);
                   4432: }
                   4433: 
                   4434: 
                   4435: BOOL ConfigWriteInt (char *configKey, int configValue)
                   4436: {
                   4437:        char val[32];
                   4438:        sprintf (val, "%d", configValue);
                   4439:        return ConfigWriteString (configKey, val);
                   4440: }
                   4441: 
                   4442: 
                   4443: static BOOL ConfigRead (char *configKey, char *configValue, int maxValueSize)
                   4444: {
                   4445:        DWORD size;
                   4446:        char *xml;
                   4447: 
                   4448:        if (ConfigBuffer == NULL)
                   4449:                ConfigBuffer = LoadFile (GetConfigPath (FILE_CONFIGURATION), &size);
                   4450: 
                   4451:        xml = ConfigBuffer;
                   4452:        if (xml != NULL)
                   4453:        {
                   4454:                xml = XmlFindElementByAttributeValue (xml, "config", "key", configKey);
                   4455:                if (xml != NULL)
1.1.1.6   root     4456:                {
1.1.1.7   root     4457:                        XmlNodeText (xml, configValue, maxValueSize);
                   4458:                        return TRUE;
1.1.1.6   root     4459:                }
1.1.1.7   root     4460:        }
                   4461: 
                   4462:        return FALSE;
                   4463: }
                   4464: 
                   4465: 
                   4466: int ConfigReadInt (char *configKey, int defaultValue)
                   4467: {
                   4468:        char s[32];
                   4469: 
                   4470:        if (ConfigRead (configKey, s, sizeof (s)))
                   4471:                return atoi (s);
                   4472:        else
                   4473:                return defaultValue;
                   4474: }
                   4475: 
                   4476: 
                   4477: char *ConfigReadString (char *configKey, char *defaultValue, char *str, int maxLen)
                   4478: {
                   4479:        if (ConfigRead (configKey, str, maxLen))
                   4480:                return str;
                   4481:        else
                   4482:                return defaultValue;
                   4483: }
                   4484: 
                   4485: 
                   4486: void OpenPageHelp (HWND hwndDlg, int nPage)
                   4487: {
                   4488:        int r = (int)ShellExecute (NULL, "open", szHelpFile, NULL, NULL, SW_SHOWNORMAL);
                   4489:        if (nPage);             /* Remove warning */
1.1.1.6   root     4490: 
1.1.1.7   root     4491:        if (r == ERROR_FILE_NOT_FOUND)
                   4492:        {
                   4493:                // Try the secondary help file
                   4494:                r = (int)ShellExecute (NULL, "open", szHelpFile2, NULL, NULL, SW_SHOWNORMAL);
                   4495: 
                   4496:                if (r == ERROR_FILE_NOT_FOUND)
                   4497:                        MessageBoxW (hwndDlg, GetString ("HELP_ERROR"), lpszTitle, MB_ICONERROR);
                   4498:        }
                   4499:        if (r == SE_ERR_NOASSOC)
                   4500:                MessageBoxW (hwndDlg, GetString ("HELP_READER_ERROR"), lpszTitle, MB_ICONERROR);
                   4501: }
                   4502: 
                   4503: 
                   4504: #ifndef SETUP
                   4505: 
                   4506: void RestoreDefaultKeyFilesParam (void)
                   4507: {
                   4508:        KeyFileRemoveAll (&FirstKeyFile);
                   4509:        if (defaultKeyFilesParam.FirstKeyFile != NULL)
                   4510:        {
                   4511:                FirstKeyFile = KeyFileCloneAll (defaultKeyFilesParam.FirstKeyFile);
                   4512:                KeyFilesEnable = defaultKeyFilesParam.EnableKeyFiles;
                   4513:        }
                   4514:        else
                   4515:                KeyFilesEnable = FALSE;
                   4516: }
                   4517: 
                   4518: 
                   4519: BOOL LoadDefaultKeyFilesParam (void)
                   4520: {
                   4521:        BOOL status = TRUE;
                   4522:        DWORD size;
                   4523:        char *defaultKeyfilesFile = LoadFile (GetConfigPath (FILE_DEFAULT_KEYFILES), &size);
                   4524:        char *xml = defaultKeyfilesFile;
                   4525:        KeyFile *kf;
                   4526: 
                   4527:        if (xml == NULL) 
                   4528:                return FALSE;
                   4529: 
                   4530:        KeyFileRemoveAll (&defaultKeyFilesParam.FirstKeyFile);
                   4531: 
                   4532:        while (xml = XmlFindElement (xml, "keyfile"))
                   4533:        {
                   4534:                kf = malloc (sizeof (KeyFile));
                   4535: 
                   4536:                if (XmlNodeText (xml, kf->FileName, sizeof (kf->FileName)) != NULL)
                   4537:                        defaultKeyFilesParam.FirstKeyFile = KeyFileAdd (defaultKeyFilesParam.FirstKeyFile, kf);
                   4538:                else
                   4539:                        free (kf);
                   4540: 
                   4541:                xml++;
                   4542:        }
                   4543: 
                   4544:        free (defaultKeyfilesFile);
                   4545:        KeyFilesEnable = defaultKeyFilesParam.EnableKeyFiles;
                   4546: 
                   4547:        return status;
                   4548: }
                   4549: 
                   4550: #endif /* #ifndef SETUP */
                   4551: 
                   4552: 
                   4553: void Debug (char *format, ...)
                   4554: {
                   4555:        char buf[1024];
                   4556:        va_list val;
                   4557: 
                   4558:        va_start(val, format);
                   4559:        _vsnprintf (buf, sizeof (buf), format, val);
                   4560:        va_end(val);
                   4561: 
                   4562:        OutputDebugString (buf);
                   4563: }
                   4564: 
                   4565: 
                   4566: void DebugMsgBox (char *format, ...)
                   4567: {
                   4568:        char buf[1024];
                   4569:        va_list val;
                   4570: 
                   4571:        va_start(val, format);
                   4572:        _vsnprintf (buf, sizeof (buf), format, val);
                   4573:        va_end(val);
                   4574: 
                   4575:        MessageBox (MainDlg, buf, "TrueCrypt debug", 0);
                   4576: }
                   4577: 
                   4578: 
                   4579: BOOL Is64BitOs ()
                   4580: {
                   4581:     static BOOL isWow64 = FALSE;
                   4582:        static BOOL valid = FALSE;
                   4583:        typedef BOOL (__stdcall *LPFN_ISWOW64PROCESS ) (HANDLE hProcess,PBOOL Wow64Process);
                   4584:        LPFN_ISWOW64PROCESS fnIsWow64Process;
                   4585: 
                   4586:        if (valid)
                   4587:                return isWow64;
                   4588: 
                   4589:        fnIsWow64Process = (LPFN_ISWOW64PROCESS) GetProcAddress (GetModuleHandle("kernel32"), "IsWow64Process");
                   4590: 
                   4591:     if (fnIsWow64Process != NULL)
                   4592:         if (!fnIsWow64Process (GetCurrentProcess(), &isWow64))
                   4593:                        isWow64 = FALSE;
                   4594: 
                   4595:        valid = TRUE;
                   4596:     return isWow64;
                   4597: }
                   4598: 
                   4599: 
                   4600: char *RelativePath2Absolute (char *szFileName)
                   4601: {
                   4602:        if (szFileName[0] != '\\'
                   4603:                && strchr (szFileName, ':') == 0
                   4604:                && strstr (szFileName, "Volume{") != szFileName)
                   4605:        {
                   4606:                char path[MAX_PATH*2];
                   4607:                GetCurrentDirectory (MAX_PATH, path);
                   4608: 
                   4609:                if (path[strlen (path) - 1] != '\\')
                   4610:                        strcat (path, "\\");
                   4611: 
                   4612:                strcat (path, szFileName);
                   4613:                strncpy (szFileName, path, MAX_PATH-1);
                   4614:        }
                   4615: 
                   4616:        return szFileName;
                   4617: }
                   4618: 
                   4619: 
                   4620: void CheckSystemAutoMount ()
                   4621: {
                   4622:        HKEY hkey = 0;
                   4623:        DWORD value = 0, size = sizeof (DWORD);
                   4624: 
                   4625:        if (RegOpenKeyEx (HKEY_LOCAL_MACHINE, "SYSTEM\\ControlSet001\\Services\\MountMgr",
                   4626:                0, KEY_READ, &hkey) != ERROR_SUCCESS)
                   4627:                return;
                   4628: 
                   4629:        if (RegQueryValueEx (hkey, "NoAutoMount", 0, 0, (LPBYTE) &value, &size) == ERROR_SUCCESS
                   4630:                && value != 0)
                   4631:                Warning ("SYS_AUTOMOUNT_DISABLED");
                   4632:        
                   4633:        RegCloseKey (hkey);
                   4634: }
                   4635: 
                   4636: 
1.1.1.8 ! root     4637: BOOL CALLBACK CloseTCWindowsEnum (HWND hwnd, LPARAM lParam)
1.1.1.7   root     4638: {
                   4639:        if (GetWindowLongPtr (hwnd, GWLP_USERDATA) == (LONG_PTR) 'TRUE')
                   4640:        {
                   4641:                char name[32] = { 0 };
                   4642:                GetWindowText (hwnd, name, sizeof (name) - 1);
                   4643:                if (hwnd != MainDlg && strstr (name, "TrueCrypt") == name)
1.1.1.6   root     4644:                {
1.1.1.7   root     4645:                        PostMessage (hwnd, WM_ENDSESSION, 0, 0);
                   4646:                        PostMessage (hwnd, WM_CLOSE, 0, 0);
1.1.1.6   root     4647: 
1.1.1.7   root     4648:                        if (lParam != 0)
                   4649:                                *((BOOL *)lParam) = TRUE;
1.1.1.6   root     4650:                }
                   4651:        }
1.1.1.7   root     4652:        return TRUE;
1.1.1.6   root     4653: }
1.1.1.7   root     4654: 
                   4655: 
1.1.1.8 ! root     4656: BOOL CALLBACK FindTCWindowEnum (HWND hwnd, LPARAM lParam)
        !          4657: {
        !          4658:        if (*(HWND *)lParam == hwnd)
        !          4659:                return TRUE;
        !          4660: 
        !          4661:        if (GetWindowLongPtr (hwnd, GWLP_USERDATA) == (LONG_PTR) 'TRUE')
        !          4662:        {
        !          4663:                char name[32] = { 0 };
        !          4664:                GetWindowText (hwnd, name, sizeof (name) - 1);
        !          4665:                if (hwnd != MainDlg && strstr (name, "TrueCrypt") == name)
        !          4666:                {
        !          4667:                        if (lParam != 0)
        !          4668:                                *((HWND *)lParam) = hwnd;
        !          4669:                }
        !          4670:        }
        !          4671:        return TRUE;
        !          4672: }
        !          4673: 
        !          4674: 
1.1.1.7   root     4675: BYTE *MapResource (char *resourceType, int resourceId, PDWORD size)
                   4676: {
                   4677:        HGLOBAL hResL; 
                   4678:     HRSRC hRes;
                   4679: 
                   4680:        hRes = FindResource (NULL, MAKEINTRESOURCE(resourceId), resourceType);
                   4681:        hResL = LoadResource (NULL, hRes);
                   4682: 
                   4683:        if (size != NULL)
                   4684:                *size = SizeofResource (NULL, hRes);
                   4685:   
                   4686:        return (BYTE *) LockResource (hResL);
                   4687: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.