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

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

unix.superglobalmegacorp.com

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