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

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

unix.superglobalmegacorp.com

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