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

1.1.1.3   root        1: /* The source code contained in this file has been derived from the source code
                      2:    of Encryption for the Masses 2.02a by Paul Le Roux. Modifications and
                      3:    additions to that source code contained in this file are Copyright (c) 2004
1.1.1.5 ! root        4:    TrueCrypt Foundation and Copyright (c) 2004 TrueCrypt Team. Unmodified
1.1.1.3   root        5:    parts are Copyright (c) 1998-99 Paul Le Roux. This is a TrueCrypt Foundation
                      6:    release. Please see the file license.txt for full license details. */
1.1       root        7: 
                      8: #include "TCdefs.h"
                      9: 
                     10: #include <stdlib.h>
1.1.1.5 ! root       11: #include <time.h>
        !            12: #include <shlobj.h>
        !            13: #include <dbt.h>
1.1       root       14: 
                     15: #include "resource.h"
                     16: #include "crypto.h"
                     17: #include "apidrvr.h"
                     18: #include "dlgcode.h"
1.1.1.5 ! root       19: #include "volumes.h"
        !            20: 
1.1       root       21: 
                     22: char szHelpFile[TC_MAX_PATH];
                     23: HFONT hSmallFont = NULL;
                     24: HFONT hBoldFont = NULL;
                     25: HFONT hSmallBoldFont = NULL;
                     26: HFONT hTitleFont = NULL;
                     27: HFONT hFixedFont = NULL;
                     28: 
                     29: HFONT hUserFont = NULL;
                     30: HFONT hUserUnderlineFont = NULL;
                     31: HFONT hUserBoldFont = NULL;
1.1.1.5 ! root       32: HFONT hUserUnderlineBoldFont = NULL;
1.1       root       33: 
                     34: char *lpszTitle = NULL;
                     35: int nCurrentOS = 0;
                     36: int CurrentOSMajor = 0;
                     37: int CurrentOSMinor = 0;
                     38: 
                     39: /* Handle to the device driver */
                     40: HANDLE hDriver = INVALID_HANDLE_VALUE;
                     41: HINSTANCE hInst = NULL;
                     42: HANDLE hMutex = NULL;
                     43: HCURSOR hCursor = NULL;
                     44: 
                     45: ATOM hDlgClass, hSplashClass;
                     46: 
                     47: /* Windows dialog class */
                     48: #define WINDOWS_DIALOG_CLASS "#32770"
                     49: 
                     50: /* Custom class names */
                     51: #define TC_DLG_CLASS "CustomDlg"
                     52: #define TC_SPLASH_CLASS "SplashDlg"
                     53: 
1.1.1.5 ! root       54: /* Benchmark */
        !            55: #ifndef SETUP
        !            56: #define BENCHMARK_MAX_ITEMS 100
        !            57: #define BENCHMARK_DEFAULT_BUF_SIZE (1*BYTES_PER_MB)
        !            58: 
        !            59: enum 
        !            60: {
        !            61:        BENCHMARK_SORT_BY_NAME = 0,
        !            62:        BENCHMARK_SORT_BY_SPEED
        !            63: };
        !            64: 
        !            65: typedef struct 
        !            66: {
        !            67:        int id;
        !            68:        char name[100];
        !            69:        unsigned __int64 encSpeed;
        !            70:        unsigned __int64 decSpeed;
        !            71:        unsigned __int64 meanBytesPerSec;
        !            72: } BENCHMARK_REC;
        !            73: 
        !            74: BENCHMARK_REC benchmarkTable [BENCHMARK_MAX_ITEMS];
        !            75: int benchmarkTotalItems = 0;
        !            76: int benchmarkBufferSize = BENCHMARK_DEFAULT_BUF_SIZE;
        !            77: int benchmarkLastBufferSize = BENCHMARK_DEFAULT_BUF_SIZE;
        !            78: int benchmarkSortMethod = BENCHMARK_SORT_BY_SPEED;
        !            79: LARGE_INTEGER benchmarkPerformanceFrequency;
        !            80: #endif // #ifndef SETUP
        !            81: 
        !            82: 
1.1       root       83: void
                     84: cleanup ()
                     85: {
                     86:        /* Cleanup the GDI fonts */
                     87:        if (hFixedFont != NULL)
                     88:                DeleteObject (hFixedFont);
                     89:        if (hSmallFont != NULL)
                     90:                DeleteObject (hSmallFont);
                     91:        if (hBoldFont != NULL)
                     92:                DeleteObject (hBoldFont);
                     93:        if (hSmallBoldFont != NULL)
                     94:                DeleteObject (hSmallBoldFont);
                     95:        if (hTitleFont != NULL)
                     96:                DeleteObject (hTitleFont);
                     97:        if (hUserFont != NULL)
                     98:                DeleteObject (hUserFont);
                     99:        if (hUserUnderlineFont != NULL)
                    100:                DeleteObject (hUserUnderlineFont);
                    101:        if (hUserBoldFont != NULL)
                    102:                DeleteObject (hUserBoldFont);
1.1.1.5 ! root      103:        if (hUserUnderlineBoldFont != NULL)
        !           104:                DeleteObject (hUserUnderlineBoldFont);
1.1       root      105:        /* Cleanup our dialog class */
                    106:        if (hDlgClass)
                    107:                UnregisterClass (TC_DLG_CLASS, hInst);
                    108:        if (hSplashClass)
                    109:                UnregisterClass (TC_SPLASH_CLASS, hInst);
                    110:        /* Close the device driver handle */
                    111:        if (hDriver != INVALID_HANDLE_VALUE)
                    112:        {
                    113:                CloseHandle (hDriver);
                    114:        }
                    115: 
                    116:        if (hMutex != NULL)
                    117:        {
                    118:                CloseHandle (hMutex);
                    119:        }
                    120: }
                    121: 
                    122: void
                    123: LowerCaseCopy (char *lpszDest, char *lpszSource)
                    124: {
                    125:        int i = strlen (lpszSource);
                    126: 
                    127:        lpszDest[i] = 0;
                    128:        while (--i >= 0)
                    129:        {
                    130:                lpszDest[i] = (char) tolower (lpszSource[i]);
                    131:        }
                    132: 
                    133: }
                    134: 
                    135: void
                    136: UpperCaseCopy (char *lpszDest, char *lpszSource)
                    137: {
                    138:        int i = strlen (lpszSource);
                    139: 
                    140:        lpszDest[i] = 0;
                    141:        while (--i >= 0)
                    142:        {
                    143:                lpszDest[i] = (char) toupper (lpszSource[i]);
                    144:        }
                    145: }
                    146: 
                    147: void
                    148: CreateFullVolumePath (char *lpszDiskFile, char *lpszFileName, BOOL * bDevice)
                    149: {
                    150:        if (strcmp (lpszFileName, "Floppy (A:)") == 0)
                    151:                strcpy (lpszFileName, "\\Device\\Floppy0");
                    152:        else if (strcmp (lpszFileName, "Floppy (B:)") == 0)
                    153:                strcpy (lpszFileName, "\\Device\\Floppy1");
                    154: 
                    155:        UpperCaseCopy (lpszDiskFile, lpszFileName);
                    156: 
                    157:        *bDevice = FALSE;
                    158: 
                    159:        if (memcmp (lpszDiskFile, "\\DEVICE", sizeof (char) * 7) == 0)
                    160:        {
                    161:                *bDevice = TRUE;
                    162:        }
                    163: 
                    164:        strcpy (lpszDiskFile, lpszFileName);
                    165: 
                    166: #if _DEBUG
                    167:        OutputDebugString ("CreateFullVolumePath: ");
                    168:        OutputDebugString (lpszDiskFile);
                    169:        OutputDebugString ("\n");
                    170: #endif
                    171: 
                    172: }
                    173: 
                    174: int
                    175: FakeDosNameForDevice (char *lpszDiskFile, char *lpszDosDevice, char *lpszCFDevice, BOOL bNameOnly)
                    176: {
                    177:        BOOL bDosLinkCreated = TRUE;
                    178:        sprintf (lpszDosDevice, "truecrypt%lu", GetCurrentProcessId ());
                    179: 
                    180:        if (bNameOnly == FALSE)
                    181:                bDosLinkCreated = DefineDosDevice (DDD_RAW_TARGET_PATH, lpszDosDevice, lpszDiskFile);
                    182: 
                    183:        if (bDosLinkCreated == FALSE)
                    184:        {
                    185:                return ERR_OS_ERROR;
                    186:        }
                    187:        else
                    188:                sprintf (lpszCFDevice, "\\\\.\\%s", lpszDosDevice);
                    189: 
                    190:        return 0;
                    191: }
                    192: 
                    193: int
                    194: RemoveFakeDosName (char *lpszDiskFile, char *lpszDosDevice)
                    195: {
                    196:        BOOL bDosLinkRemoved = DefineDosDevice (DDD_RAW_TARGET_PATH | DDD_EXACT_MATCH_ON_REMOVE |
                    197:                        DDD_REMOVE_DEFINITION, lpszDosDevice, lpszDiskFile);
                    198:        if (bDosLinkRemoved == FALSE)
                    199:        {
                    200:                return ERR_OS_ERROR;
                    201:        }
                    202: 
                    203:        return 0;
                    204: }
                    205: 
                    206: char *
                    207: getstr (UINT nID)
                    208: {
                    209:        static char szMsg[256];
                    210:        if (LoadString (hInst, nID, szMsg, sizeof (szMsg)) == 0)
                    211:                return "";
                    212:        else
                    213:                return szMsg;
                    214: }
                    215: 
                    216: char *
                    217: getmultilinestr (UINT nID[4])
                    218: {
                    219:        static char szMsg[1024];
                    220:        if (nID[0])
                    221:                strcpy (szMsg, getstr (nID[0]));
                    222:        if (nID[1])
                    223:                strcat (szMsg, getstr (nID[1]));
                    224:        if (nID[2])
                    225:                strcat (szMsg, getstr (nID[2]));
                    226:        if (nID[3])
                    227:                strcat (szMsg, getstr (nID[3]));
                    228:        return szMsg;
                    229: 
                    230: }
                    231: 
                    232: void
                    233: AbortProcess (UINT nID)
                    234: {
                    235:        MessageBeep (MB_ICONEXCLAMATION);
                    236:        MessageBox (NULL, getstr (nID), lpszTitle, ICON_HAND);
                    237:        exit (1);
                    238: }
                    239: 
1.1.1.5 ! root      240: void
        !           241: AbortProcessSilent (void)
        !           242: {
        !           243:        exit (1);
        !           244: }
        !           245: 
1.1       root      246: void *
                    247: err_malloc (size_t size)
                    248: {
                    249:        void *z = (void *) TCalloc (size);
                    250:        if (z)
                    251:                return z;
                    252:        AbortProcess (IDS_OUTOFMEMORY);
                    253:        return 0;
                    254: }
                    255: 
                    256: char *
                    257: err_strdup (char *lpszText)
                    258: {
                    259:        int j = (strlen (lpszText) + 1) * sizeof (char);
                    260:        char *z = (char *) err_malloc (j);
                    261:        memmove (z, lpszText, j);
                    262:        return z;
                    263: }
                    264: 
1.1.1.5 ! root      265: DWORD
1.1       root      266: handleWin32Error (HWND hwndDlg)
                    267: {
                    268:        LPVOID lpMsgBuf;
                    269:        DWORD dwError = GetLastError ();
                    270: 
                    271:        FormatMessage (
                    272:                FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
                    273:                              NULL,
                    274:                              dwError,
                    275:                              MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT),       /* Default language */
                    276:                              (char *) &lpMsgBuf,
                    277:                              0,
                    278:                              NULL
                    279:            );
                    280: 
                    281:        MessageBox (hwndDlg, lpMsgBuf, lpszTitle, ICON_HAND);
                    282:        LocalFree (lpMsgBuf);
1.1.1.5 ! root      283: 
        !           284:        return dwError;
1.1       root      285: }
                    286: 
                    287: BOOL
                    288: translateWin32Error (char *lpszMsgBuf, int nSizeOfBuf)
                    289: {
                    290:        DWORD dwError = GetLastError ();
                    291: 
                    292:        if (FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM, NULL, dwError,
                    293:                           MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT),  /* Default language */
                    294:                           lpszMsgBuf, nSizeOfBuf, NULL))
                    295:                return TRUE;
                    296:        else
                    297:                return FALSE;
                    298: }
                    299: 
                    300: /* Except in response to the WM_INITDIALOG message, the dialog box procedure
                    301:    should return nonzero if it processes the message, and zero if it does
                    302:    not. - see DialogProc */
                    303: BOOL WINAPI
                    304: AboutDlgProc (HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam)
                    305: {
                    306:        WORD lw = LOWORD (wParam);
                    307:        if (lParam);            /* remove warning */
                    308: 
                    309:        switch (msg)
                    310:        {
                    311: 
                    312:        case WM_INITDIALOG:
                    313:                {
                    314:                        char szTmp[32];
1.1.1.5 ! root      315:                        char credits[5000];
1.1       root      316: 
                    317:                        SetDefaultUserFont (hwndDlg);
                    318: 
1.1.1.5 ! root      319:                        SendMessage (GetDlgItem (hwndDlg, IDC_HOMEPAGE), WM_SETFONT, (WPARAM) hUserUnderlineFont, 0);
        !           320:                        SendMessage (GetDlgItem (hwndDlg, IDC_FORUMS), WM_SETFONT, (WPARAM) hUserUnderlineFont, 0);
        !           321: 
        !           322:                        // Version
        !           323:                        SendMessage (GetDlgItem (hwndDlg, IDT_ABOUT_VERSION), WM_SETFONT, (WPARAM) hUserBoldFont, 0);
1.1       root      324:                        sprintf (szTmp, "TrueCrypt %s", VERSION_STRING);
                    325:                        SetDlgItemText (hwndDlg, IDT_ABOUT_VERSION, szTmp);
1.1.1.5 ! root      326: 
        !           327:                        // Credits
        !           328:                        SendMessage (GetDlgItem (hwndDlg, IDC_ABOUT_CREDITS), WM_SETFONT, (WPARAM) hUserFont, (LPARAM) 0);
        !           329:                        sprintf (credits,"\
        !           330: Based on E4M by Paul Le Roux.\r\n\
        !           331: Portions of this software are based in part on the works of the following people: \
        !           332: Bruce Schneier, \
        !           333: Horst Feistel, Don Coppersmith, \
        !           334: Whitfield Diffie, Martin Hellman, Walt Tuchmann, \
        !           335: Joan Daemen, Vincent Rijmen, \
        !           336: Lars Knudsen, Ross Anderson, Eli Biham, \
        !           337: David Wagner, John Kelsey, Niels Ferguson, Doug Whiting, Chris Hall, \
        !           338: Carlisle Adams, Stafford Tavares, \
        !           339: Hans Dobbertin, Antoon Bosselaers, Bart Preneel, \
        !           340: Peter Gutmann, and many others.\r\n\r\n\
        !           341: Portions of this software:\r\n\
        !           342: Copyright \xA9 2004 TrueCrypt Foundation. All Rights Reserved.\r\n\
        !           343: Copyright \xA9 1998-2000 Paul Le Roux. All Rights Reserved.\r\n\
        !           344: Copyright \xA9 2004 TrueCrypt Team. All Rights Reserved.\r\n\
        !           345: Copyright \xA9 1995-1997 Eric Young. All Rights Reserved.\r\n\
        !           346: Copyright \xA9 1999-2004 Dr. Brian Gladman. All Rights Reserved.\r\n\
        !           347: Copyright \xA9 2001 Markus Friedl. All Rights Reserved.\r\n\
        !           348: Copyright \xA9 2000 Dag Arne Osvik. All Rights Reserved.\r\n\r\n\
        !           349: A TrueCrypt Foundation Release");
        !           350:                        SetWindowText (GetDlgItem (hwndDlg, IDC_ABOUT_CREDITS), credits);
        !           351: 
1.1       root      352:                        return 1;
                    353:                }
                    354: 
                    355:        case WM_COMMAND:
                    356:                if (lw == IDOK || lw == IDCANCEL)
                    357:                {
                    358:                        EndDialog (hwndDlg, 0);
                    359:                        return 1;
                    360:                }
                    361: 
1.1.1.5 ! root      362:                if (lw == IDC_HOMEPAGE)
        !           363:                {
        !           364:                        char tmpstr [256];
        !           365: 
        !           366:                        ArrowWaitCursor ();
        !           367:                        sprintf (tmpstr, "http://truecrypt.sourceforge.net/applink.php?version=%s", VERSION_STRING);
        !           368:                        ShellExecute (NULL, "open", (LPCTSTR) tmpstr, NULL, NULL, SW_SHOWNORMAL);
        !           369:                        Sleep (200);
        !           370:                        NormalCursor ();
        !           371:                        return 1;
        !           372:                }
        !           373: 
        !           374:                if (lw == IDC_FORUMS)
        !           375:                {
        !           376:                        char tmpstr [256];
        !           377: 
        !           378:                        ArrowWaitCursor ();
        !           379:                        sprintf (tmpstr, "http://truecrypt.sourceforge.net/applink.php?version=%s&dest=forum", VERSION_STRING);
        !           380:                        ShellExecute (NULL, "open", (LPCTSTR) tmpstr, NULL, NULL, SW_SHOWNORMAL);
        !           381:                        Sleep (200);
        !           382:                        NormalCursor ();
        !           383:                        return 1;
        !           384:                }
        !           385: 
        !           386:                // Disallow modification of credits
        !           387:                if (HIWORD (wParam) == EN_UPDATE)
        !           388:                {
        !           389:                        SendMessage (hwndDlg, WM_INITDIALOG, 0, 0);
        !           390:                        return 1;
        !           391:                }
        !           392: 
1.1       root      393:                return 0;
                    394: 
                    395:        case WM_CLOSE:
                    396:                EndDialog (hwndDlg, 0);
                    397:                return 1;
                    398:        }
                    399: 
                    400:        return 0;
                    401: }
                    402: 
                    403: /* Except in response to the WM_INITDIALOG message, the dialog box procedure
                    404:    should return nonzero if it processes the message, and zero if it does
                    405:    not. - see DialogProc */
                    406: BOOL WINAPI
                    407: WarningDlgProc (HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam)
                    408: {
                    409:        WORD lw = LOWORD (wParam);
                    410:        if (lParam);            /* remove warning */
                    411: 
                    412:        switch (msg)
                    413:        {
                    414:        case WM_INITDIALOG:
                    415:                SetDefaultUserFont (hwndDlg);
                    416:                SetWindowText (GetDlgItem (hwndDlg, IDC_WARNING_TEXT), (char*) lParam);
                    417:                return 1;
                    418:        case WM_COMMAND:
                    419:                if (lw == IDOK || lw == IDCANCEL)
                    420:                {
                    421:                        BOOL x = IsButtonChecked (GetDlgItem (hwndDlg, IDC_NEVER_SHOW));
                    422:                        if (x == TRUE)
                    423:                                EndDialog (hwndDlg, IDOK);
                    424:                        else
                    425:                                EndDialog (hwndDlg, IDCANCEL);
                    426:                        return 1;
                    427:                }
                    428:                return 0;
                    429:        case WM_CLOSE:
                    430:                EndDialog (hwndDlg, 0);
                    431:                return 1;
                    432:        }
                    433: 
                    434:        return 0;
                    435: }
                    436: 
                    437: BOOL
                    438: IsButtonChecked (HWND hButton)
                    439: {
                    440:        if (SendMessage (hButton, BM_GETCHECK, 0, 0) == BST_CHECKED)
                    441:                return TRUE;
                    442:        else
                    443:                return FALSE;
                    444: }
                    445: 
                    446: void
                    447: CheckButton (HWND hButton)
                    448: {
                    449:        SendMessage (hButton, BM_SETCHECK, BST_CHECKED, 0);
                    450: }
                    451: 
                    452: 
                    453: /*****************************************************************************
                    454:   ToSBCS: converts a unicode string to Single Byte Character String (SBCS).
                    455:   ***************************************************************************/
                    456: 
                    457: void
                    458: ToSBCS (LPWSTR lpszText)
                    459: {
                    460:        int j = wcslen (lpszText);
                    461:        if (j == 0)
                    462:        {
                    463:                strcpy ((char *) lpszText, "");
                    464:                return;
                    465:        }
                    466:        else
                    467:        {
                    468:                char *lpszNewText = (char *) err_malloc (j + 1);
                    469:                j = WideCharToMultiByte (CP_ACP, 0L, lpszText, -1, lpszNewText, j + 1, NULL, NULL);
                    470:                if (j > 0)
                    471:                        strcpy ((char *) lpszText, lpszNewText);
                    472:                else
                    473:                        strcpy ((char *) lpszText, "");
                    474:                free (lpszNewText);
                    475:        }
                    476: }
                    477: 
                    478: /*****************************************************************************
                    479:   ToUNICODE: converts a SBCS string to a UNICODE string.
                    480:   ***************************************************************************/
                    481: 
                    482: void
                    483: ToUNICODE (char *lpszText)
                    484: {
                    485:        int j = strlen (lpszText);
                    486:        if (j == 0)
                    487:        {
                    488:                wcscpy ((LPWSTR) lpszText, (LPWSTR) WIDE (""));
                    489:                return;
                    490:        }
                    491:        else
                    492:        {
                    493:                LPWSTR lpszNewText = (LPWSTR) err_malloc ((j + 1) * 2);
                    494:                j = MultiByteToWideChar (CP_ACP, 0L, lpszText, -1, lpszNewText, j + 1);
                    495:                if (j > 0)
                    496:                        wcscpy ((LPWSTR) lpszText, lpszNewText);
                    497:                else
                    498:                        wcscpy ((LPWSTR) lpszText, (LPWSTR) "");
                    499:                free (lpszNewText);
                    500:        }
                    501: }
                    502: 
                    503: /* InitDialog - initialize the applications main dialog, this function should
                    504:    be called only once in the dialogs WM_INITDIALOG message handler */
                    505: void
                    506: InitDialog (HWND hwndDlg)
                    507: {
                    508:        HDC hDC;
                    509:        int nHeight;
                    510:        LOGFONT lf;
                    511:        HMENU hMenu;
                    512: 
                    513:        hDC = GetDC (hwndDlg);
                    514: 
                    515:        nHeight = -((8 * GetDeviceCaps (hDC, LOGPIXELSY)) / 72);
                    516:        lf.lfHeight = nHeight;
                    517:        lf.lfWidth = 0;
                    518:        lf.lfEscapement = 0;
                    519:        lf.lfOrientation = 0;
                    520:        lf.lfWeight = FW_LIGHT;
                    521:        lf.lfItalic = FALSE;
                    522:        lf.lfUnderline = FALSE;
                    523:        lf.lfStrikeOut = FALSE;
                    524:        lf.lfCharSet = DEFAULT_CHARSET;
                    525:        lf.lfOutPrecision = OUT_DEFAULT_PRECIS;
                    526:        lf.lfClipPrecision = CLIP_DEFAULT_PRECIS;
                    527:        lf.lfQuality = PROOF_QUALITY;
                    528:        lf.lfPitchAndFamily = FF_DONTCARE;
                    529:        strcpy (lf.lfFaceName, "Courier");
                    530:        hSmallFont = CreateFontIndirect (&lf);
                    531:        if (hSmallFont == NULL)
                    532:        {
                    533:                handleWin32Error (hwndDlg);
                    534:                AbortProcess (IDS_NOFONT);
                    535:        }
                    536: 
                    537:        nHeight = -((10 * GetDeviceCaps (hDC, LOGPIXELSY)) / 72);
                    538:        lf.lfHeight = nHeight;
                    539:        lf.lfWeight = FW_BLACK;
                    540:        strcpy (lf.lfFaceName, "Arial");
                    541:        hSmallBoldFont = CreateFontIndirect (&lf);
                    542:        if (hSmallBoldFont == NULL)
                    543:        {
                    544:                handleWin32Error (hwndDlg);
                    545:                AbortProcess (IDS_NOFONT);
                    546:        }
                    547: 
                    548:        nHeight = -((16 * GetDeviceCaps (hDC, LOGPIXELSY)) / 72);
                    549:        lf.lfHeight = nHeight;
                    550:        lf.lfWeight = FW_BOLD;
                    551:        strcpy (lf.lfFaceName, "Times");
                    552:        hBoldFont = CreateFontIndirect (&lf);
                    553:        if (hBoldFont == NULL)
                    554:        {
                    555:                handleWin32Error (hwndDlg);
                    556:                AbortProcess (IDS_NOFONT);
                    557:        }
                    558: 
                    559:        nHeight = -((16 * GetDeviceCaps (hDC, LOGPIXELSY)) / 72);
                    560:        lf.lfHeight = nHeight;
                    561:        lf.lfWeight = FW_REGULAR;
                    562:        hTitleFont = CreateFontIndirect (&lf);
                    563:        if (hTitleFont == NULL)
                    564:        {
                    565:                handleWin32Error (hwndDlg);
                    566:                AbortProcess (IDS_NOFONT);
                    567:        }
                    568: 
                    569:        nHeight = -((9 * GetDeviceCaps (hDC, LOGPIXELSY)) / 72);
                    570:        lf.lfHeight = nHeight;
                    571:        lf.lfWidth = 0;
                    572:        lf.lfEscapement = 0;
                    573:        lf.lfOrientation = 0;
                    574:        lf.lfWeight = FW_NORMAL;
                    575:        lf.lfItalic = FALSE;
                    576:        lf.lfUnderline = FALSE;
                    577:        lf.lfStrikeOut = FALSE;
                    578:        lf.lfCharSet = DEFAULT_CHARSET;
                    579:        lf.lfOutPrecision = OUT_DEFAULT_PRECIS;
                    580:        lf.lfClipPrecision = CLIP_DEFAULT_PRECIS;
                    581:        lf.lfQuality = PROOF_QUALITY;
                    582:        lf.lfPitchAndFamily = FF_DONTCARE;
                    583:        strcpy (lf.lfFaceName, "Lucida Console");
                    584:        hFixedFont = CreateFontIndirect (&lf);
                    585:        if (hFixedFont == NULL)
                    586:        {
                    587:                handleWin32Error (hwndDlg);
                    588:                AbortProcess (IDS_NOFONT);
                    589:        }
                    590: 
                    591:        hMenu = GetSystemMenu (hwndDlg, FALSE);
                    592:        AppendMenu (hMenu, MF_SEPARATOR, 0, NULL);
                    593:        AppendMenu (hMenu, MF_ENABLED | MF_STRING, IDC_ABOUT, getstr (IDS_ABOUTBOX));
1.1.1.5 ! root      594: 
        !           595:        SetDefaultUserFont(hwndDlg);
1.1       root      596: }
                    597: 
                    598: HDC
                    599: CreateMemBitmap (HINSTANCE hInstance, HWND hwnd, char *resource)
                    600: {
                    601:        HBITMAP picture = LoadBitmap (hInstance, resource);
                    602:        HDC viewDC = GetDC (hwnd), dcMem;
                    603: 
                    604:        dcMem = CreateCompatibleDC (viewDC);
                    605: 
                    606:        SetMapMode (dcMem, MM_TEXT);
                    607: 
                    608:        SelectObject (dcMem, picture);
                    609: 
                    610:        ReleaseDC (hwnd, viewDC);
                    611: 
                    612:        return dcMem;
                    613: }
                    614: 
                    615: /* Draw the specified bitmap at the specified location - Stretch to fit. */
                    616: void
                    617: PaintBitmap (HDC pdcMem, int x, int y, int nWidth, int nHeight, HDC hDC)
                    618: {
                    619:        HGDIOBJ picture = GetCurrentObject (pdcMem, OBJ_BITMAP);
                    620: 
                    621:        BITMAP bitmap;
                    622:        GetObject (picture, sizeof (BITMAP), &bitmap);
                    623: 
                    624:        BitBlt (hDC, x, y, nWidth, nHeight, pdcMem, 0, 0, SRCCOPY);
                    625: }
                    626: 
                    627: LRESULT CALLBACK
                    628: SplashDlgProc (HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
                    629: {
                    630: 
                    631:        //if (uMsg == WM_ERASEBKGND)
                    632:        //{
                    633:        //      NONCLIENTMETRICS metric;
                    634:        //      HFONT font;
                    635: 
                    636: 
                    637:        //      HDC hDC = (HDC) wParam;
                    638:        //      char szTmp[64];
                    639:        //      HGDIOBJ obj;
                    640:        //      WORD bx = LOWORD (GetDialogBaseUnits ());
                    641:        //      WORD by = HIWORD (GetDialogBaseUnits ());
                    642: 
                    643: 
                    644:        //      DefDlgProc (hwnd, uMsg, wParam, lParam);
                    645: 
                    646:        //      SetBkMode (hDC, TRANSPARENT);
                    647:        //      SetTextColor (hDC, RGB (0, 0, 100));
                    648:        //      obj = SelectObject (hDC, hTitleFont);
                    649: 
                    650:        //      metric.cbSize = sizeof (NONCLIENTMETRICS);
                    651:        //      SystemParametersInfo (SPI_GETNONCLIENTMETRICS, sizeof(NONCLIENTMETRICS), &metric, 0);
                    652:        //      font = CreateFontIndirect (&metric.lfMessageFont);
                    653:        //      obj = SelectObject (hDC, font);
                    654: 
                    655:        //      TextOut (hDC, (12 * bx) / 4, (70 * by) / 8, szTmp, strlen (szTmp));
                    656: 
                    657:        //      SelectObject (hDC, obj);
                    658:        //      return TRUE;
                    659:        //}
                    660: 
                    661:        return DefDlgProc (hwnd, uMsg, wParam, lParam);
                    662: }
                    663: 
                    664: void
                    665: WaitCursor ()
                    666: {
                    667:        static HCURSOR hcWait;
                    668:        if (hcWait == NULL)
                    669:                hcWait = LoadCursor (NULL, IDC_WAIT);
                    670:        SetCursor (hcWait);
                    671:        hCursor = hcWait;
                    672: }
                    673: 
                    674: void
                    675: NormalCursor ()
                    676: {
                    677:        static HCURSOR hcArrow;
                    678:        if (hcArrow == NULL)
                    679:                hcArrow = LoadCursor (NULL, IDC_ARROW);
                    680:        SetCursor (hcArrow);
                    681:        hCursor = NULL;
                    682: }
                    683: 
                    684: void
                    685: ArrowWaitCursor ()
                    686: {
                    687:        static HCURSOR hcArrowWait;
                    688:        if (hcArrowWait == NULL)
                    689:                hcArrowWait = LoadCursor (NULL, IDC_APPSTARTING);
                    690:        SetCursor (hcArrowWait);
                    691:        hCursor = hcArrowWait;
                    692: }
                    693: 
                    694: LRESULT CALLBACK
                    695: CustomDlgProc (HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
                    696: {
                    697:        if (uMsg == WM_SETCURSOR && hCursor != NULL)
                    698:        {
                    699:                SetCursor (hCursor);
                    700:                return TRUE;
                    701:        }
                    702: 
                    703:        return DefDlgProc (hwnd, uMsg, wParam, lParam);
                    704: }
                    705: 
                    706: /* InitApp - initialize the application, this function is called once in the
                    707:    applications WinMain function, but before the main dialog has been created */
                    708: void
                    709: InitApp (HINSTANCE hInstance)
                    710: {
                    711:        WNDCLASS wc;
                    712:        char *lpszTmp;
                    713:        OSVERSIONINFO os;
                    714: 
                    715:        /* Save the instance handle for later */
                    716:        hInst = hInstance;
                    717: 
                    718:        /* Pull down the windows version */
                    719:        os.dwOSVersionInfoSize = sizeof (OSVERSIONINFO);
                    720:        if (GetVersionEx (&os) == FALSE)
                    721:                AbortProcess (IDS_NO_OS_VER);
                    722:        else if (os.dwPlatformId == VER_PLATFORM_WIN32_NT)
                    723:                nCurrentOS = WIN_NT;
                    724:        else if (os.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS && os.dwMajorVersion == 4 && os.dwMinorVersion == 0)
                    725:                nCurrentOS = WIN_95;
                    726:        else if (os.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS && os.dwMajorVersion == 4 && os.dwMinorVersion == 10)
                    727:                nCurrentOS = WIN_98;
                    728:        else {
                    729:        /*      AbortProcess (IDS_NO_OS_VER); */
                    730:                nCurrentOS = WIN_98;
                    731:        }
                    732: 
                    733:        CurrentOSMajor = os.dwMajorVersion;
                    734:        CurrentOSMinor = os.dwMinorVersion;
                    735: 
                    736:        /* Get the attributes for the standard dialog class */
                    737:        if ((GetClassInfo (hInst, WINDOWS_DIALOG_CLASS, &wc)) == 0)
                    738:                AbortProcess (IDS_INIT_REGISTER);
                    739: 
                    740: #ifndef SETUP
                    741:        wc.hIcon = LoadIcon (hInstance, MAKEINTRESOURCE (IDI_TRUECRYPT_ICON));
                    742: #else
                    743: #include "../setup/resource.h"
                    744:        wc.hIcon = LoadIcon (hInstance, MAKEINTRESOURCE (IDI_SETUP));
                    745: #endif
                    746:        wc.lpszClassName = TC_DLG_CLASS;
                    747:        wc.lpfnWndProc = &CustomDlgProc;
                    748:        wc.hCursor = LoadCursor (NULL, IDC_ARROW);
                    749:        wc.cbWndExtra = DLGWINDOWEXTRA;
                    750: 
                    751:        hDlgClass = RegisterClass (&wc);
                    752:        if (hDlgClass == 0)
                    753:                AbortProcess (IDS_INIT_REGISTER);
                    754: 
                    755:        wc.lpszClassName = TC_SPLASH_CLASS;
                    756:        wc.lpfnWndProc = &SplashDlgProc;
                    757:        wc.hCursor = LoadCursor (NULL, IDC_ARROW);
                    758:        wc.cbWndExtra = DLGWINDOWEXTRA;
                    759: 
                    760:        hSplashClass = RegisterClass (&wc);
                    761:        if (hSplashClass == 0)
                    762:                AbortProcess (IDS_INIT_REGISTER);
                    763: 
                    764:        GetModuleFileName (NULL, szHelpFile, sizeof (szHelpFile));
                    765:        lpszTmp = strrchr (szHelpFile, '\\');
                    766:        if (lpszTmp)
                    767:        {
                    768:                strcpy (++lpszTmp, "TrueCrypt User Guide.pdf");
                    769:        }
                    770: 
                    771:        hMutex = CreateMutex (NULL, TRUE, lpszTitle);
                    772:        if (hMutex == NULL)
                    773:        {
                    774:                handleWin32Error (NULL);
                    775:                AbortProcess (IDS_INIT_MUTEX);
                    776:        }
                    777: 
                    778:        if (GetLastError ()== ERROR_ALREADY_EXISTS)
                    779:        {
                    780:                // If executed twice, just top the first instance and exit
                    781:                HWND h = FindWindow (0, lpszTitle);
                    782:                if (h != 0)
                    783:                {
                    784:                        ShowWindow (h, SW_SHOWNORMAL);
                    785:                        SetForegroundWindow (h);
                    786:                        exit (1);
                    787:                }
                    788:                else
                    789:                        AbortProcess (IDS_TWO_INSTANCES);
                    790:        }
                    791: }
                    792: 
                    793: 
                    794: BOOL
                    795: OpenDevice (char *lpszPath, OPEN_TEST_STRUCT * driver)
                    796: {
                    797:        DWORD dwResult;
                    798:        BOOL bResult;
                    799: 
                    800:        strcpy ((char *) &driver->wszFileName[0], lpszPath);
                    801: 
                    802:        if (nCurrentOS == WIN_NT)
                    803:                ToUNICODE ((char *) &driver->wszFileName[0]);
                    804: 
                    805:        bResult = DeviceIoControl (hDriver, OPEN_TEST,
                    806:                                   driver, sizeof (OPEN_TEST_STRUCT),
                    807:                                   &driver, sizeof (OPEN_TEST_STRUCT),
                    808:                                   &dwResult, NULL);
                    809: 
                    810:        if (bResult == FALSE)
                    811:        {
                    812:                dwResult = GetLastError ();
                    813:                if (dwResult == ERROR_SHARING_VIOLATION)
                    814:                        return TRUE;
                    815:                else
                    816:                        return FALSE;
                    817:        }
                    818:        else
                    819:        {
                    820:                if (nCurrentOS == WIN_NT)
                    821:                        return TRUE;
                    822:                else if (driver->nReturnCode == 0)
                    823:                        return TRUE;
                    824:                else
                    825:                {
                    826:                        SetLastError (ERROR_FILE_NOT_FOUND);
                    827:                        return FALSE;
                    828:                }
                    829:        }
                    830: }
                    831: 
                    832: UINT _stdcall
                    833: win9x_io (HFILE hFile, char *lpBuffer, UINT uBytes)
                    834: {
                    835:        DISKIO_STRUCT *win9x_r0 = (DISKIO_STRUCT *) hFile;
                    836:        DWORD dwResult;
                    837:        BOOL bResult;
                    838:        LONG secs;
                    839: 
                    840:        win9x_r0->bufferad = (void *) lpBuffer;
                    841: 
                    842:        secs = uBytes / SECTOR_SIZE;
                    843: 
                    844:        win9x_r0->sectorlen = secs;
                    845: 
                    846:        bResult = DeviceIoControl (hDriver, DISKIO, win9x_r0, sizeof (DISKIO_STRUCT), win9x_r0,
                    847:                                   sizeof (DISKIO_STRUCT), &dwResult, NULL);
                    848: 
                    849:        if (bResult == FALSE || win9x_r0->nReturnCode != 0)
                    850:                return (UINT) HFILE_ERROR;
                    851: 
                    852:        win9x_r0->sectorstart += secs;
                    853: 
                    854:        return uBytes;
                    855: }
                    856: 
                    857: int
                    858: GetAvailableFixedDisks (HWND hComboBox, char *lpszRootPath)
                    859: {
                    860:        int i, n;
                    861:        int line = 0;
1.1.1.5 ! root      862:        LVITEM LvItem;
1.1       root      863: 
                    864:        for (i = 0; i < 64; i++)
                    865:        {
                    866:                BOOL drivePresent = FALSE;
                    867: 
1.1.1.5 ! root      868:                for (n = 0; n <= 32; n++)
1.1       root      869:                {
                    870:                        char szTmp[TC_MAX_PATH], item1[100]={0}, item2[100]={0};
                    871:                        OPEN_TEST_STRUCT driver;
                    872: 
                    873:                        sprintf (szTmp, lpszRootPath, i, n);
                    874:                        if (OpenDevice (szTmp, &driver) == TRUE)
                    875:                        {
                    876:                                int nDosLinkCreated;
                    877:                                HANDLE dev;
                    878:                                DWORD dwResult;
                    879:                                BOOL bResult;
                    880:                                PARTITION_INFORMATION diskInfo;
                    881:                                char szDosDevice[TC_MAX_PATH], szCFDevice[TC_MAX_PATH];
                    882: 
                    883:                                drivePresent = TRUE;
                    884: 
                    885:                                if (nCurrentOS == WIN_NT)
                    886:                                {
                    887:                                        nDosLinkCreated = FakeDosNameForDevice (szTmp, szDosDevice,
                    888:                                                szCFDevice, FALSE);
                    889: 
1.1.1.5 ! root      890:                                        dev = CreateFile (szCFDevice, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE , NULL, OPEN_EXISTING, FILE_ATTRIBUTE_READONLY, NULL);
1.1       root      891: 
                    892:                                        bResult = DeviceIoControl (dev, IOCTL_DISK_GET_PARTITION_INFO, NULL, 0,
                    893:                                                &diskInfo, sizeof (diskInfo), &dwResult, NULL);
                    894: 
                    895:                                        RemoveFakeDosName(szTmp, szDosDevice);
                    896:                                        CloseHandle(dev);
                    897: 
                    898:                                        if (bResult == TRUE)
                    899:                                        {
                    900:                                                char partType[100];
                    901: 
                    902:                                                switch(diskInfo.PartitionType)
                    903:                                                {
1.1.1.5 ! root      904:                                                case PARTITION_ENTRY_UNUSED:    strcpy(partType, "Empty/unused"); break;
        !           905:                                                case PARTITION_XINT13_EXTENDED:
1.1       root      906:                                                case PARTITION_EXTENDED:                strcpy(partType, "Extended"); break;
1.1.1.5 ! root      907:                                                case PARTITION_HUGE:                    sprintf(partType, "Unformatted (0x%02X)", diskInfo.PartitionType); break;
1.1       root      908:                                                case PARTITION_FAT_12:                  strcpy(partType, "FAT12"); break;
                    909:                                                case PARTITION_FAT_16:                  strcpy(partType, "FAT16"); break;
                    910:                                                case PARTITION_FAT32:           
                    911:                                                case PARTITION_FAT32_XINT13:    strcpy(partType, "FAT32"); break;
1.1.1.5 ! root      912:                                                case 0x08:                                              strcpy(partType, "DELL (spanning)"); break;
        !           913:                                                case 0x12:                                              strcpy(partType, "Config/diagnostics"); break;
1.1       root      914:                                                case 0x11:
                    915:                                                case 0x14:
                    916:                                                case 0x16:
                    917:                                                case 0x1b:
                    918:                                                case 0x1c:
                    919:                                                case 0x1e:                                              strcpy(partType, "Hidden FAT"); break;
                    920:                                                case PARTITION_IFS:                             strcpy(partType, "NTFS"); break;
                    921:                                                case 0x17:                                              strcpy(partType, "Hidden NTFS"); break;
1.1.1.5 ! root      922:                                                case 0x3c:                                              strcpy(partType, "PMagic recovery"); break;
        !           923:                                                case 0x3d:                                              strcpy(partType, "Hidden NetWare"); break;
        !           924:                                                case 0x41:                                              strcpy(partType, "Linux/MINIX"); break;
        !           925:                                                case 0x42:                                              strcpy(partType, "SFS/LDM/Linux Swap"); break;
        !           926:                                                case 0x51:
        !           927:                                                case 0x64:
        !           928:                                                case 0x65:
        !           929:                                                case 0x66:
        !           930:                                                case 0x67:
        !           931:                                                case 0x68:
        !           932:                                                case 0x69:                                              strcpy(partType, "Novell"); break;
        !           933:                                                case 0x55:                                              strcpy(partType, "EZ-Drive"); break;
        !           934:                                                case PARTITION_OS2BOOTMGR:              strcpy(partType, "OS/2 BM"); break;
        !           935:                                                case PARTITION_XENIX_1:
        !           936:                                                case PARTITION_XENIX_2:                 strcpy(partType, "Xenix"); break;
1.1       root      937:                                                case PARTITION_UNIX:                    strcpy(partType, "UNIX"); break;
1.1.1.5 ! root      938:                                                case 0x74:                                              strcpy(partType, "Scramdisk"); break;
        !           939:                                                case 0x78:                                              strcpy(partType, "XOSL FS"); break;
        !           940:                                                case 0x80:
        !           941:                                                case 0x81:                                              strcpy(partType, "MINIX"); break;
1.1       root      942:                                                case 0x82:                                              strcpy(partType, "Linux Swap"); break;
1.1.1.5 ! root      943:                                                case 0x43:
        !           944:                                                case 0x83:                                              strcpy(partType, "Linux"); break;
        !           945:                                                case 0xc2:
        !           946:                                                case 0x93:                                              strcpy(partType, "Hidden Linux"); break;
        !           947:                                                case 0x86:
        !           948:                                                case 0x87:                                              strcpy(partType, "NTFS volume set"); break;
        !           949:                                                case 0x9f:                                              strcpy(partType, "BSD/OS"); break;
        !           950:                                                case 0xa0:
        !           951:                                                case 0xa1:                                              strcpy(partType, "Hibernation"); break;
        !           952:                                                case 0xa5:                                              strcpy(partType, "BSD"); break;
        !           953:                                                case 0xa8:                                              strcpy(partType, "Mac OS-X"); break;
        !           954:                                                case 0xa9:                                              strcpy(partType, "NetBSD"); break;
        !           955:                                                case 0xab:                                              strcpy(partType, "Mac OS-X Boot"); break;
        !           956:                                                case 0xb8:                                              strcpy(partType, "BSDI BSD/386 swap"); break;
        !           957:                                                case 0xc3:                                              strcpy(partType, "Hidden Linux swap"); break;
        !           958:                                                case 0xfb:                                              strcpy(partType, "VMware"); break;
        !           959:                                                case 0xfc:                                              strcpy(partType, "VMware swap"); break;
        !           960:                                                case 0xfd:                                              strcpy(partType, "Linux RAID"); break;
        !           961:                                                case 0xfe:                                              strcpy(partType, "WinNT hidden"); break;
        !           962:                                                default:                                                sprintf(partType, "0x%02X", diskInfo.PartitionType); break;
1.1       root      963:                                                }
                    964: 
1.1.1.5 ! root      965:                                                if (diskInfo.PartitionLength.QuadPart > 1024I64*1024*1024*1024*1024*99)
        !           966:                                                        sprintf (item1,"%d PB", diskInfo.PartitionLength.QuadPart/1024/1024/1024/1024/1024);
        !           967:                                                else if (diskInfo.PartitionLength.QuadPart > 1024I64*1024*1024*1024*1024)
        !           968:                                                        sprintf (item1,"%.1f PB",(double)(diskInfo.PartitionLength.QuadPart/1024.0/1024/1024/1024/1024));
        !           969:                                                else if (diskInfo.PartitionLength.QuadPart > 1024I64*1024*1024*1024*99)
        !           970:                                                        sprintf (item1,"%d TB",diskInfo.PartitionLength.QuadPart/1024.0/1024/1024/1024);
        !           971:                                                else if (diskInfo.PartitionLength.QuadPart > 1024I64*1024*1024*1024)
        !           972:                                                        sprintf (item1,"%.1f TB",(double)(diskInfo.PartitionLength.QuadPart/1024.0/1024/1024/1024));
        !           973:                                                else if (diskInfo.PartitionLength.QuadPart > 1024I64*1024*1024*99)
        !           974:                                                        sprintf (item1,"%d GB",diskInfo.PartitionLength.QuadPart/1024/1024/1024);
        !           975:                                                else if (diskInfo.PartitionLength.QuadPart > 1024I64*1024*1024)
1.1       root      976:                                                        sprintf (item1,"%.1f GB",(double)(diskInfo.PartitionLength.QuadPart/1024.0/1024/1024));
                    977:                                                else
                    978:                                                        sprintf (item1,"%d MB", diskInfo.PartitionLength.QuadPart/1024/1024);
                    979: 
                    980:                                                strcpy (item2, partType);
                    981:                                        }
                    982:                                }
                    983: 
1.1.1.5 ! root      984:                                if (n == 0)
        !           985:                                        sprintf (szTmp, "Harddisk %d:", i);
        !           986: 
        !           987:                                memset (&LvItem,0,sizeof(LvItem));
        !           988:                                LvItem.mask = LVIF_TEXT;   
        !           989:                                LvItem.iItem = line++;   
        !           990: 
        !           991:                                // Device Name
        !           992:                                LvItem.pszText = szTmp;
        !           993:                                SendMessage (hComboBox,LVM_INSERTITEM,0,(LPARAM)&LvItem);
        !           994: 
        !           995:                                // Size
        !           996:                                LvItem.iSubItem = 2;
        !           997:                                LvItem.pszText = item1;
        !           998:                                SendMessage (hComboBox,LVM_SETITEM,0,(LPARAM)&LvItem); 
        !           999: 
        !          1000:                                if (n > 0)
        !          1001:                                {
        !          1002:                                        char drive[] = { 0, ':', 0 };
        !          1003:                                        char device[MAX_PATH * 2];
        !          1004:                                        int driveNo;
        !          1005: 
        !          1006:                                        // Drive letter
        !          1007:                                        strcpy (device, szTmp);
        !          1008:                                        ToUNICODE (device);
        !          1009:                                        driveNo = GetDiskDeviceDriveLetter ((PWSTR) device);
        !          1010:                                        drive[0] = driveNo == -1 ? 0 : 'A' + driveNo;
        !          1011: 
        !          1012:                                        LvItem.iSubItem = 1;
        !          1013:                                        LvItem.pszText = drive;
        !          1014:                                        SendMessage (hComboBox,LVM_SETITEM,0,(LPARAM)&LvItem);
        !          1015: 
        !          1016:                                        // Partition type
        !          1017:                                        LvItem.iSubItem = 3;
        !          1018:                                        LvItem.pszText = item2;
        !          1019:                                        SendMessage (hComboBox,LVM_SETITEM,0,(LPARAM)&LvItem);
        !          1020:                                }
1.1       root     1021: 
1.1.1.5 ! root     1022:                                // Mark device with partitions
        !          1023:                                if (n == 1)
        !          1024:                                {
        !          1025:                                        LvItem.iItem = line - 2;
        !          1026:                                        LvItem.iSubItem = 3;
        !          1027:                                        LvItem.pszText = " ";
        !          1028:                                        SendMessage (hComboBox,LVM_SETITEM,0,(LPARAM)&LvItem);
        !          1029:                                }
1.1       root     1030: 
1.1.1.5 ! root     1031:                        }
1.1.1.2   root     1032:                }
                   1033: 
1.1.1.5 ! root     1034:                if (drivePresent)
1.1.1.2   root     1035:                {
1.1.1.5 ! root     1036:                        memset (&LvItem,0,sizeof(LvItem));
        !          1037:                        LvItem.mask = LVIF_TEXT;   
        !          1038:                        LvItem.iItem = line++;   
1.1.1.2   root     1039: 
1.1.1.5 ! root     1040:                        LvItem.pszText = "";
        !          1041:                        SendMessage (hComboBox,LVM_INSERTITEM,0,(LPARAM)&LvItem);
1.1       root     1042:                }
                   1043:        }
                   1044: 
                   1045:        i = SendMessage (hComboBox, LVM_GETITEMCOUNT, 0, 0);
                   1046:        if (i != CB_ERR)
                   1047:                return i;
                   1048:        else
                   1049:                return 0;
                   1050: }
                   1051: 
                   1052: int
                   1053: GetAvailableRemovables (HWND hComboBox, char *lpszRootPath)
                   1054: {
                   1055:        char szTmp[TC_MAX_PATH];
                   1056:        int i;
                   1057:        LVITEM LvItem;
                   1058: 
                   1059:        if (lpszRootPath);      /* Remove unused parameter warning */
                   1060: 
                   1061:        if (nCurrentOS != WIN_NT)
                   1062:                return 0;
                   1063: 
1.1.1.5 ! root     1064:        memset (&LvItem,0,sizeof(LvItem));
1.1       root     1065:        LvItem.mask = LVIF_TEXT;   
                   1066:        LvItem.iItem = SendMessage (hComboBox, LVM_GETITEMCOUNT, 0, 0)+1;   
                   1067: 
                   1068:        if (QueryDosDevice ("A:", szTmp, sizeof (szTmp)) != 0)
                   1069:        {
1.1.1.5 ! root     1070:                LvItem.pszText = "\\Device\\Floppy0";
        !          1071:                LvItem.iItem = SendMessage (hComboBox,LVM_INSERTITEM,0,(LPARAM)&LvItem);
        !          1072: 
        !          1073:                LvItem.iSubItem = 1;
        !          1074:                LvItem.pszText = "A:";
        !          1075:                SendMessage (hComboBox,LVM_SETITEM,0,(LPARAM)&LvItem);
        !          1076: 
1.1       root     1077:        }
                   1078:        if (QueryDosDevice ("B:", szTmp, sizeof (szTmp)) != 0)
                   1079:        {
1.1.1.5 ! root     1080:                LvItem.pszText = "\\Device\\Floppy1";
        !          1081:                LvItem.iSubItem = 0;
        !          1082:                LvItem.iItem = SendMessage (hComboBox, LVM_GETITEMCOUNT, 0, 0)+1;   
        !          1083:                LvItem.iItem = SendMessage (hComboBox,LVM_INSERTITEM,0,(LPARAM)&LvItem);
        !          1084: 
        !          1085:                LvItem.iSubItem = 1;
        !          1086:                LvItem.pszText = "B:";
        !          1087:                SendMessage (hComboBox,LVM_SETITEM,0,(LPARAM)&LvItem);
1.1       root     1088:        }
                   1089: 
                   1090:        i = SendMessage (hComboBox, LVM_GETITEMCOUNT, 0, 0);
                   1091:        if (i != CB_ERR)
                   1092:                return i;
                   1093:        else
                   1094:                return 0;
                   1095: }
                   1096: 
                   1097: BOOL WINAPI
                   1098: RawDevicesDlgProc (HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam)
                   1099: {
                   1100:        static char *lpszFileName;
                   1101:        WORD lw = LOWORD (wParam);
                   1102: 
                   1103:        if (lParam);            /* remove warning */
                   1104: 
                   1105:        switch (msg)
                   1106:        {
                   1107:        case WM_INITDIALOG:
                   1108:                {
                   1109:                        int nCount;
                   1110:                        LVCOLUMN LvCol;
                   1111:                        HWND hList = GetDlgItem (hwndDlg, IDC_DEVICELIST);
                   1112: 
                   1113:                        SetDefaultUserFont (hwndDlg);
                   1114: 
1.1.1.5 ! root     1115:                        SendMessage (hList,LVM_SETEXTENDEDLISTVIEWSTYLE,0,
1.1       root     1116:                                LVS_EX_FULLROWSELECT|LVS_EX_HEADERDRAGDROP|LVS_EX_TWOCLICKACTIVATE 
                   1117:                                ); 
                   1118: 
1.1.1.5 ! root     1119:                        memset (&LvCol,0,sizeof(LvCol));               
1.1       root     1120:                        LvCol.mask = LVCF_TEXT|LVCF_WIDTH|LVCF_SUBITEM|LVCF_FMT;  
                   1121:                        LvCol.pszText = "Device";                           
1.1.1.5 ! root     1122:                        LvCol.cx =154;
        !          1123:                        LvCol.fmt = LVCFMT_LEFT;
        !          1124:                        SendMessage (hList,LVM_INSERTCOLUMN,0,(LPARAM)&LvCol);
        !          1125: 
        !          1126:                        LvCol.pszText = "Drive";  
        !          1127:                        LvCol.cx = 38;           
1.1       root     1128:                        LvCol.fmt = LVCFMT_LEFT;
1.1.1.5 ! root     1129:                        SendMessage (hList,LVM_INSERTCOLUMN,1,(LPARAM)&LvCol);
1.1       root     1130: 
                   1131:                        LvCol.pszText = "Size";  
1.1.1.5 ! root     1132:                        LvCol.cx = 62;           
1.1       root     1133:                        LvCol.fmt = LVCFMT_RIGHT;
1.1.1.5 ! root     1134:                        SendMessage (hList,LVM_INSERTCOLUMN,2,(LPARAM)&LvCol);
1.1       root     1135: 
                   1136:                        LvCol.pszText = "Type";  
1.1.1.5 ! root     1137:                        LvCol.cx = 112;
1.1       root     1138:                        LvCol.fmt = LVCFMT_LEFT;
1.1.1.5 ! root     1139:                        SendMessage (hList,LVM_INSERTCOLUMN,3,(LPARAM)&LvCol);
1.1       root     1140: 
                   1141:                        nCount = GetAvailableFixedDisks (hList, "\\Device\\Harddisk%d\\Partition%d");
                   1142:                        nCount += GetAvailableRemovables (hList, "\\Device\\Floppy%d");
                   1143: 
                   1144:                        if (nCount == 0)
                   1145:                        {
                   1146:                                handleWin32Error (hwndDlg);
                   1147:                                MessageBox (hwndDlg, getstr (IDS_RAWDEVICES), lpszTitle, ICON_HAND);
                   1148:                                EndDialog (hwndDlg, IDCANCEL);
                   1149:                        }
                   1150: 
                   1151:                        lpszFileName = (char *) lParam;
                   1152:                        return 1;
                   1153:                }
                   1154: 
                   1155:        case WM_COMMAND:
                   1156:        case WM_NOTIFY:
                   1157:                // catch non-device line selected
                   1158:                if (msg == WM_NOTIFY && ((LPNMHDR) lParam)->code == LVN_ITEMCHANGED && (((LPNMLISTVIEW) lParam)->uNewState & LVIS_FOCUSED ))
                   1159:                {
                   1160:                        LVITEM LvItem;
                   1161:                        memset(&LvItem,0,sizeof(LvItem));
                   1162:                        LvItem.mask = LVIF_TEXT;   
                   1163:                        LvItem.iItem = ((LPNMLISTVIEW) lParam)->iItem;
                   1164:                        LvItem.pszText = lpszFileName;
                   1165:                        LvItem.cchTextMax = TC_MAX_PATH;
                   1166: 
                   1167:                        SendMessage (GetDlgItem (hwndDlg, IDC_DEVICELIST), LVM_GETITEMTEXT, LvItem.iItem, (LPARAM) &LvItem);
                   1168:                        EnableWindow (GetDlgItem ((HWND) hwndDlg, IDOK), lpszFileName[0] != 0 && lpszFileName[0] != ' ');
                   1169:                        return 1;
                   1170:                }
                   1171: 
                   1172:                if (msg == WM_COMMAND && lw == IDOK || msg == WM_NOTIFY && ((NMHDR *)lParam)->code == LVN_ITEMACTIVATE)
                   1173:                {
                   1174:                        LVITEM LvItem;
1.1.1.5 ! root     1175:                        memset (&LvItem,0,sizeof(LvItem));
1.1       root     1176:                        LvItem.mask = LVIF_TEXT;   
                   1177:                        LvItem.iItem =  SendMessage (GetDlgItem (hwndDlg, IDC_DEVICELIST), LVM_GETSELECTIONMARK, 0, 0);
                   1178:                        LvItem.pszText = lpszFileName;
                   1179:                        LvItem.cchTextMax = TC_MAX_PATH;
                   1180: 
                   1181:                        SendMessage (GetDlgItem (hwndDlg, IDC_DEVICELIST), LVM_GETITEMTEXT, LvItem.iItem, (LPARAM) &LvItem);
                   1182: 
1.1.1.5 ! root     1183:                        if (lpszFileName[0] == 'H')
        !          1184:                        {
        !          1185:                                // Whole device selected
        !          1186:                                int driveNo;
        !          1187:                                char tmp[10];
        !          1188: 
        !          1189:                                sscanf (lpszFileName, "Harddisk %d", &driveNo);
        !          1190:                                sprintf (lpszFileName, "\\Device\\Harddisk%d\\Partition0", driveNo);
        !          1191: 
        !          1192: #ifdef VOLFORMAT
        !          1193:                                // Warn if device contains partitions
        !          1194:                                LvItem.iSubItem = 3;
        !          1195:                                LvItem.pszText = tmp;
        !          1196:                                SendMessage (GetDlgItem (hwndDlg, IDC_DEVICELIST), LVM_GETITEMTEXT, LvItem.iItem, (LPARAM) &LvItem);
        !          1197:                                if (tmp[0] == ' ')
        !          1198:                                {
        !          1199:                                        MessageBox (hwndDlg, getstr (IDS_DEVICE_PARTITIONS_WARN), lpszTitle, MB_OK|MB_ICONWARNING);
        !          1200:                                }
        !          1201: #endif
        !          1202:                        }
        !          1203: 
        !          1204:                        if (lpszFileName[0] == 0)
1.1       root     1205:                                break; // non-device line selected
                   1206: 
                   1207:                        EndDialog (hwndDlg, IDOK);
                   1208:                        return 0;
                   1209:                }
                   1210: 
                   1211:                if (lw == IDCANCEL)
                   1212:                {
                   1213:                        EndDialog (hwndDlg, IDCANCEL);
                   1214:                        return 0;
                   1215:                }
                   1216:                return 0;
                   1217:        }
                   1218: 
                   1219:        return 0;
                   1220: }
                   1221: 
                   1222: int
                   1223: DriverAttach (void)
                   1224: {
                   1225:        /* Try to open a handle to the device driver. It will be closed
                   1226:           later. */
                   1227: 
                   1228:        if (nCurrentOS == WIN_NT)
                   1229:                hDriver = CreateFile (WIN32_ROOT_PREFIX, 0, 0, NULL, OPEN_EXISTING, 0, NULL);
                   1230:        else
                   1231:                hDriver = CreateFile (WIN9X_DRIVER_NAME, 0, 0, NULL, OPEN_EXISTING, 0, NULL);
                   1232: 
                   1233:        if (hDriver == INVALID_HANDLE_VALUE)
                   1234:        {
                   1235:                return ERR_OS_ERROR;
                   1236:        }
                   1237: #ifndef SETUP // Don't check version during setup to allow removal of older version
                   1238:        else
                   1239:        {
                   1240:                LONG driver = 0;
                   1241:                DWORD dwResult;
                   1242: 
                   1243:                BOOL bResult = DeviceIoControl (hDriver, DRIVER_VERSION,
                   1244:                                   &driver, 4, &driver, 4, &dwResult, NULL);
                   1245: 
                   1246:                if (bResult == FALSE)
                   1247:                        return ERR_OS_ERROR;
                   1248:                else if (driver != VERSION_NUM)
                   1249:                        return ERR_DRIVER_VERSION;
                   1250:        }
                   1251: #endif
                   1252: 
                   1253:        if (nCurrentOS == WIN_98)
                   1254:        {
                   1255:                DWORD dwResult;
                   1256:                DeviceIoControl (hDriver, ALLOW_FAST_SHUTDOWN, NULL, 0, NULL, 0, &dwResult, NULL);
                   1257:        }
                   1258: 
                   1259:        return 0;
                   1260: }
                   1261: 
1.1.1.5 ! root     1262: 
        !          1263: // Sets file pointer to hidden volume header
        !          1264: BOOL SeekHiddenVolHeader (HFILE dev, unsigned __int64 volSize, BOOL deviceFlag)
        !          1265: {
        !          1266:        LARGE_INTEGER offset, offsetNew;
        !          1267: 
        !          1268:        if (deviceFlag)
        !          1269:        {
        !          1270:                // Partition/device
        !          1271: 
        !          1272:                offset.QuadPart = volSize - HIDDEN_VOL_HEADER_OFFSET;
        !          1273: 
        !          1274:                if (SetFilePointerEx ((HANDLE) dev, offset, &offsetNew, FILE_BEGIN) == 0)
        !          1275:                        return FALSE;
        !          1276: 
        !          1277:                if (offsetNew.QuadPart != offset.QuadPart)
        !          1278:                        return FALSE;
        !          1279:        }
        !          1280:        else
        !          1281:        {
        !          1282:                // File-hosted volume
        !          1283: 
        !          1284:                offset.QuadPart = - HIDDEN_VOL_HEADER_OFFSET;
        !          1285: 
        !          1286:                if (SetFilePointerEx ((HANDLE) dev, offset, &offsetNew, FILE_END) == 0)
        !          1287:                        return FALSE;
        !          1288:        }
        !          1289: 
        !          1290:        return TRUE;
        !          1291: }
        !          1292: 
        !          1293: 
1.1       root     1294: BOOL
1.1.1.5 ! root     1295: BrowseFiles (HWND hwndDlg, UINT nTitleID, char *lpszFileName, BOOL keepHistory)
1.1       root     1296: {
                   1297:        OPENFILENAME ofn;
                   1298:        char szFileTitle[TC_MAX_PATH];
                   1299:        ZeroMemory (&ofn, sizeof (OPENFILENAME));
                   1300: 
                   1301:        *szFileTitle = *lpszFileName = 0;
                   1302:        ofn.lStructSize = OPENFILENAME_SIZE_VERSION_400; //sizeof (OPENFILENAME);
                   1303:        ofn.hwndOwner = hwndDlg;
                   1304:        ofn.lpstrFilter = "All Files (*.*)\0*.*\0TrueCrypt Volumes (*.tc)\0*.tc\0";
                   1305:        ofn.lpstrCustomFilter = NULL;
                   1306:        ofn.nFilterIndex = 1;
                   1307:        ofn.lpstrFile = lpszFileName;
                   1308:        ofn.nMaxFile = TC_MAX_PATH;
                   1309:        ofn.lpstrFileTitle = szFileTitle;
                   1310:        ofn.nMaxFileTitle = TC_MAX_PATH;
                   1311:        ofn.lpstrInitialDir = NULL;
                   1312:        ofn.lpstrTitle = getstr (nTitleID);
1.1.1.5 ! root     1313:        ofn.Flags = OFN_HIDEREADONLY | OFN_PATHMUSTEXIST | (keepHistory ? 0 : OFN_DONTADDTORECENT);
1.1       root     1314:        //ofn.lpstrDefExt = "tc";
                   1315: 
                   1316:        if (!GetOpenFileName (&ofn))
                   1317:                return FALSE;
                   1318:        else
                   1319:                return TRUE;
                   1320: }
                   1321: 
                   1322: 
                   1323: void
                   1324: handleError (HWND hwndDlg, int code)
                   1325: {
                   1326:        char szTmp[512];
                   1327: 
                   1328:        switch (code)
                   1329:        {
                   1330:        case ERR_OS_ERROR:
                   1331:                handleWin32Error (hwndDlg);
                   1332:                break;
                   1333:        case ERR_OUTOFMEMORY:
                   1334:                MessageBox (hwndDlg, getstr (IDS_OUTOFMEMORY), lpszTitle, ICON_HAND);
                   1335:                break;
                   1336:        case ERR_PASSWORD_WRONG:
1.1.1.5 ! root     1337:                MessageBox (hwndDlg, getstr (CheckCapsLock (NULL, TRUE) ? IDS_PASSWORD_WRONG_CAPSLOCK_ON : IDS_PASSWORD_WRONG), lpszTitle, MB_ICONEXCLAMATION);
1.1       root     1338:                break;
                   1339:        case ERR_VOL_FORMAT_BAD:
                   1340:                MessageBox (hwndDlg, getstr (IDS_VOL_FORMAT_BAD), lpszTitle, ICON_HAND);
                   1341:                break;
                   1342:        case ERR_BAD_DRIVE_LETTER:
                   1343:                MessageBox (hwndDlg, getstr (IDS_BAD_DRIVE_LETTER), lpszTitle, ICON_HAND);
                   1344:                break;
                   1345:        case ERR_DRIVE_NOT_FOUND:
                   1346:                MessageBox (hwndDlg, getstr (IDS_NOT_FOUND), lpszTitle, ICON_HAND);
                   1347:                break;
                   1348:        case ERR_FILES_OPEN:
                   1349:                MessageBox (hwndDlg, getstr (IDS_OPENFILES_DRIVER), lpszTitle, ICON_HAND);
                   1350:                break;
                   1351:        case ERR_FILES_OPEN_LOCK:
                   1352:                MessageBox (hwndDlg, getstr (IDS_OPENFILES_LOCK), lpszTitle, ICON_HAND);
                   1353:                break;
                   1354:        case ERR_VOL_SIZE_WRONG:
                   1355:                MessageBox (hwndDlg, getstr (IDS_VOL_SIZE_WRONG), lpszTitle, ICON_HAND);
                   1356:                break;
                   1357:        case ERR_COMPRESSION_NOT_SUPPORTED:
                   1358:                MessageBox (hwndDlg, getstr (IDS_COMPRESSION_NOT_SUPPORTED), lpszTitle, ICON_HAND);
                   1359:                break;
                   1360:        case ERR_PASSWORD_CHANGE_VOL_TYPE:
                   1361:                MessageBox (hwndDlg, getstr (IDS_WRONG_VOL_TYPE), lpszTitle, ICON_HAND);
                   1362:                break;
                   1363:        case ERR_PASSWORD_CHANGE_VOL_VERSION:
                   1364:                MessageBox (hwndDlg, getstr (IDS_WRONG_VOL_VERSION), lpszTitle, ICON_HAND);
                   1365:                break;
                   1366:        case ERR_VOL_SEEKING:
                   1367:                MessageBox (hwndDlg, getstr (IDS_VOL_SEEKING), lpszTitle, ICON_HAND);
                   1368:                break;
                   1369:        case ERR_VOL_WRITING:
                   1370:                MessageBox (hwndDlg, getstr (IDS_VOL_WRITING), lpszTitle, ICON_HAND);
                   1371:                break;
                   1372:        case ERR_VOL_READING:
                   1373:                MessageBox (hwndDlg, getstr (IDS_VOL_READING), lpszTitle, ICON_HAND);
                   1374:                break;
                   1375:        case ERR_VOL_ALREADY_MOUNTED:
                   1376:                MessageBox (hwndDlg, getstr (IDS_VOL_ALREADY_MOUNTED), lpszTitle, ICON_HAND);
                   1377:                break;
                   1378:        case ERR_FILE_OPEN_FAILED:
                   1379:                MessageBox (hwndDlg, getstr (IDS_FILE_OPEN_FAILED), lpszTitle, ICON_HAND);
                   1380:                break;
                   1381:        case ERR_VOL_MOUNT_FAILED:
1.1.1.5 ! root     1382:                MessageBox (hwndDlg, getstr (CheckCapsLock (NULL, TRUE) ? IDS_VOL_MOUNT_FAILED_CAPSLOCK_ON : IDS_VOL_MOUNT_FAILED), lpszTitle, MB_ICONEXCLAMATION);
1.1       root     1383:                break;
                   1384:        case ERR_NO_FREE_SLOTS:
                   1385:                MessageBox (hwndDlg, getstr (IDS_NO_FREE_SLOTS), lpszTitle, ICON_HAND);
                   1386:                break;
                   1387:        case ERR_NO_FREE_DRIVES:
                   1388:                MessageBox (hwndDlg, getstr (IDS_NO_FREE_DRIVES), lpszTitle, ICON_HAND);
                   1389:                break;
                   1390:        case ERR_INVALID_DEVICE:
                   1391:                MessageBox (hwndDlg, getstr (IDS_INVALID_DEVICE), lpszTitle, ICON_HAND);
                   1392:                break;
                   1393:        case ERR_ACCESS_DENIED:
                   1394:                MessageBox (hwndDlg, getstr (IDS_ACCESS_DENIED), lpszTitle, ICON_HAND);
                   1395:                break;
                   1396: 
                   1397:        case ERR_DRIVER_VERSION:
                   1398:                sprintf (szTmp, getstr (IDS_DRIVER_VERSION), VERSION_STRING);
                   1399:                MessageBox (hwndDlg, szTmp, lpszTitle, ICON_HAND);
                   1400:                break;
                   1401: 
                   1402:        case ERR_NEW_VERSION_REQUIRED:
                   1403:                MessageBox (hwndDlg, getstr (IDS_NEW_VERSION_REQUIRED), lpszTitle, ICON_HAND);
                   1404:                break;
                   1405: 
                   1406:        default:
                   1407:                sprintf (szTmp, getstr (IDS_UNKNOWN), code);
                   1408:                MessageBox (hwndDlg, szTmp, lpszTitle, ICON_HAND);
                   1409: 
                   1410:        }
                   1411: }
                   1412: 
                   1413: static BOOL CALLBACK SetDefaultUserFontEnum( HWND hwnd, LPARAM font)
                   1414: {
                   1415:        SendMessage (hwnd, WM_SETFONT, (WPARAM) font, 0);
                   1416:        return TRUE;
                   1417: }
                   1418: 
                   1419: void SetDefaultUserFont (HWND hwnd)
                   1420: {
                   1421:        NONCLIENTMETRICS metric;
                   1422: 
                   1423:        if (hUserFont == 0)
                   1424:        {
                   1425:                metric.cbSize = sizeof (NONCLIENTMETRICS);
                   1426:                SystemParametersInfo (SPI_GETNONCLIENTMETRICS, sizeof(NONCLIENTMETRICS), &metric, 0);
                   1427: 
1.1.1.5 ! root     1428:                metric.lfMessageFont.lfHeight = -11;
        !          1429:                metric.lfMessageFont.lfWidth = 0;
        !          1430: 
1.1       root     1431:                hUserFont = CreateFontIndirect (&metric.lfMessageFont);
                   1432: 
                   1433:                metric.lfMessageFont.lfUnderline = TRUE;
                   1434:                hUserUnderlineFont = CreateFontIndirect (&metric.lfMessageFont);
                   1435: 
                   1436:                metric.lfMessageFont.lfUnderline = FALSE;
                   1437:                metric.lfMessageFont.lfWeight = FW_BOLD;
                   1438:                hUserBoldFont = CreateFontIndirect (&metric.lfMessageFont);
1.1.1.5 ! root     1439: 
        !          1440:                metric.lfMessageFont.lfUnderline = TRUE;
        !          1441:                metric.lfMessageFont.lfWeight = FW_BOLD;
        !          1442:                hUserUnderlineBoldFont = CreateFontIndirect (&metric.lfMessageFont);
1.1       root     1443:        }
                   1444: 
                   1445:        EnumChildWindows (hwnd, SetDefaultUserFontEnum, (LPARAM) hUserFont);
1.1.1.5 ! root     1446: }
        !          1447: 
        !          1448: void OpenVolumeExplorerWindow (int driveNo)
        !          1449: {
        !          1450:        char dosName[5];
        !          1451:        SHFILEINFO fInfo;
        !          1452: 
        !          1453:        sprintf (dosName, "%c:\\", (char) driveNo + 'A');
        !          1454: 
        !          1455:        // Force explorer to discover the drive
        !          1456:        SHGetFileInfo (dosName, 0, &fInfo, sizeof (fInfo), 0);
        !          1457: 
        !          1458:        ShellExecute (NULL, "open", dosName, NULL, NULL, SW_SHOWNORMAL);
        !          1459: }
        !          1460: 
        !          1461: static BOOL explorerCloseSent;
        !          1462: 
        !          1463: static BOOL CALLBACK CloseVolumeExplorerWindowsEnum( HWND hwnd, LPARAM driveNo)
        !          1464: {
        !          1465:        char get[128], driveStr[10];
        !          1466:        HWND h;
        !          1467: 
        !          1468:        GetClassName (hwnd, get, sizeof get);
        !          1469: 
        !          1470:        if (strcmp (get, "CabinetWClass") == 0)
        !          1471:        {
        !          1472:                sprintf (driveStr, "%c:\\", driveNo + 'A');
        !          1473: 
        !          1474:                // Title bar
        !          1475:                GetWindowText (hwnd, get, sizeof get);
        !          1476:                if (strstr (get, driveStr) == get)
        !          1477:                {
        !          1478:                        PostMessage (hwnd, WM_CLOSE, 0, 0);
        !          1479:                        explorerCloseSent = TRUE;
        !          1480:                        return TRUE;
        !          1481:                }
        !          1482: 
        !          1483:                // URL edit box
        !          1484:                h = FindWindowEx (hwnd, 0, "WorkerW", 0);
        !          1485:                if (!h) return TRUE;
        !          1486:                h = FindWindowEx (h, 0, "ReBarWindow32", 0);
        !          1487:                if (!h) return TRUE;
        !          1488:                h = FindWindowEx (h, 0, "ComboBoxEx32", 0);
        !          1489:                if (h)
        !          1490:                {
        !          1491:                        SendMessage (h, WM_GETTEXT, sizeof get, (LPARAM) get);
        !          1492:                        if (strstr (get, driveStr) == get)
        !          1493:                        {
        !          1494:                                PostMessage (hwnd, WM_CLOSE, 0, 0);
        !          1495:                                explorerCloseSent = TRUE;
        !          1496:                        }
        !          1497:                }
        !          1498:        }
        !          1499:        return TRUE;
        !          1500: }
        !          1501: 
        !          1502: BOOL CloseVolumeExplorerWindows (HWND hwnd, int driveNo)
        !          1503: {
        !          1504:        explorerCloseSent = FALSE;
        !          1505:        EnumWindows (CloseVolumeExplorerWindowsEnum, (LPARAM) driveNo);
        !          1506: 
        !          1507:        return explorerCloseSent;
        !          1508: }
        !          1509: 
        !          1510: 
        !          1511: #ifndef SETUP
        !          1512: void GetSpeedString (unsigned __int64 speed, char *str)
        !          1513: {
        !          1514:        if (speed > 1024I64*1024*1024*1024*1024*99)
        !          1515:                sprintf (str,"%d PB/s", speed/1024/1024/1024/1024/1024);
        !          1516:        else if (speed > 1024I64*1024*1024*1024*1024)
        !          1517:                sprintf (str,"%.1f PB/s",(double)(speed/1024.0/1024/1024/1024/1024));
        !          1518:        else if (speed > 1024I64*1024*1024*1024*99)
        !          1519:                sprintf (str,"%d TB/s",speed/1024/1024/1024/1024);
        !          1520:        else if (speed > 1024I64*1024*1024*1024)
        !          1521:                sprintf (str,"%.1f TB/s",(double)(speed/1024.0/1024/1024/1024));
        !          1522:        else if (speed > 1024I64*1024*1024*99)
        !          1523:                sprintf (str,"%d GB/s",speed/1024/1024/1024);
        !          1524:        else if (speed > 1024I64*1024*1024)
        !          1525:                sprintf (str,"%.1f GB/s",(double)(speed/1024.0/1024/1024));
        !          1526:        else if (speed > 1024I64*1024*99)
        !          1527:                sprintf (str,"%d MB/s", speed/1024/1024);
        !          1528:        else if (speed > 1024I64*1024)
        !          1529:                sprintf (str,"%.1f MB/s",(double)(speed/1024.0/1024));
        !          1530:        else
        !          1531:                sprintf (str,"%d KB/s", speed/1024);
        !          1532: }
        !          1533: 
        !          1534: static void DisplayBenchmarkResults (HWND hwndDlg)
        !          1535: {
        !          1536:        BENCHMARK_REC tmp_line;
        !          1537:        char item1[100]={0};
        !          1538:        int line = 0;
        !          1539:        LVITEM LvItem;
        !          1540:        HWND hList = GetDlgItem (hwndDlg, IDC_RESULTS);
        !          1541:        int ea, i;
        !          1542:        BOOL unsorted = TRUE;
        !          1543:        unsigned __int64 encBytesPerSec;
        !          1544:        unsigned __int64 decBytesPerSec;
        !          1545: 
        !          1546:        /* Sort the list */
        !          1547: 
        !          1548:        switch (benchmarkSortMethod)
        !          1549:        {
        !          1550:        case BENCHMARK_SORT_BY_SPEED:
        !          1551: 
        !          1552:                while (unsorted)
        !          1553:                {
        !          1554:                        unsorted = FALSE;
        !          1555:                        for (i = 0; i < benchmarkTotalItems - 1; i++)
        !          1556:                        {
        !          1557:                                if (benchmarkTable[i].meanBytesPerSec < benchmarkTable[i+1].meanBytesPerSec)
        !          1558:                                {
        !          1559:                                        unsorted = TRUE;
        !          1560:                                        memcpy (&tmp_line, &benchmarkTable[i], sizeof(BENCHMARK_REC));
        !          1561:                                        memcpy (&benchmarkTable[i], &benchmarkTable[i+1], sizeof(BENCHMARK_REC));
        !          1562:                                        memcpy (&benchmarkTable[i+1], &tmp_line, sizeof(BENCHMARK_REC));
        !          1563:                                }
        !          1564:                        }
        !          1565:                }
        !          1566:                break;
        !          1567: 
        !          1568:        case BENCHMARK_SORT_BY_NAME:
        !          1569: 
        !          1570:                while (unsorted)
        !          1571:                {
        !          1572:                        unsorted = FALSE;
        !          1573:                        for (i = 0; i < benchmarkTotalItems - 1; i++)
        !          1574:                        {
        !          1575:                                if (benchmarkTable[i].id > benchmarkTable[i+1].id)
        !          1576:                                {
        !          1577:                                        unsorted = TRUE;
        !          1578:                                        memcpy (&tmp_line, &benchmarkTable[i], sizeof(BENCHMARK_REC));
        !          1579:                                        memcpy (&benchmarkTable[i], &benchmarkTable[i+1], sizeof(BENCHMARK_REC));
        !          1580:                                        memcpy (&benchmarkTable[i+1], &tmp_line, sizeof(BENCHMARK_REC));
        !          1581:                                }
        !          1582:                        }
        !          1583:                }
        !          1584:                break;
        !          1585:        }
        !          1586:   
        !          1587:        /* Render the results */
        !          1588: 
        !          1589:        SendMessage (hList,LVM_DELETEALLITEMS,0,(LPARAM)&LvItem);
        !          1590: 
        !          1591:        for (i = 0; i < benchmarkTotalItems; i++)
        !          1592:        {
        !          1593:                ea = benchmarkTable[i].id;
        !          1594: 
        !          1595:                memset (&LvItem,0,sizeof(LvItem));
        !          1596:                LvItem.mask = LVIF_TEXT;
        !          1597:                LvItem.iItem = line++;
        !          1598:                LvItem.iSubItem = 0;
        !          1599:                LvItem.pszText = benchmarkTable[i].name;
        !          1600: 
        !          1601:                SendMessage (hList,LVM_INSERTITEM,0,(LPARAM)&LvItem);
        !          1602: 
        !          1603:                GetSpeedString((unsigned __int64) (benchmarkLastBufferSize / ((float) benchmarkTable[i].encSpeed / benchmarkPerformanceFrequency.QuadPart)), item1);
        !          1604:                LvItem.iSubItem = 1;
        !          1605:                LvItem.pszText = item1;
        !          1606: 
        !          1607:                SendMessage (hList,LVM_SETITEM,0,(LPARAM)&LvItem); 
        !          1608:                GetSpeedString((unsigned __int64) (benchmarkLastBufferSize / ((float) benchmarkTable[i].decSpeed / benchmarkPerformanceFrequency.QuadPart)), item1);
        !          1609:                LvItem.iSubItem = 2;
        !          1610:                LvItem.pszText = item1;
        !          1611: 
        !          1612:                SendMessage (hList,LVM_SETITEM,0,(LPARAM)&LvItem); 
        !          1613: 
        !          1614:                GetSpeedString(benchmarkTable[i].meanBytesPerSec, item1);
        !          1615:                LvItem.iSubItem = 3;
        !          1616:                LvItem.pszText = item1;
        !          1617: 
        !          1618:                SendMessage (hList,LVM_SETITEM,0,(LPARAM)&LvItem); 
        !          1619:        }
        !          1620: }
        !          1621: 
        !          1622: static BOOL PerformBenchmark(HWND hwndDlg)
        !          1623: {
        !          1624:     LARGE_INTEGER performanceCountStart, performanceCountEnd;
        !          1625:        BYTE *lpTestBuffer;
        !          1626:        HWND hList = GetDlgItem (hwndDlg, IDC_RESULTS);
        !          1627:        LVITEM LvItem;
        !          1628:        int ea, i;
        !          1629:        unsigned char iv[DISK_IV_SIZE];
        !          1630:        unsigned char ks[MAX_EXPANDED_KEY];
        !          1631:        unsigned char key[DISKKEY_SIZE];
        !          1632: 
        !          1633:        if (QueryPerformanceFrequency (&benchmarkPerformanceFrequency) == 0)
        !          1634:        {
        !          1635:                MessageBox (hwndDlg, "Error: Your hardware does not support a high-resolution performance counter.", lpszTitle, ICON_HAND);
        !          1636:                return FALSE;
        !          1637:        }
        !          1638: 
        !          1639:        ArrowWaitCursor ();
        !          1640: 
        !          1641:        lpTestBuffer = malloc(benchmarkBufferSize - (benchmarkBufferSize % 16));
        !          1642:        if (lpTestBuffer == NULL)
        !          1643:        {
        !          1644:                NormalCursor ();
        !          1645:                MessageBox (hwndDlg, "Error: Cannot allocate memory.", lpszTitle, ICON_HAND);
        !          1646:                return FALSE;
        !          1647:        }
        !          1648:        VirtualLock (lpTestBuffer, benchmarkBufferSize - (benchmarkBufferSize % 16));
        !          1649: 
        !          1650:        benchmarkTotalItems = 0;
        !          1651:        
        !          1652:        for (ea = EAGetFirst(); ea != 0; ea = EAGetNext(ea))
        !          1653:        {
        !          1654:                EAInit (ea, key, ks);
        !          1655:         
        !          1656:                if (QueryPerformanceCounter (&performanceCountStart) == 0)
        !          1657:                        goto counter_error;
        !          1658: 
        !          1659:                EncryptBuffer ((unsigned long *) lpTestBuffer, (unsigned __int64) benchmarkBufferSize, ks, iv, iv, ea);
        !          1660: 
        !          1661:                if (QueryPerformanceCounter (&performanceCountEnd) == 0)
        !          1662:                        goto counter_error;
        !          1663: 
        !          1664:                benchmarkTable[benchmarkTotalItems].encSpeed = performanceCountEnd.QuadPart - performanceCountStart.QuadPart;
        !          1665: 
        !          1666:                if (QueryPerformanceCounter (&performanceCountStart) == 0)
        !          1667:                        goto counter_error;
        !          1668: 
        !          1669:                DecryptBuffer ((unsigned long *) lpTestBuffer, (unsigned __int64) benchmarkBufferSize, ks, iv, iv, ea);
        !          1670:                
        !          1671:                if (QueryPerformanceCounter (&performanceCountEnd) == 0)
        !          1672:                        goto counter_error;
        !          1673: 
        !          1674:                benchmarkTable[benchmarkTotalItems].decSpeed = performanceCountEnd.QuadPart - performanceCountStart.QuadPart;
        !          1675:                benchmarkTable[benchmarkTotalItems].id = ea;
        !          1676:                benchmarkTable[benchmarkTotalItems].meanBytesPerSec = ((unsigned __int64) (benchmarkBufferSize / ((float) benchmarkTable[benchmarkTotalItems].encSpeed / benchmarkPerformanceFrequency.QuadPart)) + (unsigned __int64) (benchmarkBufferSize / ((float) benchmarkTable[benchmarkTotalItems].decSpeed / benchmarkPerformanceFrequency.QuadPart))) / 2;
        !          1677:                EAGetName (benchmarkTable[benchmarkTotalItems].name, ea);
        !          1678: 
        !          1679:                benchmarkTotalItems++;
        !          1680: 
        !          1681:        }
        !          1682: 
        !          1683:        VirtualUnlock (lpTestBuffer, benchmarkBufferSize - (benchmarkBufferSize % 16));
        !          1684: 
        !          1685:        free(lpTestBuffer);
        !          1686: 
        !          1687:        benchmarkLastBufferSize = benchmarkBufferSize;
        !          1688: 
        !          1689:        DisplayBenchmarkResults(hwndDlg);
        !          1690: 
        !          1691:        EnableWindow (GetDlgItem (hwndDlg, ID_PERFORM_BENCHMARK), TRUE);
        !          1692:        EnableWindow (GetDlgItem (hwndDlg, IDCLOSE), TRUE);
        !          1693: 
        !          1694:        NormalCursor ();
        !          1695:        return TRUE;
        !          1696: 
        !          1697: counter_error:
        !          1698: 
        !          1699:        VirtualUnlock (lpTestBuffer, benchmarkBufferSize - (benchmarkBufferSize % 16));
        !          1700: 
        !          1701:        free(lpTestBuffer);
        !          1702: 
        !          1703:        NormalCursor ();
        !          1704: 
        !          1705:        EnableWindow (GetDlgItem (hwndDlg, ID_PERFORM_BENCHMARK), TRUE);
        !          1706:        EnableWindow (GetDlgItem (hwndDlg, IDCLOSE), TRUE);
        !          1707: 
        !          1708:        MessageBox (hwndDlg, "Error: Could not retrieve value of performance counter.", lpszTitle, ICON_HAND);
        !          1709:        return FALSE;
        !          1710: }
        !          1711: 
        !          1712: 
        !          1713: BOOL WINAPI BenchmarkDlgProc (HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam)
        !          1714: {
        !          1715:        WORD lw = LOWORD (wParam);
        !          1716:        LPARAM nIndex;
        !          1717:        HWND hCboxSortMethod = GetDlgItem (hwndDlg, IDC_BENCHMARK_SORT_METHOD);
        !          1718:        HWND hCboxBufferSize = GetDlgItem (hwndDlg, IDC_BENCHMARK_BUFFER_SIZE);
        !          1719: 
        !          1720:        switch (msg)
        !          1721:        {
        !          1722:        case WM_INITDIALOG:
        !          1723:                {
        !          1724:                        LVCOLUMN LvCol;
        !          1725:                        HWND hList = GetDlgItem (hwndDlg, IDC_RESULTS);
        !          1726:                        char buf[100];
        !          1727: 
        !          1728:                        SetDefaultUserFont (hwndDlg);
        !          1729: 
        !          1730:                        benchmarkBufferSize = BENCHMARK_DEFAULT_BUF_SIZE;
        !          1731:                        benchmarkSortMethod = BENCHMARK_SORT_BY_SPEED;
        !          1732: 
        !          1733:                        SendMessage (hList,LVM_SETEXTENDEDLISTVIEWSTYLE,0,
        !          1734:                                LVS_EX_FULLROWSELECT|LVS_EX_HEADERDRAGDROP|LVS_EX_TWOCLICKACTIVATE 
        !          1735:                                ); 
        !          1736: 
        !          1737:                        memset (&LvCol,0,sizeof(LvCol));               
        !          1738:                        LvCol.mask = LVCF_TEXT|LVCF_WIDTH|LVCF_SUBITEM|LVCF_FMT;  
        !          1739:                        LvCol.pszText = "Algorithm";                           
        !          1740:                        LvCol.cx =114;
        !          1741:                        LvCol.fmt = LVCFMT_LEFT;
        !          1742:                        SendMessage (hList,LVM_INSERTCOLUMN,0,(LPARAM)&LvCol);
        !          1743: 
        !          1744:                        LvCol.pszText = "Encryption";  
        !          1745:                        LvCol.cx = 80;           
        !          1746:                        LvCol.fmt = LVCFMT_RIGHT;
        !          1747:                        SendMessage (hList,LVM_INSERTCOLUMN,1,(LPARAM)&LvCol);
        !          1748: 
        !          1749:                        LvCol.pszText = "Decryption";  
        !          1750:                        LvCol.cx = 80;
        !          1751:                        LvCol.fmt = LVCFMT_RIGHT;
        !          1752:                        SendMessage (hList,LVM_INSERTCOLUMN,2,(LPARAM)&LvCol);
        !          1753: 
        !          1754:                        LvCol.pszText = "Mean";  
        !          1755:                        LvCol.cx = 80;
        !          1756:                        LvCol.fmt = LVCFMT_RIGHT;
        !          1757:                        SendMessage (hList,LVM_INSERTCOLUMN,3,(LPARAM)&LvCol);
        !          1758: 
        !          1759:                        /* Combo boxes */
        !          1760: 
        !          1761:                        // Sort method
        !          1762: 
        !          1763:                        SendMessage (hCboxSortMethod, CB_RESETCONTENT, 0, 0);
        !          1764: 
        !          1765:                        nIndex = SendMessage (hCboxSortMethod, CB_ADDSTRING, 0, (LPARAM) "Alphabetical/Categorized");
        !          1766:                        SendMessage (hCboxSortMethod, CB_SETITEMDATA, nIndex, (LPARAM) 0);
        !          1767: 
        !          1768:                        nIndex = SendMessage (hCboxSortMethod, CB_ADDSTRING, 0, (LPARAM) "Mean Speed (Descending)");
        !          1769:                        SendMessage (hCboxSortMethod, CB_SETITEMDATA, nIndex, (LPARAM) 0);
        !          1770: 
        !          1771:                        SendMessage (hCboxSortMethod, CB_SETCURSEL, 1, 0);              // Default sort method
        !          1772: 
        !          1773:                        // Buffer size
        !          1774: 
        !          1775:                        SendMessage (hCboxBufferSize, CB_RESETCONTENT, 0, 0);
        !          1776: 
        !          1777:                        nIndex = SendMessage (hCboxBufferSize, CB_ADDSTRING, 0, (LPARAM) "5 KB");
        !          1778:                        SendMessage (hCboxBufferSize, CB_SETITEMDATA, nIndex, (LPARAM) 5 * BYTES_PER_KB);
        !          1779: 
        !          1780:                        nIndex = SendMessage (hCboxBufferSize, CB_ADDSTRING, 0, (LPARAM) "100 KB");
        !          1781:                        SendMessage (hCboxBufferSize, CB_SETITEMDATA, nIndex, (LPARAM) 100 * BYTES_PER_KB);
        !          1782: 
        !          1783:                        nIndex = SendMessage (hCboxBufferSize, CB_ADDSTRING, 0, (LPARAM) "500 KB");
        !          1784:                        SendMessage (hCboxBufferSize, CB_SETITEMDATA, nIndex, (LPARAM) 500 * BYTES_PER_KB);
        !          1785: 
        !          1786:                        nIndex = SendMessage (hCboxBufferSize, CB_ADDSTRING, 0, (LPARAM) "1 MB");
        !          1787:                        SendMessage (hCboxBufferSize, CB_SETITEMDATA, nIndex, (LPARAM) 1 * BYTES_PER_MB);
        !          1788: 
        !          1789:                        nIndex = SendMessage (hCboxBufferSize, CB_ADDSTRING, 0, (LPARAM) "5 MB");
        !          1790:                        SendMessage (hCboxBufferSize, CB_SETITEMDATA, nIndex, (LPARAM) 5 * BYTES_PER_MB);
        !          1791: 
        !          1792:                        nIndex = SendMessage (hCboxBufferSize, CB_ADDSTRING, 0, (LPARAM) "10 MB");
        !          1793:                        SendMessage (hCboxBufferSize, CB_SETITEMDATA, nIndex, (LPARAM) 10 * BYTES_PER_MB);
        !          1794: 
        !          1795:                        nIndex = SendMessage (hCboxBufferSize, CB_ADDSTRING, 0, (LPARAM) "50 MB");
        !          1796:                        SendMessage (hCboxBufferSize, CB_SETITEMDATA, nIndex, (LPARAM) 50 * BYTES_PER_MB);
        !          1797: 
        !          1798:                        nIndex = SendMessage (hCboxBufferSize, CB_ADDSTRING, 0, (LPARAM) "100 MB");
        !          1799:                        SendMessage (hCboxBufferSize, CB_SETITEMDATA, nIndex, (LPARAM) 100 * BYTES_PER_MB);
        !          1800: 
        !          1801:                        nIndex = SendMessage (hCboxBufferSize, CB_ADDSTRING, 0, (LPARAM) "200 MB");
        !          1802:                        SendMessage (hCboxBufferSize, CB_SETITEMDATA, nIndex, (LPARAM) 200 * BYTES_PER_MB);
        !          1803: 
        !          1804:                        nIndex = SendMessage (hCboxBufferSize, CB_ADDSTRING, 0, (LPARAM) "500 MB");
        !          1805:                        SendMessage (hCboxBufferSize, CB_SETITEMDATA, nIndex, (LPARAM) 500 * BYTES_PER_MB);
        !          1806: 
        !          1807:                        nIndex = SendMessage (hCboxBufferSize, CB_ADDSTRING, 0, (LPARAM) "1 GB");
        !          1808:                        SendMessage (hCboxBufferSize, CB_SETITEMDATA, nIndex, (LPARAM) 1 * BYTES_PER_GB);
        !          1809: 
        !          1810:                        SendMessage (hCboxBufferSize, CB_SETCURSEL, 3, 0);              // Default size
        !          1811: 
        !          1812:                        return 1;
        !          1813:                }
        !          1814:                break;
        !          1815: 
        !          1816:        case WM_COMMAND:
        !          1817:        case WM_NOTIFY:
        !          1818: 
        !          1819:                if (lw == IDC_BENCHMARK_SORT_METHOD)
        !          1820:                {
        !          1821:                        nIndex = SendMessage (hCboxSortMethod, CB_GETCURSEL, 0, 0);
        !          1822:                        if (nIndex != benchmarkSortMethod)
        !          1823:                        {
        !          1824:                                benchmarkSortMethod = nIndex;
        !          1825:                                DisplayBenchmarkResults (hwndDlg);
        !          1826:                        }
        !          1827:                }
        !          1828: 
        !          1829:                if (lw == ID_PERFORM_BENCHMARK)
        !          1830:                {
        !          1831:                        nIndex = SendMessage (hCboxBufferSize, CB_GETCURSEL, 0, 0);
        !          1832:                        benchmarkBufferSize = SendMessage (hCboxBufferSize, CB_GETITEMDATA, nIndex, 0);
        !          1833: 
        !          1834:                        if (PerformBenchmark(hwndDlg) == FALSE)
        !          1835:                        {
        !          1836:                                EndDialog (hwndDlg, IDCLOSE);
        !          1837:                        }
        !          1838:                        return 0;
        !          1839:                }
        !          1840:                if (lw == IDCLOSE)
        !          1841:                {
        !          1842:                        EndDialog (hwndDlg, IDCLOSE);
        !          1843:                        return 0;
        !          1844:                }
        !          1845:                return 0;
        !          1846: 
        !          1847:                break;
        !          1848: 
        !          1849:        case WM_CLOSE:
        !          1850:                EndDialog (hwndDlg, IDCLOSE);
        !          1851:                return 0;
        !          1852: 
        !          1853:                break;
        !          1854: 
        !          1855:        }
        !          1856: 
        !          1857:        return 0;
        !          1858: }
        !          1859: #endif // #ifndef SETUP
        !          1860: 
        !          1861: 
        !          1862: BOOL CheckCapsLock (HWND hwnd, BOOL quiet)
        !          1863: {
        !          1864:        if ((GetKeyState(VK_CAPITAL) & 1) != 0) 
        !          1865:        {
        !          1866:                if (!quiet)
        !          1867:                {
        !          1868:                        MessageBox (hwnd, getstr (IDS_CAPSLOCK_ON), lpszTitle, MB_ICONEXCLAMATION);
        !          1869:                }
        !          1870:                return TRUE;
        !          1871:        }
        !          1872:        return FALSE;
        !          1873: }
        !          1874: 
        !          1875: 
        !          1876: BOOL CheckPasswordLength (HWND hwndDlg, HWND hwndItem)                 
        !          1877: {
        !          1878:        if (GetWindowTextLength (hwndItem) < PASSWORD_LEN_WARNING)
        !          1879:        {
        !          1880:                if (MessageBox (hwndDlg, getstr (IDS_PASSWORD_LENGTH_WARNING), lpszTitle, MB_YESNO|MB_ICONWARNING|MB_DEFBUTTON2) != IDYES)
        !          1881:                        return FALSE;
        !          1882:        }
        !          1883:        return TRUE;
        !          1884: }
        !          1885: 
        !          1886: 
        !          1887: int GetFirstAvailableDrive ()
        !          1888: {
        !          1889:        DWORD dwUsedDrives = GetLogicalDrives();
        !          1890:        int i;
        !          1891: 
        !          1892:        for (i = 3; i < 26; i++)
        !          1893:        {
        !          1894:                if (!(dwUsedDrives & 1 << i))
        !          1895:                        return i;
        !          1896:        }
        !          1897: 
        !          1898:        return -1;
        !          1899: }
        !          1900: 
        !          1901: 
        !          1902: int GetLastAvailableDrive ()
        !          1903: {
        !          1904:        DWORD dwUsedDrives = GetLogicalDrives();
        !          1905:        int i;
        !          1906: 
        !          1907:        for (i = 25; i > 2; i--)
        !          1908:        {
        !          1909:                if (!(dwUsedDrives & 1 << i))
        !          1910:                        return i;
        !          1911:        }
        !          1912: 
        !          1913:        return -1;
        !          1914: }
        !          1915: 
        !          1916: 
        !          1917: BOOL IsDriveAvailable (int driveNo)
        !          1918: {
        !          1919:        return (GetLogicalDrives() & (1 << driveNo)) == 0;
        !          1920: }
        !          1921: 
        !          1922: 
        !          1923: int DriverUnmountVolume (HWND hwndDlg, int nDosDriveNo, BOOL forced)
        !          1924: {
        !          1925:        UNMOUNT_STRUCT unmount;
        !          1926:        DWORD dwResult;
        !          1927: 
        !          1928:        BOOL bResult;
        !          1929:        
        !          1930:        unmount.nDosDriveNo = nDosDriveNo;
        !          1931:        unmount.ignoreOpenFiles = forced;
        !          1932: 
        !          1933:        bResult = DeviceIoControl (hDriver, UNMOUNT, &unmount,
        !          1934:                sizeof (unmount), &unmount, sizeof (unmount), &dwResult, NULL);
        !          1935: 
        !          1936:        if (bResult == FALSE)
        !          1937:        {
        !          1938:                handleWin32Error (hwndDlg);
        !          1939:                return 1;
        !          1940:        }
        !          1941: 
        !          1942:        return unmount.nReturnCode;
        !          1943: }
        !          1944: 
        !          1945: 
        !          1946: void BroadcastDeviceChange (WPARAM message, int nDosDriveNo)
        !          1947: {
        !          1948:        DEV_BROADCAST_VOLUME dbv;
        !          1949:        char root[] = {nDosDriveNo + 'A', ':', '\\', 0 };
        !          1950: 
        !          1951:        if (message == DBT_DEVICEREMOVECOMPLETE)
        !          1952:                SHChangeNotify(SHCNE_DRIVEREMOVED, SHCNF_PATH, root, NULL);
        !          1953: 
        !          1954:        dbv.dbcv_size = sizeof(dbv); 
        !          1955:        dbv.dbcv_devicetype = DBT_DEVTYP_VOLUME; 
        !          1956:        dbv.dbcv_reserved = 0;
        !          1957:        dbv.dbcv_unitmask = 1 << nDosDriveNo;
        !          1958:        dbv.dbcv_flags = 0; 
        !          1959: 
        !          1960:        SendMessage (HWND_BROADCAST, WM_DEVICECHANGE, message, (LPARAM)(&dbv));
        !          1961: }
        !          1962: 
        !          1963: 
        !          1964: 
        !          1965: // Returns:
        !          1966: // -1 = user aborted mount / error
        !          1967: // 0  = mount failed
        !          1968: // 1  = mount OK
        !          1969: // 2  = mount OK in shared mode
        !          1970: 
        !          1971: int MountVolume (HWND hwndDlg,
        !          1972:                                 int driveNo,
        !          1973:                                 char *volumePath,
        !          1974:                                 char *szPassword,
        !          1975:                                 BOOL cachePassword,
        !          1976:                                 BOOL sharedAccess,
        !          1977:                                 BOOL quiet)
        !          1978: {
        !          1979:        MOUNT_STRUCT driver;
        !          1980:        DWORD dwResult;
        !          1981:        BOOL bResult, bDevice;
        !          1982: 
        !          1983:        if (IsMountedVolume (volumePath))
        !          1984:        {
        !          1985:                if (!quiet)
        !          1986:                        MessageBox(0, getstr (IDS_ALREADY_MOUNTED), lpszTitle, MB_ICONASTERISK);
        !          1987:                return -1;
        !          1988:        }
        !          1989: 
        !          1990:        if (!IsDriveAvailable (driveNo))
        !          1991:                return -1;
        !          1992: 
        !          1993:        // If using cached passwords, check cache status first
        !          1994:        if (szPassword[0] == 0 && IsPasswordCacheEmpty ())
        !          1995:                return 0;
        !          1996: 
        !          1997:        ZeroMemory (&driver, sizeof (driver));
        !          1998:        driver.bExclusiveAccess = sharedAccess ? FALSE : TRUE;
        !          1999: retry:
        !          2000:        driver.nDosDriveNo = driveNo;
        !          2001:        driver.bCache = cachePassword;
        !          2002:        driver.time = time (NULL);
        !          2003:        driver.nPasswordLen = strlen (szPassword);
        !          2004:        strcpy (driver.szPassword, szPassword);
        !          2005:        driver.bMountReadOnly = FALSE;
        !          2006:        driver.bMountRemovable = FALSE;
        !          2007:        driver.bMountManager = TRUE;
        !          2008: 
        !          2009:        // Windows 2000 mount manager causes problems with remounted volumes
        !          2010:        if (CurrentOSMajor == 5 && CurrentOSMinor == 0)
        !          2011:                driver.bMountManager = FALSE;
        !          2012: 
        !          2013:        CreateFullVolumePath ((char *) driver.wszVolume, volumePath, &bDevice);
        !          2014: 
        !          2015:        if (nCurrentOS == WIN_NT)
        !          2016:                ToUNICODE ((char *) driver.wszVolume);
        !          2017: 
        !          2018:        bResult = DeviceIoControl (hDriver, MOUNT, &driver,
        !          2019:                sizeof (driver), &driver, sizeof (driver), &dwResult, NULL);
        !          2020: 
        !          2021:        burn (&driver.szPassword, sizeof (driver.szPassword));
        !          2022: 
        !          2023:        if (bResult == FALSE)
        !          2024:        {
        !          2025:                // Volume already open by another process
        !          2026:                if (GetLastError() == ERROR_SHARING_VIOLATION)
        !          2027:                {
        !          2028:                        if (driver.bExclusiveAccess == FALSE)
        !          2029:                        {
        !          2030:                                if (!quiet)
        !          2031:                                        MessageBox (hwndDlg, getstr (bDevice ? IDS_DEVICE_IN_USE_FAILED : IDS_FILE_IN_USE_FAILED),
        !          2032:                                                lpszTitle, MB_ICONSTOP);
        !          2033: 
        !          2034:                                return -1;
        !          2035:                        }
        !          2036:                        else
        !          2037:                        {
        !          2038:                                if (quiet)
        !          2039:                                {
        !          2040:                                        driver.bExclusiveAccess = FALSE;
        !          2041:                                        goto retry;
        !          2042:                                }
        !          2043: 
        !          2044:                                // Ask user 
        !          2045:                                if (IDYES == MessageBox (hwndDlg, getstr (bDevice ? IDS_DEVICE_IN_USE : IDS_FILE_IN_USE),
        !          2046:                                        lpszTitle, MB_YESNO | MB_DEFBUTTON2 | MB_ICONEXCLAMATION))
        !          2047:                                {
        !          2048:                                        driver.bExclusiveAccess = FALSE;
        !          2049:                                        goto retry;
        !          2050:                                }
        !          2051:                        }
        !          2052: 
        !          2053:                        return -1;
        !          2054:                }
        !          2055: 
        !          2056:                if (!quiet)
        !          2057:                        handleWin32Error (hwndDlg);
        !          2058: 
        !          2059:                return -1;
        !          2060:        }
        !          2061: 
        !          2062:        if (driver.nReturnCode != 0)
        !          2063:        {
        !          2064:                // If using cached passwords, do not report wrong password
        !          2065:                if (szPassword[0] == 0 && driver.nReturnCode == ERR_PASSWORD_WRONG)
        !          2066:                        return 0;
        !          2067: 
        !          2068:                if (!quiet)     handleError (hwndDlg, driver.nReturnCode);
        !          2069: 
        !          2070:                return 0;
        !          2071:        }
        !          2072: 
        !          2073:        BroadcastDeviceChange (DBT_DEVICEARRIVAL, driveNo);
        !          2074: 
        !          2075:        if (driver.bExclusiveAccess == FALSE)
        !          2076:                return 2;
        !          2077: 
        !          2078:        return 1;
        !          2079: }
        !          2080: 
        !          2081: 
        !          2082: BOOL UnmountVolume (HWND hwndDlg , int nDosDriveNo, BOOL forceUnmount)
        !          2083: {
        !          2084:        int result;
        !          2085:        BOOL bResult;
        !          2086:        BOOL forced = forceUnmount;
        !          2087: 
        !          2088:        BroadcastDeviceChange (DBT_DEVICEREMOVEPENDING, nDosDriveNo);
        !          2089: 
        !          2090: retry:
        !          2091:        result = DriverUnmountVolume (hwndDlg, nDosDriveNo, forced);
        !          2092: 
        !          2093:        if (result != 0)
        !          2094:        {
        !          2095:                if (result == ERR_FILES_OPEN)
        !          2096:                {
        !          2097:                        if (IDYES == MessageBox (hwndDlg, getstr (IDS_UNMOUNT_LOCK_FAILED),
        !          2098:                                lpszTitle, MB_YESNO | MB_DEFBUTTON2 | MB_ICONEXCLAMATION))
        !          2099:                        {
        !          2100:                                forced = TRUE;
        !          2101:                                goto retry;
        !          2102:                        }
        !          2103: 
        !          2104:                        return FALSE;
        !          2105:                }
        !          2106: 
        !          2107:                MessageBox (hwndDlg, getstr (IDS_UNMOUNT_FAILED),
        !          2108:                                lpszTitle, MB_ICONERROR);
        !          2109: 
        !          2110:                return FALSE;
        !          2111:        } 
        !          2112:        
        !          2113:        BroadcastDeviceChange (DBT_DEVICEREMOVECOMPLETE, nDosDriveNo);
        !          2114: 
        !          2115:        return TRUE;
        !          2116: }
        !          2117: 
        !          2118: 
        !          2119: BOOL IsPasswordCacheEmpty (void)
        !          2120: {
        !          2121:        DWORD dw;
        !          2122:        return !DeviceIoControl (hDriver, CACHE_STATUS, 0, 0, 0, 0, &dw, 0);
        !          2123: }
        !          2124: 
        !          2125: BOOL IsMountedVolume (char *volname)
        !          2126: {
        !          2127:        MOUNT_LIST_STRUCT mlist;
        !          2128:        DWORD dwResult;
        !          2129:        int i;
        !          2130:        char volume[TC_MAX_PATH*2+16];
        !          2131: 
        !          2132:        strcpy (volume, volname);
        !          2133:        if (nCurrentOS == WIN_NT) 
        !          2134:        {
        !          2135:                if (strstr (volname, "\\Device\\") != volname)
        !          2136:                        sprintf(volume, "\\??\\%s", volname);
        !          2137:                ToUNICODE (volume);
        !          2138:        }
        !          2139: 
        !          2140:        memset (&mlist, 0, sizeof (mlist));
        !          2141:        DeviceIoControl (hDriver, MOUNT_LIST, &mlist,
        !          2142:                sizeof (mlist), &mlist, sizeof (mlist), &dwResult,
        !          2143:                NULL);
        !          2144: 
        !          2145:        for (i=0 ; i<26; i++)
        !          2146:                if (nCurrentOS != WIN_NT && 0 == strcmp ((char *)mlist.wszVolume[i], volume) 
        !          2147:                        || nCurrentOS == WIN_NT && 0 == wcscmp (mlist.wszVolume[i], (WCHAR *)volume))
        !          2148:                        return TRUE;
        !          2149: 
        !          2150:        return FALSE;
        !          2151: }
        !          2152: 
        !          2153: 
        !          2154: BOOL IsAdmin (void)
        !          2155: {
        !          2156:        HANDLE hAccessToken;
        !          2157:        UCHAR InfoBuffer[1024];
        !          2158:        PTOKEN_GROUPS ptgGroups = (PTOKEN_GROUPS) InfoBuffer;
        !          2159:        DWORD dwInfoBufferSize;
        !          2160:        PSID psidAdministrators;
        !          2161:        SID_IDENTIFIER_AUTHORITY siaNtAuthority = SECURITY_NT_AUTHORITY;
        !          2162:        BOOL bSuccess;
        !          2163:        UINT x;
        !          2164: 
        !          2165:        if (!OpenThreadToken (GetCurrentThread (), TOKEN_QUERY, TRUE,
        !          2166:                              &hAccessToken))
        !          2167:        {
        !          2168:                if (GetLastError ()!= ERROR_NO_TOKEN)
        !          2169:                        return FALSE;
        !          2170: 
        !          2171:                /* Retry against process token if no thread token exists */
        !          2172:                if (!OpenProcessToken (GetCurrentProcess (), TOKEN_QUERY,
        !          2173:                                       &hAccessToken))
        !          2174:                        return FALSE;
        !          2175:        }
        !          2176: 
        !          2177:        bSuccess = GetTokenInformation (hAccessToken, TokenGroups, InfoBuffer,
        !          2178:                                        1024, &dwInfoBufferSize);
        !          2179: 
        !          2180:        CloseHandle (hAccessToken);
        !          2181: 
        !          2182:        if (!bSuccess)
        !          2183:                return FALSE;
        !          2184: 
        !          2185:        if (!AllocateAndInitializeSid (&siaNtAuthority, 2,
        !          2186:                                       SECURITY_BUILTIN_DOMAIN_RID,
        !          2187:                                       DOMAIN_ALIAS_RID_ADMINS,
        !          2188:                                       0, 0, 0, 0, 0, 0,
        !          2189:                                       &psidAdministrators))
        !          2190:                return FALSE;
        !          2191:  
        !          2192:        /* Assume that we don't find the admin SID. */
        !          2193:        bSuccess = FALSE;
        !          2194: 
        !          2195:        for (x = 0; x < ptgGroups->GroupCount; x++)
        !          2196:        {
        !          2197:                if (EqualSid (psidAdministrators, ptgGroups->Groups[x].Sid))
        !          2198:                {
        !          2199:                        bSuccess = TRUE;
        !          2200:                        break;
        !          2201:                }
        !          2202: 
        !          2203:        }
        !          2204: 
        !          2205:        FreeSid (psidAdministrators);
        !          2206:        return bSuccess;
        !          2207: }
        !          2208: 
        !          2209: 
        !          2210: BOOL ResolveSymbolicLink (PWSTR symLinkName, PWSTR targetName)
        !          2211: {
        !          2212:        BOOL bResult;
        !          2213:        DWORD dwResult;
        !          2214:        RESOLVE_SYMLINK_STRUCT resolve;
        !          2215: 
        !          2216:        memset (&resolve, 0, sizeof(resolve));
        !          2217:        wcscpy ((PWSTR) &resolve.symLinkName, symLinkName);
        !          2218: 
        !          2219:        bResult = DeviceIoControl (hDriver, RESOLVE_SYMLINK, &resolve,
        !          2220:                sizeof (resolve), &resolve, sizeof (resolve), &dwResult,
        !          2221:                NULL);
        !          2222: 
        !          2223:        wcscpy (targetName, (PWSTR) &resolve.targetName);
        !          2224: 
        !          2225:        return bResult;
        !          2226: }
        !          2227: 
        !          2228: 
        !          2229: // Returns drive letter number assigned to device (-1 if none)
        !          2230: int GetDiskDeviceDriveLetter (PWSTR deviceName)
        !          2231: {
        !          2232:        int i;
        !          2233:        WCHAR link[MAX_PATH];
        !          2234:        WCHAR target[MAX_PATH];
        !          2235:        WCHAR device[MAX_PATH];
        !          2236:        char msg[100];
        !          2237: 
        !          2238:        if (!ResolveSymbolicLink (deviceName, device))
        !          2239:                wcscpy (device, deviceName);
        !          2240: 
        !          2241:        for (i = 0; i < 26; i++)
        !          2242:        {
        !          2243:                WCHAR drive[] = { i + 'A', ':', 0 };
        !          2244: 
        !          2245:                wcscpy (link, L"\\DosDevices\\");
        !          2246:                wcscat (link, drive);
        !          2247: 
        !          2248:                ResolveSymbolicLink (link, target);
        !          2249: 
        !          2250:                if (wcscmp (device, target) == 0)
        !          2251:                        return i;
        !          2252:        }
        !          2253: 
        !          2254:        return -1;
        !          2255: }
        !          2256: 
        !          2257: 
        !          2258: HANDLE DismountDrive (int driveNo)
        !          2259: {
        !          2260:        char volMountName[32];
        !          2261:        char dosName[3];
        !          2262:        DWORD dwResult;
        !          2263:        BOOL bResult;
        !          2264:        HANDLE hVolume;
        !          2265: 
        !          2266:        dosName[0] = (char) (driveNo + 'A');
        !          2267:        dosName[1] = ':';
        !          2268:        dosName[2] = 0;
        !          2269: 
        !          2270:        sprintf (volMountName, "\\\\.\\%s", dosName);
        !          2271: 
        !          2272:        hVolume = CreateFile (volMountName, GENERIC_READ | GENERIC_WRITE,
        !          2273:                FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
        !          2274: 
        !          2275:        bResult = DeviceIoControl (hVolume, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0, &dwResult, NULL);
        !          2276:        bResult = DeviceIoControl (hVolume, FSCTL_DISMOUNT_VOLUME, NULL, 0, NULL, 0, &dwResult, NULL);
        !          2277: 
        !          2278:        return hVolume;
1.1       root     2279: }

unix.superglobalmegacorp.com

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