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

1.1.1.11  root        1: /*
1.1.1.13  root        2:  Legal Notice: Some portions of the source code contained in this file were
                      3:  derived from the source code of Encryption for the Masses 2.02a, which is
                      4:  Copyright (c) 1998-2000 Paul Le Roux and which is governed by the 'License
                      5:  Agreement for Encryption for the Masses'. Modifications and additions to
                      6:  the original source code (contained in this file) and all other portions of
                      7:  this file are Copyright (c) 2003-2008 TrueCrypt Foundation and are governed
                      8:  by the TrueCrypt License 2.4 the full text of which is contained in the
                      9:  file License.txt included in TrueCrypt binary and source code distribution
1.1.1.11  root       10:  packages. */
1.1       root       11: 
1.1.1.7   root       12: #include "Tcdefs.h"
1.1       root       13: 
1.1.1.7   root       14: #include <dbt.h>
                     15: #include <fcntl.h>
                     16: #include <io.h>
1.1.1.12  root       17: #include <math.h>
1.1.1.7   root       18: #include <shlobj.h>
                     19: #include <sys/stat.h>
1.1       root       20: #include <stdlib.h>
1.1.1.5   root       21: #include <time.h>
1.1       root       22: 
1.1.1.7   root       23: #include "Resource.h"
                     24: 
                     25: #include "Apidrvr.h"
1.1.1.11  root       26: #include "Combo.h"
1.1.1.12  root       27: #include "Crc.h"
1.1.1.7   root       28: #include "Crypto.h"
                     29: #include "Dictionary.h"
                     30: #include "Dlgcode.h"
1.1.1.13  root       31: #include "Endian.h"
1.1.1.7   root       32: #include "Language.h"
                     33: #include "Keyfiles.h"
1.1.1.11  root       34: #include "Mount/Mount.h"
1.1.1.7   root       35: #include "Pkcs5.h"
                     36: #include "Random.h"
                     37: #include "Registry.h"
                     38: #include "Tests.h"
                     39: #include "Volumes.h"
1.1.1.13  root       40: #include "Wipe.h"
1.1.1.7   root       41: #include "Xml.h"
1.1.1.13  root       42: #include "Xts.h"
1.1.1.5   root       43: 
1.1.1.11  root       44: #ifdef VOLFORMAT
                     45: #include "Format/Tcformat.h"
                     46: #endif
                     47: 
                     48: LONG DriverVersion;
                     49: 
1.1.1.10  root       50: char *LastDialogId;
1.1       root       51: char szHelpFile[TC_MAX_PATH];
1.1.1.7   root       52: char szHelpFile2[TC_MAX_PATH];
                     53: HFONT hFixedDigitFont = NULL;
1.1       root       54: HFONT hBoldFont = NULL;
                     55: HFONT hTitleFont = NULL;
                     56: HFONT hFixedFont = NULL;
                     57: 
                     58: HFONT hUserFont = NULL;
                     59: HFONT hUserUnderlineFont = NULL;
                     60: HFONT hUserBoldFont = NULL;
1.1.1.5   root       61: HFONT hUserUnderlineBoldFont = NULL;
1.1       root       62: 
1.1.1.12  root       63: int ScreenDPI = USER_DEFAULT_SCREEN_DPI;
                     64: double DPIScaleFactorX = 1;
                     65: double DPIScaleFactorY = 1;
                     66: double DlgAspectRatio = 1;
                     67: 
1.1.1.7   root       68: HWND MainDlg = NULL;
                     69: wchar_t *lpszTitle = NULL;
                     70: 
                     71: BOOL Silent = FALSE;
                     72: BOOL bPreserveTimestamp = TRUE;
1.1.1.13  root       73: BOOL bStartOnLogon = FALSE;
                     74: BOOL bMountDevicesOnLogon = FALSE;
                     75: BOOL bMountFavoritesOnLogon = FALSE;
1.1.1.7   root       76: 
1.1.1.10  root       77: BOOL bHistory = FALSE;
                     78: 
1.1       root       79: int nCurrentOS = 0;
                     80: int CurrentOSMajor = 0;
                     81: int CurrentOSMinor = 0;
1.1.1.10  root       82: int CurrentOSServicePack = 0;
                     83: BOOL RemoteSession = FALSE;
1.1.1.11  root       84: BOOL UacElevated = FALSE;
1.1       root       85: 
1.1.1.7   root       86: /* Globals used by Mount and Format (separately per instance) */ 
                     87: BOOL   KeyFilesEnable = FALSE;
                     88: KeyFile        *FirstKeyFile = NULL;
                     89: KeyFilesDlgParam               defaultKeyFilesParam;
                     90: 
1.1.1.12  root       91: BOOL IgnoreWmDeviceChange = FALSE;
                     92: 
1.1       root       93: /* Handle to the device driver */
                     94: HANDLE hDriver = INVALID_HANDLE_VALUE;
1.1.1.11  root       95: 
1.1.1.13  root       96: /* This mutex is used to prevent multiple instances of the wizard or main app from dealing with system encryption */
                     97: HANDLE hSysEncMutex = NULL;            
                     98: 
1.1       root       99: HINSTANCE hInst = NULL;
                    100: HCURSOR hCursor = NULL;
                    101: 
                    102: ATOM hDlgClass, hSplashClass;
                    103: 
1.1.1.13  root      104: /* This value may changed only by calling ChangeSystemEncryptionStatus(). Only the wizard can change it
                    105: (others may still read it though). */
                    106: int SystemEncryptionStatus = SYSENC_STATUS_NONE;       
                    107: 
                    108: /* Only the wizard can change this value (others may only read it). */
                    109: WipeAlgorithmId nWipeMode = TC_WIPE_NONE;
                    110: 
                    111: BOOL bSysPartitionSelected = FALSE;            /* TRUE if the user selected the system partition via the Select Device dialog */
                    112: BOOL bSysDriveSelected = FALSE;                        /* TRUE if the user selected the system drive via the Select Device dialog */
                    113: 
                    114: /* To populate these arrays, call GetSysDevicePaths(). If they contain valid paths, bCachedSysDevicePathsValid is TRUE. */
                    115: char SysPartitionDevicePath [TC_MAX_PATH];
                    116: char SysDriveDevicePath [TC_MAX_PATH];
                    117: char bCachedSysDevicePathsValid = FALSE;
                    118: BOOL bRawDevicesDlgProcInstantExit = FALSE;
                    119: 
                    120: BOOL bHyperLinkBeingTracked = FALSE;
                    121: 
1.1.1.7   root      122: static FILE *ConfigFileHandle;
1.1.1.13  root      123: char *ConfigBuffer;
1.1.1.7   root      124: 
1.1.1.13  root      125: #define RANDPOOL_DISPLAY_REFRESH_INTERVAL      30
                    126: #define RANDPOOL_DISPLAY_ROWS 20
                    127: #define RANDPOOL_DISPLAY_COLUMNS 32
1.1.1.7   root      128: 
1.1       root      129: /* Windows dialog class */
                    130: #define WINDOWS_DIALOG_CLASS "#32770"
                    131: 
                    132: /* Custom class names */
                    133: #define TC_DLG_CLASS "CustomDlg"
                    134: #define TC_SPLASH_CLASS "SplashDlg"
                    135: 
1.1.1.7   root      136: /* Benchmarks */
                    137: 
1.1.1.5   root      138: #ifndef SETUP
1.1.1.7   root      139: 
1.1.1.5   root      140: #define BENCHMARK_MAX_ITEMS 100
1.1.1.7   root      141: #define BENCHMARK_DEFAULT_BUF_SIZE     BYTES_PER_MB
                    142: #define HASH_FNC_BENCHMARKS    FALSE   // For development purposes only. Must be FALSE when building a public release.
                    143: #define PKCS5_BENCHMARKS       FALSE   // For development purposes only. Must be FALSE when building a public release.
                    144: #if PKCS5_BENCHMARKS && HASH_FNC_BENCHMARKS
                    145: #error PKCS5_BENCHMARKS and HASH_FNC_BENCHMARKS are both TRUE (at least one of them should be FALSE).
                    146: #endif
1.1.1.5   root      147: 
                    148: enum 
                    149: {
                    150:        BENCHMARK_SORT_BY_NAME = 0,
                    151:        BENCHMARK_SORT_BY_SPEED
                    152: };
                    153: 
                    154: typedef struct 
                    155: {
                    156:        int id;
                    157:        char name[100];
                    158:        unsigned __int64 encSpeed;
                    159:        unsigned __int64 decSpeed;
                    160:        unsigned __int64 meanBytesPerSec;
                    161: } BENCHMARK_REC;
                    162: 
                    163: BENCHMARK_REC benchmarkTable [BENCHMARK_MAX_ITEMS];
                    164: int benchmarkTotalItems = 0;
                    165: int benchmarkBufferSize = BENCHMARK_DEFAULT_BUF_SIZE;
                    166: int benchmarkLastBufferSize = BENCHMARK_DEFAULT_BUF_SIZE;
                    167: int benchmarkSortMethod = BENCHMARK_SORT_BY_SPEED;
                    168: LARGE_INTEGER benchmarkPerformanceFrequency;
1.1.1.7   root      169: 
1.1.1.5   root      170: #endif // #ifndef SETUP
                    171: 
                    172: 
1.1       root      173: void
                    174: cleanup ()
                    175: {
                    176:        /* Cleanup the GDI fonts */
                    177:        if (hFixedFont != NULL)
                    178:                DeleteObject (hFixedFont);
1.1.1.7   root      179:        if (hFixedDigitFont != NULL)
                    180:                DeleteObject (hFixedDigitFont);
1.1       root      181:        if (hBoldFont != NULL)
                    182:                DeleteObject (hBoldFont);
                    183:        if (hTitleFont != NULL)
                    184:                DeleteObject (hTitleFont);
                    185:        if (hUserFont != NULL)
                    186:                DeleteObject (hUserFont);
                    187:        if (hUserUnderlineFont != NULL)
                    188:                DeleteObject (hUserUnderlineFont);
                    189:        if (hUserBoldFont != NULL)
                    190:                DeleteObject (hUserBoldFont);
1.1.1.5   root      191:        if (hUserUnderlineBoldFont != NULL)
                    192:                DeleteObject (hUserUnderlineBoldFont);
1.1.1.12  root      193: 
1.1       root      194:        /* Cleanup our dialog class */
                    195:        if (hDlgClass)
                    196:                UnregisterClass (TC_DLG_CLASS, hInst);
                    197:        if (hSplashClass)
                    198:                UnregisterClass (TC_SPLASH_CLASS, hInst);
1.1.1.12  root      199: 
1.1       root      200:        /* Close the device driver handle */
                    201:        if (hDriver != INVALID_HANDLE_VALUE)
                    202:        {
1.1.1.7   root      203:                // Unload driver mode if possible (non-install mode) 
                    204:                if (IsNonInstallMode ())
1.1.1.12  root      205:                {
                    206:                        // If a dismount was forced in the lifetime of the driver, Windows may later prevent it to be loaded again from
                    207:                        // the same path. Therefore, the driver will not be unloaded even though it was loaded in non-install mode.
                    208:                        int refDevDeleted;
                    209:                        DWORD dwResult;
                    210: 
1.1.1.13  root      211:                        if (!DeviceIoControl (hDriver, TC_IOCTL_WAS_REFERENCED_DEVICE_DELETED, NULL, 0, &refDevDeleted, sizeof (refDevDeleted), &dwResult, NULL))
1.1.1.12  root      212:                                refDevDeleted = 0;
                    213: 
                    214:                        if (!refDevDeleted)
                    215:                                DriverUnload ();
                    216:                        else
                    217:                                CloseHandle (hDriver);
                    218:                }
1.1.1.7   root      219:                else
1.1.1.12  root      220:                {
1.1.1.7   root      221:                        CloseHandle (hDriver);
1.1.1.12  root      222:                }
1.1       root      223:        }
                    224: 
1.1.1.7   root      225:        if (ConfigBuffer != NULL)
                    226:        {
                    227:                free (ConfigBuffer);
                    228:                ConfigBuffer = NULL;
                    229:        }
1.1.1.10  root      230: 
                    231:        CoUninitialize ();
1.1.1.13  root      232: 
                    233:        CloseSysEncMutex ();
1.1       root      234: }
                    235: 
1.1.1.12  root      236: 
1.1       root      237: void
                    238: LowerCaseCopy (char *lpszDest, char *lpszSource)
                    239: {
                    240:        int i = strlen (lpszSource);
                    241: 
                    242:        lpszDest[i] = 0;
                    243:        while (--i >= 0)
                    244:        {
                    245:                lpszDest[i] = (char) tolower (lpszSource[i]);
                    246:        }
                    247: 
                    248: }
                    249: 
                    250: void
                    251: UpperCaseCopy (char *lpszDest, char *lpszSource)
                    252: {
                    253:        int i = strlen (lpszSource);
                    254: 
                    255:        lpszDest[i] = 0;
                    256:        while (--i >= 0)
                    257:        {
                    258:                lpszDest[i] = (char) toupper (lpszSource[i]);
                    259:        }
                    260: }
                    261: 
1.1.1.11  root      262: 
                    263: BOOL IsVolumeDeviceHosted (char *lpszDiskFile)
                    264: {
                    265:        return strstr (lpszDiskFile, "\\Device\\") == lpszDiskFile
                    266:                || strstr (lpszDiskFile, "\\DEVICE\\") == lpszDiskFile;
                    267: }
                    268: 
                    269: 
1.1       root      270: void
                    271: CreateFullVolumePath (char *lpszDiskFile, char *lpszFileName, BOOL * bDevice)
                    272: {
                    273:        if (strcmp (lpszFileName, "Floppy (A:)") == 0)
                    274:                strcpy (lpszFileName, "\\Device\\Floppy0");
                    275:        else if (strcmp (lpszFileName, "Floppy (B:)") == 0)
                    276:                strcpy (lpszFileName, "\\Device\\Floppy1");
                    277: 
                    278:        UpperCaseCopy (lpszDiskFile, lpszFileName);
                    279: 
                    280:        *bDevice = FALSE;
                    281: 
                    282:        if (memcmp (lpszDiskFile, "\\DEVICE", sizeof (char) * 7) == 0)
                    283:        {
                    284:                *bDevice = TRUE;
                    285:        }
                    286: 
                    287:        strcpy (lpszDiskFile, lpszFileName);
                    288: 
                    289: #if _DEBUG
                    290:        OutputDebugString ("CreateFullVolumePath: ");
                    291:        OutputDebugString (lpszDiskFile);
                    292:        OutputDebugString ("\n");
                    293: #endif
                    294: 
                    295: }
                    296: 
                    297: int
                    298: FakeDosNameForDevice (char *lpszDiskFile, char *lpszDosDevice, char *lpszCFDevice, BOOL bNameOnly)
                    299: {
                    300:        BOOL bDosLinkCreated = TRUE;
                    301:        sprintf (lpszDosDevice, "truecrypt%lu", GetCurrentProcessId ());
                    302: 
                    303:        if (bNameOnly == FALSE)
                    304:                bDosLinkCreated = DefineDosDevice (DDD_RAW_TARGET_PATH, lpszDosDevice, lpszDiskFile);
                    305: 
                    306:        if (bDosLinkCreated == FALSE)
                    307:                return ERR_OS_ERROR;
                    308:        else
                    309:                sprintf (lpszCFDevice, "\\\\.\\%s", lpszDosDevice);
                    310: 
                    311:        return 0;
                    312: }
                    313: 
                    314: int
                    315: RemoveFakeDosName (char *lpszDiskFile, char *lpszDosDevice)
                    316: {
                    317:        BOOL bDosLinkRemoved = DefineDosDevice (DDD_RAW_TARGET_PATH | DDD_EXACT_MATCH_ON_REMOVE |
                    318:                        DDD_REMOVE_DEFINITION, lpszDosDevice, lpszDiskFile);
                    319:        if (bDosLinkRemoved == FALSE)
                    320:        {
                    321:                return ERR_OS_ERROR;
                    322:        }
                    323: 
                    324:        return 0;
                    325: }
                    326: 
                    327: 
                    328: void
1.1.1.7   root      329: AbortProcess (char *stringId)
1.1       root      330: {
                    331:        MessageBeep (MB_ICONEXCLAMATION);
1.1.1.7   root      332:        MessageBoxW (NULL, GetString (stringId), lpszTitle, ICON_HAND);
1.1       root      333:        exit (1);
                    334: }
                    335: 
1.1.1.5   root      336: void
                    337: AbortProcessSilent (void)
                    338: {
                    339:        exit (1);
                    340: }
                    341: 
1.1       root      342: void *
                    343: err_malloc (size_t size)
                    344: {
                    345:        void *z = (void *) TCalloc (size);
                    346:        if (z)
                    347:                return z;
1.1.1.7   root      348:        AbortProcess ("OUTOFMEMORY");
1.1       root      349:        return 0;
                    350: }
                    351: 
                    352: char *
                    353: err_strdup (char *lpszText)
                    354: {
                    355:        int j = (strlen (lpszText) + 1) * sizeof (char);
                    356:        char *z = (char *) err_malloc (j);
                    357:        memmove (z, lpszText, j);
                    358:        return z;
                    359: }
                    360: 
1.1.1.5   root      361: DWORD
1.1       root      362: handleWin32Error (HWND hwndDlg)
                    363: {
1.1.1.7   root      364:        PWSTR lpMsgBuf;
1.1       root      365:        DWORD dwError = GetLastError ();
                    366: 
1.1.1.13  root      367:        if (Silent || dwError == 0 || dwError == ERROR_INVALID_WINDOW_HANDLE)
1.1.1.11  root      368:                return dwError;
                    369: 
                    370:        // Access denied
                    371:        if (dwError == ERROR_ACCESS_DENIED && !IsAdmin ())
                    372:        {
                    373:                Error ("ERR_ACCESS_DENIED");
                    374:                return dwError;
                    375:        }
1.1.1.7   root      376: 
                    377:        FormatMessageW (
1.1       root      378:                FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
                    379:                              NULL,
                    380:                              dwError,
                    381:                              MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT),       /* Default language */
1.1.1.7   root      382:                              (PWSTR) &lpMsgBuf,
1.1       root      383:                              0,
                    384:                              NULL
                    385:            );
                    386: 
1.1.1.7   root      387:        MessageBoxW (hwndDlg, lpMsgBuf, lpszTitle, ICON_HAND);
1.1       root      388:        LocalFree (lpMsgBuf);
1.1.1.5   root      389: 
1.1.1.14! root      390:        // User-friendly hardware error explanation
        !           391:        if (dwError == ERROR_CRC || dwError == ERROR_IO_DEVICE || dwError == ERROR_BAD_CLUSTERS)
        !           392:                Error ("ERR_HARDWARE_ERROR");
        !           393: 
1.1.1.7   root      394:        // Device not ready
                    395:        if (dwError == ERROR_NOT_READY)
                    396:                CheckSystemAutoMount();
                    397: 
1.1.1.5   root      398:        return dwError;
1.1       root      399: }
                    400: 
                    401: BOOL
1.1.1.7   root      402: translateWin32Error (wchar_t *lpszMsgBuf, int nSizeOfBuf)
1.1       root      403: {
                    404:        DWORD dwError = GetLastError ();
                    405: 
1.1.1.7   root      406:        if (FormatMessageW (FORMAT_MESSAGE_FROM_SYSTEM, NULL, dwError,
1.1       root      407:                           MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT),  /* Default language */
                    408:                           lpszMsgBuf, nSizeOfBuf, NULL))
                    409:                return TRUE;
                    410:        else
                    411:                return FALSE;
                    412: }
                    413: 
1.1.1.11  root      414: 
1.1.1.12  root      415: // If the user has a non-default screen DPI, all absolute font sizes must be
                    416: // converted using this function.
                    417: int CompensateDPIFont (int val)
                    418: {
                    419:        if (ScreenDPI == USER_DEFAULT_SCREEN_DPI)
                    420:                return val;
                    421:        else
                    422:        {
                    423:                double tmpVal = (double) val * DPIScaleFactorY * DlgAspectRatio * 0.999;
                    424: 
                    425:                if (tmpVal > 0)
                    426:                        return (int) floor(tmpVal);
                    427:                else
                    428:                        return (int) ceil(tmpVal);
                    429:        }
                    430: }
                    431: 
                    432: 
                    433: // If the user has a non-default screen DPI, some screen coordinates and sizes must
                    434: // be converted using this function
                    435: int CompensateXDPI (int val)
                    436: {
                    437:        if (ScreenDPI == USER_DEFAULT_SCREEN_DPI)
                    438:                return val;
                    439:        else
                    440:        {
                    441:                double tmpVal = (double) val * DPIScaleFactorX;
                    442: 
                    443:                if (tmpVal > 0)
                    444:                        return (int) floor(tmpVal);
                    445:                else
                    446:                        return (int) ceil(tmpVal);
                    447:        }
                    448: }
                    449: 
                    450: 
                    451: // If the user has a non-default screen DPI, some screen coordinates and sizes must
                    452: // be converted using this function
                    453: int CompensateYDPI (int val)
                    454: {
                    455:        if (ScreenDPI == USER_DEFAULT_SCREEN_DPI)
                    456:                return val;
                    457:        else
                    458:        {
                    459:                double tmpVal = (double) val * DPIScaleFactorY;
                    460: 
                    461:                if (tmpVal > 0)
                    462:                        return (int) floor(tmpVal);
                    463:                else
                    464:                        return (int) ceil(tmpVal);
                    465:        }
                    466: }
                    467: 
                    468: 
1.1.1.11  root      469: int GetTextGfxWidth (HWND hwndDlgItem, wchar_t *text, HFONT hFont)
                    470: {
                    471:        SIZE sizes;
                    472:        TEXTMETRIC textMetrics;
                    473:        HDC hdc = GetDC (hwndDlgItem); 
                    474: 
                    475:        SelectObject(hdc, (HGDIOBJ) hFont);
                    476: 
                    477:        GetTextExtentPoint32W (hdc, text, wcslen (text), &sizes);
                    478: 
                    479:        GetTextMetrics(hdc, &textMetrics);      // Necessary for non-TrueType raster fonts (tmOverhang)
                    480: 
                    481:        ReleaseDC (hwndDlgItem, hdc); 
                    482: 
                    483:        return ((int) sizes.cx - (int) textMetrics.tmOverhang);
                    484: }
                    485: 
                    486: 
                    487: int GetTextGfxHeight (HWND hwndDlgItem, wchar_t *text, HFONT hFont)
                    488: {
                    489:        SIZE sizes;
                    490:        HDC hdc = GetDC (hwndDlgItem); 
                    491: 
                    492:        SelectObject(hdc, (HGDIOBJ) hFont);
                    493: 
                    494:        GetTextExtentPoint32W (hdc, text, wcslen (text), &sizes);
                    495: 
                    496:        ReleaseDC (hwndDlgItem, hdc); 
                    497: 
                    498:        return ((int) sizes.cy);
                    499: }
                    500: 
                    501: 
                    502: static LRESULT CALLBACK HyperlinkProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
                    503: {
                    504:        WNDPROC wp = (WNDPROC) GetWindowLongPtr (hwnd, GWL_USERDATA);
                    505: 
                    506:        switch (message)
                    507:        {
                    508:        case WM_SETCURSOR:
1.1.1.13  root      509:                if (!bHyperLinkBeingTracked)
1.1.1.11  root      510:                {
                    511:                        TRACKMOUSEEVENT trackMouseEvent;
                    512: 
                    513:                        trackMouseEvent.cbSize = sizeof(trackMouseEvent);
                    514:                        trackMouseEvent.dwFlags = TME_LEAVE;
                    515:                        trackMouseEvent.hwndTrack = hwnd;
                    516: 
1.1.1.13  root      517:                        bHyperLinkBeingTracked = TrackMouseEvent(&trackMouseEvent);
1.1.1.11  root      518: 
                    519:                        HandCursor();
                    520:                }
                    521:                return 0;
                    522: 
                    523:        case WM_MOUSELEAVE:
1.1.1.13  root      524:                bHyperLinkBeingTracked = FALSE;
1.1.1.11  root      525:                NormalCursor();
                    526:                return 0;
                    527:        }
                    528: 
                    529:        return CallWindowProc (wp, hwnd, message, wParam, lParam);
                    530: }
                    531: 
                    532: 
                    533: BOOL ToHyperlink (HWND hwndDlg, UINT ctrlId)
                    534: {
                    535:        HWND hwndCtrl = GetDlgItem (hwndDlg, ctrlId);
                    536: 
                    537:        SendMessage (hwndCtrl, WM_SETFONT, (WPARAM) hUserUnderlineFont, 0);
                    538: 
                    539:        SetWindowLongPtr (hwndCtrl, GWL_USERDATA, (LONG_PTR) GetWindowLongPtr (hwndCtrl, GWL_WNDPROC));
                    540:        SetWindowLongPtr (hwndCtrl, GWL_WNDPROC, (LONG_PTR) HyperlinkProc);
                    541: 
1.1.1.12  root      542:        // Resize the field according to its actual length in pixels and move if centered or right-aligned.
                    543:        // This should be done again if the link text changes.
                    544:        AccommodateTextField (hwndDlg, ctrlId, TRUE);
                    545: 
                    546:        return TRUE;
                    547: }
                    548: 
                    549: 
                    550: // Resizes a text field according to its actual width in pixels (font size is taken into account) and moves
                    551: // it accordingly if the field is centered or right-aligned. Should be used on all hyperlinks upon dialog init
                    552: // after localization (bFirstUpdate should be TRUE) and later whenever a hyperlink text changes (bFirstUpdate
                    553: // must be FALSE).
                    554: void AccommodateTextField (HWND hwndDlg, UINT ctrlId, BOOL bFirstUpdate)
                    555: {
                    556:        RECT rec, wrec, trec;
                    557:        HWND hwndCtrl = GetDlgItem (hwndDlg, ctrlId);
                    558:        int width, origWidth, origHeight;
                    559:        int horizSubOffset, vertOffset, alignPosDiff = 0;
                    560:        wchar_t text [MAX_URL_LENGTH];
                    561:        WINDOWINFO windowInfo;
                    562:        BOOL bBorderlessWindow = !(GetWindowLongPtr (hwndDlg, GWL_STYLE) & (WS_BORDER | WS_DLGFRAME));
1.1.1.11  root      563: 
                    564:        // Resize the field according to its length and font size and move if centered or right-aligned
                    565: 
                    566:        GetWindowTextW (hwndCtrl, text, sizeof (text) / sizeof (wchar_t));
                    567: 
                    568:        width = GetTextGfxWidth (hwndCtrl, text, hUserUnderlineFont);
                    569: 
                    570:        GetClientRect (hwndCtrl, &rec);         
                    571:        origWidth = rec.right;
                    572:        origHeight = rec.bottom;
                    573: 
1.1.1.12  root      574:        if (width >= 0
                    575:                && (!bFirstUpdate || origWidth > width))        // The original width of the field is the maximum allowed size 
1.1.1.11  root      576:        {
                    577:                horizSubOffset = origWidth - width;
                    578: 
                    579:                // Window coords
                    580:                GetWindowRect(hwndDlg, &wrec);
                    581:                GetClientRect(hwndDlg, &trec);
                    582: 
                    583:                // Vertical "title bar" offset
                    584:                vertOffset = wrec.bottom - wrec.top - trec.bottom - (bBorderlessWindow ? 0 : GetSystemMetrics(SM_CYFIXEDFRAME));
                    585: 
                    586:                // Text field coords
                    587:                GetWindowRect(hwndCtrl, &rec);
                    588: 
                    589:                // Alignment offset
                    590:                windowInfo.cbSize = sizeof(windowInfo);
                    591:                GetWindowInfo (hwndCtrl, &windowInfo);
                    592: 
                    593:                if (windowInfo.dwStyle & SS_CENTER)
                    594:                        alignPosDiff = horizSubOffset / 2;
                    595:                else if (windowInfo.dwStyle & SS_RIGHT)
                    596:                        alignPosDiff = horizSubOffset;
                    597:                
                    598:                // Resize/move
                    599:                if (alignPosDiff > 0)
                    600:                {
                    601:                        // Resize and move the text field
                    602:                        MoveWindow (hwndCtrl,
                    603:                                rec.left - wrec.left - (bBorderlessWindow ? 0 : GetSystemMetrics(SM_CXFIXEDFRAME)) + alignPosDiff,
                    604:                                rec.top - wrec.top - vertOffset,
                    605:                                origWidth - horizSubOffset,
                    606:                                origHeight,
                    607:                                TRUE);
                    608:                }
                    609:                else
                    610:                {
                    611:                        // Resize the text field
                    612:                        SetWindowPos (hwndCtrl, 0, 0, 0,
                    613:                                origWidth - horizSubOffset,
                    614:                                origHeight,
                    615:                                SWP_NOMOVE | SWP_NOZORDER);
                    616:                }
1.1.1.12  root      617: 
                    618:                SetWindowPos (hwndCtrl, HWND_BOTTOM, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
                    619: 
                    620:                InvalidateRect (hwndCtrl, NULL, TRUE);
1.1.1.11  root      621:        }
1.1.1.12  root      622: }
                    623: 
                    624: 
1.1.1.13  root      625: // Protects an input field from having its content updated by a Paste action (call ToBootPwdField() to use this).
                    626: static LRESULT CALLBACK BootPwdFieldProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
                    627: {
                    628:        WNDPROC wp = (WNDPROC) GetWindowLongPtr (hwnd, GWL_USERDATA);
                    629: 
                    630:        switch (message)
                    631:        {
                    632:        case WM_PASTE:
                    633:                return 1;
                    634:        }
                    635: 
                    636:        return CallWindowProc (wp, hwnd, message, wParam, lParam);
                    637: }
                    638: 
                    639: 
1.1.1.14! root      640: // Protects an input field from having its content updated by a Paste action. Used for pre-boot password
        !           641: // input fields (only the US keyboard layout is supported in pre-boot environment so we must prevent the 
        !           642: // user from pasting a password typed using a non-US keyboard layout).
1.1.1.13  root      643: void ToBootPwdField (HWND hwndDlg, UINT ctrlId)
                    644: {
                    645:        HWND hwndCtrl = GetDlgItem (hwndDlg, ctrlId);
                    646: 
                    647:        SetWindowLongPtr (hwndCtrl, GWL_USERDATA, (LONG_PTR) GetWindowLongPtr (hwndCtrl, GWL_WNDPROC));
                    648:        SetWindowLongPtr (hwndCtrl, GWL_WNDPROC, (LONG_PTR) BootPwdFieldProc);
                    649: }
                    650: 
                    651: 
                    652: 
1.1.1.12  root      653: // This function currently serves the following purposes:
                    654: // - Determines scaling factors for current screen DPI and GUI aspect ratio.
                    655: // - Determines how Windows skews the GUI aspect ratio (which happens when the user has a non-default DPI).
                    656: // The determined values must be used when performing some GUI operations and calculations.
                    657: BOOL CALLBACK AuxiliaryDlgProc (HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam)
                    658: {
                    659:        switch (msg)
                    660:        {
                    661:        case WM_INITDIALOG:
                    662:                {
                    663:                        HDC hDC = GetDC (hwndDlg);
                    664: 
                    665:                        ScreenDPI = GetDeviceCaps (hDC, LOGPIXELSY);
                    666:                        ReleaseDC (hwndDlg, hDC); 
                    667: 
                    668:                        DPIScaleFactorX = 1;
                    669:                        DPIScaleFactorY = 1;
                    670:                        DlgAspectRatio = 1;
                    671: 
                    672:                        if (ScreenDPI != USER_DEFAULT_SCREEN_DPI)
                    673:                        {
                    674:                                // Windows skews the GUI aspect ratio if the user has a non-default DPI. Hence, working with 
                    675:                                // actual screen DPI is redundant and leads to incorrect results. What really matters here is
                    676:                                // how Windows actually renders our GUI. This is determined by comparing the expected and current
                    677:                                // sizes of a hidden calibration text field.
                    678: 
                    679:                                RECT trec;
                    680: 
                    681:                                trec.right = 0;
                    682:                                trec.bottom = 0;
                    683: 
                    684:                                GetClientRect (GetDlgItem (hwndDlg, IDC_ASPECT_RATIO_CALIBRATION_BOX), &trec);
                    685: 
                    686:                                if (trec.right != 0 && trec.bottom != 0)
                    687:                                {
                    688:                                        // The size of the 282x282 IDC_ASPECT_RATIO_CALIBRATION_BOX rendered at the default DPI (96) is 423x458
                    689:                                        DPIScaleFactorX = (double) trec.right / 423;
                    690:                                        DPIScaleFactorY = (double) trec.bottom / 458;
                    691:                                        DlgAspectRatio = DPIScaleFactorX / DPIScaleFactorY;
                    692:                                }
                    693:                        }
                    694: 
                    695:                        EndDialog (hwndDlg, 0);
                    696:                        return 1;
                    697:                }
                    698: 
                    699:        case WM_CLOSE:
                    700:                EndDialog (hwndDlg, 0);
                    701:                return 1;
                    702:        }
                    703: 
                    704:        return 0;
1.1.1.11  root      705: }
                    706: 
                    707: 
1.1       root      708: /* Except in response to the WM_INITDIALOG message, the dialog box procedure
                    709:    should return nonzero if it processes the message, and zero if it does
                    710:    not. - see DialogProc */
1.1.1.12  root      711: BOOL CALLBACK
1.1       root      712: AboutDlgProc (HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam)
                    713: {
                    714:        WORD lw = LOWORD (wParam);
1.1.1.12  root      715:        static HBITMAP hbmTextualLogoBitmapRescaled = NULL;
                    716: 
1.1       root      717:        if (lParam);            /* remove warning */
                    718: 
                    719:        switch (msg)
                    720:        {
                    721:        case WM_INITDIALOG:
                    722:                {
1.1.1.7   root      723:                        char szTmp[100];
1.1.1.12  root      724:                        RECT rec;
                    725: 
                    726:                        LocalizeDialog (hwndDlg, "IDD_ABOUT_DLG");
1.1       root      727: 
1.1.1.12  root      728:                        // Hyperlink
1.1.1.8   root      729:                        SetWindowText (GetDlgItem (hwndDlg, IDC_HOMEPAGE), "www.truecrypt.org");
1.1.1.11  root      730:                        ToHyperlink (hwndDlg, IDC_HOMEPAGE);
1.1.1.5   root      731: 
1.1.1.12  root      732:                        // Logo area background (must not keep aspect ratio; must retain Windows-imposed distortion)
                    733:                        GetClientRect (GetDlgItem (hwndDlg, IDC_ABOUT_LOGO_AREA), &rec);
                    734:                        SetWindowPos (GetDlgItem (hwndDlg, IDC_ABOUT_BKG), HWND_TOP, 0, 0, rec.right, rec.bottom, SWP_NOMOVE);
                    735: 
                    736:                        // Resize the logo bitmap if the user has a non-default DPI 
                    737:                        if (ScreenDPI != USER_DEFAULT_SCREEN_DPI)
                    738:                        {
                    739:                                // Logo (must recreate and keep the original aspect ratio as Windows distorts it)
                    740:                                hbmTextualLogoBitmapRescaled = RenderBitmap (MAKEINTRESOURCE (IDB_TEXTUAL_LOGO_288DPI),
                    741:                                        GetDlgItem (hwndDlg, IDC_TEXTUAL_LOGO_IMG),
                    742:                                        0, 0, 0, 0, FALSE, TRUE);
                    743: 
                    744:                                SetWindowPos (GetDlgItem (hwndDlg, IDC_ABOUT_BKG), HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
                    745:                        }
                    746: 
1.1.1.5   root      747:                        // Version
                    748:                        SendMessage (GetDlgItem (hwndDlg, IDT_ABOUT_VERSION), WM_SETFONT, (WPARAM) hUserBoldFont, 0);
1.1       root      749:                        sprintf (szTmp, "TrueCrypt %s", VERSION_STRING);
1.1.1.7   root      750: #ifdef _DEBUG
                    751:                        strcat (szTmp, "  (debug)");
                    752: #endif
1.1       root      753:                        SetDlgItemText (hwndDlg, IDT_ABOUT_VERSION, szTmp);
1.1.1.5   root      754: 
                    755:                        // Credits
                    756:                        SendMessage (GetDlgItem (hwndDlg, IDC_ABOUT_CREDITS), WM_SETFONT, (WPARAM) hUserFont, (LPARAM) 0);
1.1.1.11  root      757:                        SendMessage (hwndDlg, WM_APP, 0, 0);
1.1       root      758:                        return 1;
                    759:                }
                    760: 
1.1.1.11  root      761:        case WM_APP:
                    762:                SetWindowText (GetDlgItem (hwndDlg, IDC_ABOUT_CREDITS),
1.1.1.12  root      763:                        "Portions of this software are based in part on the works of the following people: "
1.1.1.11  root      764:                        "Paul Le Roux, "
1.1.1.12  root      765:                        "Bruce Schneier, John Kelsey, Doug Whiting, David Wagner, Chris Hall, Niels Ferguson, "
                    766:                        "Lars Knudsen, Ross Anderson, Eli Biham, "
                    767:                        "Joan Daemen, Vincent Rijmen, "
1.1.1.13  root      768:                        "Phillip Rogaway, "
1.1.1.11  root      769:                        "Hans Dobbertin, Antoon Bosselaers, Bart Preneel, "
                    770:                        "Paulo Barreto, Brian Gladman, Wei Dai, Peter Gutmann, and many others.\r\n\r\n"
1.1.1.13  root      771: 
1.1.1.11  root      772:                        "Portions of this software:\r\n"
1.1.1.13  root      773:                        "Copyright \xA9 2003-2008 TrueCrypt Foundation. All Rights Reserved.\r\n"
1.1.1.11  root      774:                        "Copyright \xA9 1998-2000 Paul Le Roux. All Rights Reserved.\r\n"
1.1.1.13  root      775:                        "Copyright \xA9 1999-2006 Brian Gladman. All Rights Reserved.\r\n"
1.1.1.11  root      776:                        "Copyright \xA9 1995-1997 Eric Young. All Rights Reserved.\r\n"
                    777:                        "Copyright \xA9 2001 Markus Friedl. All Rights Reserved.\r\n\r\n"
1.1.1.13  root      778: 
                    779:                        "This software as a whole:\r\n"
                    780:                        "Copyright \xA9 2008 TrueCrypt Foundation. All rights reserved.\r\n\r\n"
                    781: 
1.1.1.11  root      782:                        "A TrueCrypt Foundation Release");
1.1.1.13  root      783: 
1.1.1.11  root      784:                return 1;
                    785: 
1.1       root      786:        case WM_COMMAND:
                    787:                if (lw == IDOK || lw == IDCANCEL)
                    788:                {
1.1.1.12  root      789:                        PostMessage (hwndDlg, WM_CLOSE, 0, 0);
1.1       root      790:                        return 1;
                    791:                }
                    792: 
1.1.1.5   root      793:                if (lw == IDC_HOMEPAGE)
                    794:                {
1.1.1.11  root      795:                        Applink ("main", TRUE, "");
1.1.1.5   root      796:                        return 1;
                    797:                }
                    798: 
1.1.1.11  root      799:                if (lw == IDC_DONATIONS)
                    800:                {
                    801:                        Applink ("donate", FALSE, "");
                    802:                        return 1;
                    803:                }
1.1.1.12  root      804: 
1.1.1.5   root      805:                // Disallow modification of credits
                    806:                if (HIWORD (wParam) == EN_UPDATE)
                    807:                {
1.1.1.11  root      808:                        SendMessage (hwndDlg, WM_APP, 0, 0);
1.1.1.5   root      809:                        return 1;
                    810:                }
                    811: 
1.1       root      812:                return 0;
                    813: 
                    814:        case WM_CLOSE:
1.1.1.12  root      815:                /* Delete buffered bitmaps (if any) */
                    816:                if (hbmTextualLogoBitmapRescaled != NULL)
                    817:                {
                    818:                        DeleteObject ((HGDIOBJ) hbmTextualLogoBitmapRescaled);
                    819:                        hbmTextualLogoBitmapRescaled = NULL;
                    820:                }
                    821: 
1.1       root      822:                EndDialog (hwndDlg, 0);
                    823:                return 1;
                    824:        }
                    825: 
                    826:        return 0;
                    827: }
                    828: 
                    829: 
                    830: BOOL
                    831: IsButtonChecked (HWND hButton)
                    832: {
                    833:        if (SendMessage (hButton, BM_GETCHECK, 0, 0) == BST_CHECKED)
                    834:                return TRUE;
                    835:        else
                    836:                return FALSE;
                    837: }
                    838: 
                    839: void
                    840: CheckButton (HWND hButton)
                    841: {
                    842:        SendMessage (hButton, BM_SETCHECK, BST_CHECKED, 0);
                    843: }
                    844: 
                    845: 
1.1.1.13  root      846: void LeftPadString (char *szTmp, int len, int targetLen, char filler)
                    847: {
                    848:        int i;
                    849: 
                    850:        if (targetLen <= len)
                    851:                return;
                    852: 
                    853:        for (i = targetLen-1; i >= (targetLen-len); i--)
                    854:                szTmp [i] = szTmp [i-(targetLen-len)];
                    855: 
                    856:        memset (szTmp, filler, targetLen-len);
                    857:        szTmp [targetLen] = 0;
                    858: }
                    859: 
                    860: 
1.1       root      861: /*****************************************************************************
                    862:   ToSBCS: converts a unicode string to Single Byte Character String (SBCS).
                    863:   ***************************************************************************/
                    864: 
                    865: void
                    866: ToSBCS (LPWSTR lpszText)
                    867: {
                    868:        int j = wcslen (lpszText);
                    869:        if (j == 0)
                    870:        {
                    871:                strcpy ((char *) lpszText, "");
                    872:                return;
                    873:        }
                    874:        else
                    875:        {
                    876:                char *lpszNewText = (char *) err_malloc (j + 1);
                    877:                j = WideCharToMultiByte (CP_ACP, 0L, lpszText, -1, lpszNewText, j + 1, NULL, NULL);
                    878:                if (j > 0)
                    879:                        strcpy ((char *) lpszText, lpszNewText);
                    880:                else
                    881:                        strcpy ((char *) lpszText, "");
                    882:                free (lpszNewText);
                    883:        }
                    884: }
                    885: 
                    886: /*****************************************************************************
                    887:   ToUNICODE: converts a SBCS string to a UNICODE string.
                    888:   ***************************************************************************/
                    889: 
                    890: void
                    891: ToUNICODE (char *lpszText)
                    892: {
                    893:        int j = strlen (lpszText);
                    894:        if (j == 0)
                    895:        {
                    896:                wcscpy ((LPWSTR) lpszText, (LPWSTR) WIDE (""));
                    897:                return;
                    898:        }
                    899:        else
                    900:        {
                    901:                LPWSTR lpszNewText = (LPWSTR) err_malloc ((j + 1) * 2);
                    902:                j = MultiByteToWideChar (CP_ACP, 0L, lpszText, -1, lpszNewText, j + 1);
                    903:                if (j > 0)
                    904:                        wcscpy ((LPWSTR) lpszText, lpszNewText);
                    905:                else
                    906:                        wcscpy ((LPWSTR) lpszText, (LPWSTR) "");
                    907:                free (lpszNewText);
                    908:        }
                    909: }
                    910: 
                    911: /* InitDialog - initialize the applications main dialog, this function should
                    912:    be called only once in the dialogs WM_INITDIALOG message handler */
                    913: void
                    914: InitDialog (HWND hwndDlg)
                    915: {
1.1.1.7   root      916:        NONCLIENTMETRICSW metric;
                    917:        static BOOL aboutMenuAppended = FALSE;
                    918: 
1.1       root      919:        int nHeight;
1.1.1.7   root      920:        LOGFONTW lf;
1.1       root      921:        HMENU hMenu;
1.1.1.7   root      922:        Font *font;
1.1       root      923: 
1.1.1.12  root      924:        /* Fonts */
1.1       root      925: 
1.1.1.7   root      926:        // Normal
                    927:        font = GetFont ("font_normal");
                    928: 
                    929:        metric.cbSize = sizeof (metric);
                    930:        SystemParametersInfoW (SPI_GETNONCLIENTMETRICS, sizeof(metric), &metric, 0);
                    931: 
1.1.1.12  root      932:        metric.lfMessageFont.lfHeight = CompensateDPIFont (!font ? -11 : -font->Size);
1.1.1.7   root      933:        metric.lfMessageFont.lfWidth = 0;
                    934: 
                    935:        if (font && wcscmp (font->FaceName, L"default") != 0)
1.1.1.11  root      936:        {
                    937:                wcsncpy ((WCHAR *)metric.lfMessageFont.lfFaceName, font->FaceName, sizeof (metric.lfMessageFont.lfFaceName)/2);
                    938:        }
                    939:        else if (nCurrentOS == WIN_VISTA_OR_LATER)
                    940:        {
                    941:                // Vista's new default font (size and spacing) breaks compatibility with Windows 2k/XP applications.
                    942:                // Force use of Tahoma (as Microsoft does in many dialogs) until a native Vista look is implemented.
                    943:                wcsncpy ((WCHAR *)metric.lfMessageFont.lfFaceName, L"Tahoma", sizeof (metric.lfMessageFont.lfFaceName)/2);
                    944:        }
1.1.1.7   root      945: 
                    946:        hUserFont = CreateFontIndirectW (&metric.lfMessageFont);
                    947: 
                    948:        metric.lfMessageFont.lfUnderline = TRUE;
                    949:        hUserUnderlineFont = CreateFontIndirectW (&metric.lfMessageFont);
                    950: 
                    951:        metric.lfMessageFont.lfUnderline = FALSE;
                    952:        metric.lfMessageFont.lfWeight = FW_BOLD;
                    953:        hUserBoldFont = CreateFontIndirectW (&metric.lfMessageFont);
                    954: 
                    955:        metric.lfMessageFont.lfUnderline = TRUE;
                    956:        metric.lfMessageFont.lfWeight = FW_BOLD;
                    957:        hUserUnderlineBoldFont = CreateFontIndirectW (&metric.lfMessageFont);
                    958: 
1.1.1.12  root      959:        // Fixed-size (hexadecimal digits)
                    960:        nHeight = CompensateDPIFont (-12);
1.1       root      961:        lf.lfHeight = nHeight;
                    962:        lf.lfWidth = 0;
                    963:        lf.lfEscapement = 0;
                    964:        lf.lfOrientation = 0;
1.1.1.7   root      965:        lf.lfWeight = FW_NORMAL;
1.1       root      966:        lf.lfItalic = FALSE;
                    967:        lf.lfUnderline = FALSE;
                    968:        lf.lfStrikeOut = FALSE;
                    969:        lf.lfCharSet = DEFAULT_CHARSET;
                    970:        lf.lfOutPrecision = OUT_DEFAULT_PRECIS;
                    971:        lf.lfClipPrecision = CLIP_DEFAULT_PRECIS;
                    972:        lf.lfQuality = PROOF_QUALITY;
                    973:        lf.lfPitchAndFamily = FF_DONTCARE;
1.1.1.7   root      974:        wcscpy (lf.lfFaceName, L"Courier New");
                    975:        hFixedDigitFont = CreateFontIndirectW (&lf);
                    976:        if (hFixedDigitFont == NULL)
1.1       root      977:        {
                    978:                handleWin32Error (hwndDlg);
1.1.1.7   root      979:                AbortProcess ("NOFONT");
1.1       root      980:        }
                    981: 
1.1.1.7   root      982:        // Bold
                    983:        font = GetFont ("font_bold");
1.1       root      984: 
1.1.1.12  root      985:        nHeight = CompensateDPIFont (!font ? -13 : -font->Size);
1.1       root      986:        lf.lfHeight = nHeight;
1.1.1.7   root      987:        lf.lfWeight = FW_BLACK;
                    988:        wcsncpy (lf.lfFaceName, !font ? L"Arial" : font->FaceName, sizeof (lf.lfFaceName)/2);
                    989:        hBoldFont = CreateFontIndirectW (&lf);
1.1       root      990:        if (hBoldFont == NULL)
                    991:        {
                    992:                handleWin32Error (hwndDlg);
1.1.1.7   root      993:                AbortProcess ("NOFONT");
1.1       root      994:        }
                    995: 
1.1.1.7   root      996:        // Title
                    997:        font = GetFont ("font_title");
                    998: 
1.1.1.12  root      999:        nHeight = CompensateDPIFont (!font ? -21 : -font->Size);
1.1       root     1000:        lf.lfHeight = nHeight;
                   1001:        lf.lfWeight = FW_REGULAR;
1.1.1.7   root     1002:        wcsncpy (lf.lfFaceName, !font ? L"Times New Roman" : font->FaceName, sizeof (lf.lfFaceName)/2);
                   1003:        hTitleFont = CreateFontIndirectW (&lf);
1.1       root     1004:        if (hTitleFont == NULL)
                   1005:        {
                   1006:                handleWin32Error (hwndDlg);
1.1.1.7   root     1007:                AbortProcess ("NOFONT");
1.1       root     1008:        }
                   1009: 
1.1.1.12  root     1010:        // Fixed-size
1.1.1.7   root     1011:        font = GetFont ("font_fixed");
                   1012: 
1.1.1.12  root     1013:        nHeight = CompensateDPIFont (!font ? -12 : -font->Size);
1.1       root     1014:        lf.lfHeight = nHeight;
                   1015:        lf.lfWidth = 0;
                   1016:        lf.lfEscapement = 0;
                   1017:        lf.lfOrientation = 0;
                   1018:        lf.lfWeight = FW_NORMAL;
                   1019:        lf.lfItalic = FALSE;
                   1020:        lf.lfUnderline = FALSE;
                   1021:        lf.lfStrikeOut = FALSE;
                   1022:        lf.lfCharSet = DEFAULT_CHARSET;
                   1023:        lf.lfOutPrecision = OUT_DEFAULT_PRECIS;
                   1024:        lf.lfClipPrecision = CLIP_DEFAULT_PRECIS;
                   1025:        lf.lfQuality = PROOF_QUALITY;
                   1026:        lf.lfPitchAndFamily = FF_DONTCARE;
1.1.1.7   root     1027:        wcsncpy (lf.lfFaceName, !font ? L"Lucida Console" : font->FaceName, sizeof (lf.lfFaceName)/2);
                   1028:        hFixedFont = CreateFontIndirectW (&lf);
1.1       root     1029:        if (hFixedFont == NULL)
                   1030:        {
                   1031:                handleWin32Error (hwndDlg);
1.1.1.7   root     1032:                AbortProcess ("NOFONT");
1.1       root     1033:        }
                   1034: 
1.1.1.7   root     1035:        if (!aboutMenuAppended)
                   1036:        {
                   1037:                hMenu = GetSystemMenu (hwndDlg, FALSE);
                   1038:                AppendMenu (hMenu, MF_SEPARATOR, 0, NULL);
                   1039:                AppendMenuW (hMenu, MF_ENABLED | MF_STRING, IDC_ABOUT, GetString ("ABOUTBOX"));
1.1.1.5   root     1040: 
1.1.1.7   root     1041:                aboutMenuAppended = TRUE;
                   1042:        }
1.1       root     1043: }
                   1044: 
1.1.1.12  root     1045: HDC CreateMemBitmap (HINSTANCE hInstance, HWND hwnd, char *resource)
1.1       root     1046: {
                   1047:        HBITMAP picture = LoadBitmap (hInstance, resource);
                   1048:        HDC viewDC = GetDC (hwnd), dcMem;
                   1049: 
                   1050:        dcMem = CreateCompatibleDC (viewDC);
                   1051: 
                   1052:        SetMapMode (dcMem, MM_TEXT);
                   1053: 
                   1054:        SelectObject (dcMem, picture);
                   1055: 
1.1.1.12  root     1056:        DeleteObject (picture);
                   1057: 
1.1       root     1058:        ReleaseDC (hwnd, viewDC);
                   1059: 
                   1060:        return dcMem;
                   1061: }
                   1062: 
1.1.1.12  root     1063: 
                   1064: /* Renders the specified bitmap at the specified location and stretches it to fit (anti-aliasing is applied). 
                   1065: If bDirectRender is FALSE and both nWidth and nHeight are zero, the width and height of hwndDest are
                   1066: retrieved and adjusted according to screen DPI (the width and height of the resultant image are adjusted the
                   1067: same way); furthermore, if bKeepAspectRatio is TRUE, the smaller DPI factor of the two (i.e. horiz. or vert.)
                   1068: is used both for horiz. and vert. scaling (note that the overall GUI aspect ratio changes irregularly in
                   1069: both directions depending on the DPI). If bDirectRender is TRUE, bKeepAspectRatio is ignored. 
                   1070: This function returns a handle to the scaled bitmap. When the bitmap is no longer needed, it should be
                   1071: deleted by calling DeleteObject() with the handle passed as the parameter. 
                   1072: Known Windows issues: 
                   1073: - For some reason, anti-aliasing is not applied if the source bitmap contains less than 16K pixels. 
                   1074: - Windows 2000 may produce slightly inaccurate colors even when source, buffer, and target are 24-bit true color. */
                   1075: HBITMAP RenderBitmap (char *resource, HWND hwndDest, int x, int y, int nWidth, int nHeight, BOOL bDirectRender, BOOL bKeepAspectRatio)
1.1       root     1076: {
1.1.1.12  root     1077:        LRESULT lResult = 0;
                   1078: 
                   1079:        HDC hdcSrc = CreateMemBitmap (hInst, hwndDest, resource);
                   1080: 
                   1081:        HGDIOBJ picture = GetCurrentObject (hdcSrc, OBJ_BITMAP);
1.1       root     1082: 
1.1.1.12  root     1083:        HBITMAP hbmpRescaled;
1.1       root     1084:        BITMAP bitmap;
1.1.1.12  root     1085: 
                   1086:        HDC hdcRescaled;
                   1087: 
                   1088:        if (!bDirectRender && nWidth == 0 && nHeight == 0)
                   1089:        {
                   1090:                RECT rec;
                   1091: 
                   1092:                GetClientRect (hwndDest, &rec);
                   1093: 
                   1094:                if (bKeepAspectRatio)
                   1095:                {
                   1096:                        if (DlgAspectRatio > 1)
                   1097:                        {
                   1098:                                // Do not fix this, it's correct. We use the Y scale factor intentionally for both
                   1099:                                // directions to maintain aspect ratio (see above for more info).
                   1100:                                nWidth = CompensateYDPI (rec.right);
                   1101:                                nHeight = CompensateYDPI (rec.bottom);
                   1102:                        }
                   1103:                        else
                   1104:                        {
                   1105:                                // Do not fix this, it's correct. We use the X scale factor intentionally for both
                   1106:                                // directions to maintain aspect ratio (see above for more info).
                   1107:                                nWidth = CompensateXDPI (rec.right);
                   1108:                                nHeight = CompensateXDPI (rec.bottom);
                   1109:                        }
                   1110:                }
                   1111:                else
                   1112:                {
                   1113:                        nWidth = CompensateXDPI (rec.right);
                   1114:                        nHeight = CompensateYDPI (rec.bottom);
                   1115:                }
                   1116:        }
                   1117: 
1.1       root     1118:        GetObject (picture, sizeof (BITMAP), &bitmap);
                   1119: 
1.1.1.12  root     1120:     hdcRescaled = CreateCompatibleDC (hdcSrc); 
                   1121:  
                   1122:     hbmpRescaled = CreateCompatibleBitmap (hdcSrc, nWidth, nHeight); 
                   1123:  
                   1124:     SelectObject (hdcRescaled, hbmpRescaled);
                   1125: 
                   1126:        /* Anti-aliasing mode (HALFTONE is the only anti-aliasing algorithm natively supported by Windows 2000.
                   1127:           TODO: GDI+ offers higher quality -- InterpolationModeHighQualityBicubic) */
                   1128:        SetStretchBltMode (hdcRescaled, HALFTONE);
                   1129: 
                   1130:        StretchBlt (hdcRescaled,
                   1131:                0,
                   1132:                0,
                   1133:                nWidth,
                   1134:                nHeight,
                   1135:                hdcSrc,
                   1136:                0,
                   1137:                0,
                   1138:                bitmap.bmWidth, 
                   1139:                bitmap.bmHeight,
                   1140:                SRCCOPY);
                   1141: 
                   1142:        DeleteDC (hdcSrc);
                   1143: 
                   1144:        if (bDirectRender)
                   1145:        {
                   1146:                HDC hdcDest = GetDC (hwndDest);
                   1147: 
                   1148:                BitBlt (hdcDest, x, y, nWidth, nHeight, hdcRescaled, 0, 0, SRCCOPY);
                   1149:                DeleteDC (hdcDest);
                   1150:        }
                   1151:        else
                   1152:        {
                   1153:                lResult = SendMessage (hwndDest, (UINT) STM_SETIMAGE, (WPARAM) IMAGE_BITMAP, (LPARAM) (HANDLE) hbmpRescaled);
                   1154:        }
                   1155: 
                   1156:        if ((HGDIOBJ) lResult != NULL && (HGDIOBJ) lResult != (HGDIOBJ) hbmpRescaled)
                   1157:                DeleteObject ((HGDIOBJ) lResult);
                   1158: 
                   1159:        DeleteDC (hdcRescaled);
                   1160: 
                   1161:        return hbmpRescaled;
1.1       root     1162: }
                   1163: 
1.1.1.7   root     1164: 
1.1       root     1165: LRESULT CALLBACK
1.1.1.7   root     1166: RedTick (HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1.1       root     1167: {
1.1.1.12  root     1168:        if (uMsg == WM_CREATE)
1.1.1.7   root     1169:        {
                   1170:        }
1.1.1.12  root     1171:        else if (uMsg == WM_DESTROY)
1.1.1.7   root     1172:        {
                   1173:        }
1.1.1.12  root     1174:        else if (uMsg == WM_TIMER)
                   1175:        {
                   1176:        }
                   1177:        else if (uMsg == WM_PAINT)
                   1178:        {
                   1179:                PAINTSTRUCT tmp;
                   1180:                HPEN hPen;
                   1181:                HDC hDC;
                   1182:                BOOL bEndPaint;
                   1183:                RECT Rect;
1.1.1.7   root     1184: 
1.1.1.12  root     1185:                if (GetUpdateRect (hwnd, NULL, FALSE))
                   1186:                {
                   1187:                        hDC = BeginPaint (hwnd, &tmp);
                   1188:                        bEndPaint = TRUE;
                   1189:                        if (hDC == NULL)
                   1190:                                return DefWindowProc (hwnd, uMsg, wParam, lParam);
                   1191:                }
                   1192:                else
                   1193:                {
                   1194:                        hDC = GetDC (hwnd);
                   1195:                        bEndPaint = FALSE;
                   1196:                }
1.1.1.7   root     1197: 
1.1.1.12  root     1198:                GetClientRect (hwnd, &Rect);
1.1       root     1199: 
1.1.1.12  root     1200:                hPen = CreatePen (PS_SOLID, 2, RGB (0, 255, 0));
                   1201:                if (hPen != NULL)
                   1202:                {
                   1203:                        HGDIOBJ hObj = SelectObject (hDC, hPen);
                   1204:                        WORD bx = LOWORD (GetDialogBaseUnits ());
                   1205:                        WORD by = HIWORD (GetDialogBaseUnits ());
1.1       root     1206: 
1.1.1.12  root     1207:                        MoveToEx (hDC, (Rect.right - Rect.left) / 2, Rect.bottom, NULL);
                   1208:                        LineTo (hDC, Rect.right, Rect.top);
                   1209:                        MoveToEx (hDC, (Rect.right - Rect.left) / 2, Rect.bottom, NULL);
1.1       root     1210: 
1.1.1.12  root     1211:                        LineTo (hDC, (3 * bx) / 4, (2 * by) / 8);
1.1.1.7   root     1212: 
1.1.1.12  root     1213:                        SelectObject (hDC, hObj);
                   1214:                        DeleteObject (hPen);
                   1215:                }
                   1216: 
                   1217:                if (bEndPaint)
                   1218:                        EndPaint (hwnd, &tmp);
                   1219:                else
                   1220:                        ReleaseDC (hwnd, hDC);
1.1       root     1221: 
1.1.1.12  root     1222:                return TRUE;
                   1223:        }
1.1.1.7   root     1224: 
1.1.1.12  root     1225:        return DefWindowProc (hwnd, uMsg, wParam, lParam);
1.1.1.7   root     1226: }
1.1       root     1227: 
1.1.1.7   root     1228: BOOL
                   1229: RegisterRedTick (HINSTANCE hInstance)
                   1230: {
                   1231:   WNDCLASS wc;
                   1232:   ULONG rc;
1.1       root     1233: 
1.1.1.7   root     1234:   memset(&wc, 0 , sizeof wc);
1.1       root     1235: 
1.1.1.7   root     1236:   wc.style = CS_HREDRAW | CS_VREDRAW;
                   1237:   wc.cbClsExtra = 0;
                   1238:   wc.cbWndExtra = 4;
                   1239:   wc.hInstance = hInstance;
                   1240:   wc.hIcon = LoadIcon (NULL, IDI_APPLICATION);
                   1241:   wc.hCursor = NULL;
                   1242:   wc.hbrBackground = GetStockObject (LTGRAY_BRUSH);
                   1243:   wc.lpszClassName = "REDTICK";
                   1244:   wc.lpfnWndProc = &RedTick; 
                   1245:   
                   1246:   rc = (ULONG) RegisterClass (&wc);
1.1       root     1247: 
1.1.1.7   root     1248:   return rc == 0 ? FALSE : TRUE;
                   1249: }
1.1       root     1250: 
1.1.1.7   root     1251: BOOL
                   1252: UnregisterRedTick (HINSTANCE hInstance)
                   1253: {
                   1254:   return UnregisterClass ("REDTICK", hInstance);
                   1255: }
1.1       root     1256: 
1.1.1.7   root     1257: LRESULT CALLBACK
                   1258: SplashDlgProc (HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
                   1259: {
1.1       root     1260:        return DefDlgProc (hwnd, uMsg, wParam, lParam);
                   1261: }
                   1262: 
                   1263: void
                   1264: WaitCursor ()
                   1265: {
                   1266:        static HCURSOR hcWait;
                   1267:        if (hcWait == NULL)
                   1268:                hcWait = LoadCursor (NULL, IDC_WAIT);
                   1269:        SetCursor (hcWait);
                   1270:        hCursor = hcWait;
                   1271: }
                   1272: 
                   1273: void
                   1274: NormalCursor ()
                   1275: {
                   1276:        static HCURSOR hcArrow;
                   1277:        if (hcArrow == NULL)
                   1278:                hcArrow = LoadCursor (NULL, IDC_ARROW);
                   1279:        SetCursor (hcArrow);
                   1280:        hCursor = NULL;
                   1281: }
                   1282: 
                   1283: void
                   1284: ArrowWaitCursor ()
                   1285: {
                   1286:        static HCURSOR hcArrowWait;
                   1287:        if (hcArrowWait == NULL)
                   1288:                hcArrowWait = LoadCursor (NULL, IDC_APPSTARTING);
                   1289:        SetCursor (hcArrowWait);
                   1290:        hCursor = hcArrowWait;
                   1291: }
1.1.1.11  root     1292: 
                   1293: void HandCursor ()
                   1294: {
                   1295:        static HCURSOR hcHand;
                   1296:        if (hcHand == NULL)
                   1297:                hcHand = LoadCursor (NULL, IDC_HAND);
                   1298:        SetCursor (hcHand);
                   1299:        hCursor = hcHand;
                   1300: }
                   1301: 
1.1.1.7   root     1302: void
                   1303: AddComboPair (HWND hComboBox, char *lpszItem, int value)
                   1304: {
                   1305:        LPARAM nIndex;
                   1306: 
                   1307:        nIndex = SendMessage (hComboBox, CB_ADDSTRING, 0, (LPARAM) lpszItem);
                   1308:        nIndex = SendMessage (hComboBox, CB_SETITEMDATA, nIndex, (LPARAM) value);
                   1309: }
                   1310: 
                   1311: void
                   1312: AddComboPairW (HWND hComboBox, wchar_t *lpszItem, int value)
                   1313: {
                   1314:        LPARAM nIndex;
                   1315: 
                   1316:        nIndex = SendMessageW (hComboBox, CB_ADDSTRING, 0, (LPARAM) lpszItem);
                   1317:        nIndex = SendMessage (hComboBox, CB_SETITEMDATA, nIndex, (LPARAM) value);
                   1318: }
                   1319: 
                   1320: void
                   1321: SelectAlgo (HWND hComboBox, int *algo_id)
                   1322: {
                   1323:        LPARAM nCount = SendMessage (hComboBox, CB_GETCOUNT, 0, 0);
                   1324:        LPARAM x, i;
                   1325: 
                   1326:        for (i = 0; i < nCount; i++)
                   1327:        {
                   1328:                x = SendMessage (hComboBox, CB_GETITEMDATA, i, 0);
1.1.1.13  root     1329:                if (x == (LPARAM) *algo_id)
1.1.1.7   root     1330:                {
                   1331:                        SendMessage (hComboBox, CB_SETCURSEL, i, 0);
                   1332:                        return;
                   1333:                }
                   1334:        }
                   1335: 
                   1336:        /* Something went wrong ; couldn't find the requested algo id so we drop
                   1337:           back to a default */
                   1338: 
                   1339:        *algo_id = SendMessage (hComboBox, CB_GETITEMDATA, 0, 0);
                   1340: 
                   1341:        SendMessage (hComboBox, CB_SETCURSEL, 0, 0);
                   1342: 
                   1343: }
1.1       root     1344: 
1.1.1.13  root     1345: void PopulateWipeModeCombo (HWND hComboBox, BOOL bNA)
                   1346: {
                   1347:        if (bNA)
                   1348:        {
                   1349:                AddComboPairW (hComboBox, GetString ("N_A_UISTR"), TC_WIPE_NONE);
                   1350:        }
                   1351:        else
                   1352:        {
                   1353:                AddComboPairW (hComboBox, GetString ("WIPE_MODE_NONE"), TC_WIPE_NONE);
                   1354:                AddComboPairW (hComboBox, GetString ("WIPE_MODE_3_DOD_5220"), TC_WIPE_3_DOD_5220);
                   1355:                AddComboPairW (hComboBox, GetString ("WIPE_MODE_7_DOD_5220"), TC_WIPE_7_DOD_5220);
                   1356:                AddComboPairW (hComboBox, GetString ("WIPE_MODE_35_GUTMANN"), TC_WIPE_35_GUTMANN);
                   1357:        }
                   1358: }
                   1359: 
1.1       root     1360: LRESULT CALLBACK
                   1361: CustomDlgProc (HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
                   1362: {
                   1363:        if (uMsg == WM_SETCURSOR && hCursor != NULL)
                   1364:        {
                   1365:                SetCursor (hCursor);
                   1366:                return TRUE;
                   1367:        }
                   1368: 
                   1369:        return DefDlgProc (hwnd, uMsg, wParam, lParam);
                   1370: }
                   1371: 
1.1.1.7   root     1372: 
1.1.1.11  root     1373: void ExceptionHandlerThread (void *ept)
1.1.1.9   root     1374: {
1.1.1.13  root     1375: #define MAX_RET_ADDR_COUNT 8
1.1.1.11  root     1376:        EXCEPTION_POINTERS *ep = (EXCEPTION_POINTERS *) ept;
1.1.1.10  root     1377:        DWORD addr, retAddr[MAX_RET_ADDR_COUNT];
                   1378:        DWORD exCode = ep->ExceptionRecord->ExceptionCode;
1.1.1.11  root     1379:        SYSTEM_INFO si;
                   1380:        wchar_t msg[8192];
1.1.1.12  root     1381:        char modPath[MAX_PATH];
                   1382:        int crc = 0;
1.1.1.11  root     1383:        char url[MAX_URL_LENGTH];
1.1.1.12  root     1384:        char lpack[128];
1.1.1.11  root     1385:        int i;
1.1.1.9   root     1386: 
1.1.1.10  root     1387:        addr = (DWORD) ep->ExceptionRecord->ExceptionAddress;
                   1388:        ZeroMemory (retAddr, sizeof (retAddr));
                   1389: 
1.1.1.12  root     1390:        switch (exCode)
1.1.1.10  root     1391:        {
1.1.1.12  root     1392:        case 0x80000003:
                   1393:        case 0x80000004:
                   1394:        case 0xc0000006:
                   1395:        case 0xc000001d:
                   1396:        case 0xc000001e:
                   1397:        case 0xc0000096:
                   1398:        case 0xeedfade:
1.1.1.10  root     1399:                // Exception not caused by TrueCrypt
1.1.1.12  root     1400:                MessageBoxW (0, GetString ("EXCEPTION_REPORT_EXT"),
1.1.1.10  root     1401:                        GetString ("EXCEPTION_REPORT_TITLE"),
                   1402:                        MB_ICONERROR | MB_OK | MB_SETFOREGROUND | MB_TOPMOST);
1.1.1.11  root     1403:                return;
1.1.1.10  root     1404: 
1.1.1.12  root     1405:        default:
1.1.1.10  root     1406:                {
1.1.1.12  root     1407:                        // Call stack
                   1408:                        PDWORD sp = (PDWORD) ep->ContextRecord->Esp, stackTop;
                   1409:                        int i = 0, e = 0;
                   1410:                        MEMORY_BASIC_INFORMATION mi;
                   1411: 
                   1412:                        VirtualQuery (sp, &mi, sizeof (mi));
                   1413:                        stackTop = (PDWORD)((char *)mi.BaseAddress + mi.RegionSize);
                   1414: 
                   1415:                        while (&sp[i] < stackTop && e < MAX_RET_ADDR_COUNT)
1.1.1.10  root     1416:                        {
1.1.1.12  root     1417:                                if (sp[i] > 0x400000 && sp[i] < 0x500000)
1.1.1.10  root     1418:                                {
1.1.1.12  root     1419:                                        int ee = 0;
                   1420: 
                   1421:                                        // Skip duplicates
                   1422:                                        while (ee < MAX_RET_ADDR_COUNT && retAddr[ee] != sp[i])
                   1423:                                                ee++;
                   1424:                                        if (ee != MAX_RET_ADDR_COUNT)
                   1425:                                        {
                   1426:                                                i++;
                   1427:                                                continue;
                   1428:                                        }
                   1429: 
                   1430:                                        retAddr[e++] = sp[i];
1.1.1.10  root     1431:                                }
1.1.1.12  root     1432:                                i++;
                   1433:                        }
                   1434:                }
                   1435:        }
1.1.1.10  root     1436: 
1.1.1.13  root     1437:        // Checksum of the module
1.1.1.12  root     1438:        if (GetModuleFileName (NULL, modPath, sizeof (modPath)))
                   1439:        {
                   1440:                HANDLE h = CreateFile (modPath, FILE_READ_DATA | FILE_READ_ATTRIBUTES, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
                   1441:                if (h != INVALID_HANDLE_VALUE)
                   1442:                {
                   1443:                        BY_HANDLE_FILE_INFORMATION fi;
                   1444:                        if (GetFileInformationByHandle (h, &fi))
                   1445:                        {
                   1446:                                char *buf = malloc (fi.nFileSizeLow);
                   1447:                                if (buf)
                   1448:                                {
                   1449:                                        DWORD bytesRead;
                   1450:                                        if (ReadFile (h, buf, fi.nFileSizeLow, &bytesRead, NULL) && bytesRead == fi.nFileSizeLow)
1.1.1.13  root     1451:                                                crc = GetCrc32 (buf, fi.nFileSizeLow);
1.1.1.12  root     1452:                                        free (buf);
                   1453:                                }
1.1.1.10  root     1454:                        }
1.1.1.12  root     1455:                        CloseHandle (h);
1.1.1.10  root     1456:                }
                   1457:        }
                   1458: 
1.1.1.11  root     1459:        GetSystemInfo (&si);
                   1460: 
1.1.1.12  root     1461:        if (LocalizationActive)
                   1462:                sprintf_s (lpack, sizeof (lpack), "&langpack=%s_%s", GetPreferredLangId (), GetActiveLangPackVersion ());
                   1463:        else
                   1464:                lpack[0] = 0;
                   1465: 
1.1.1.13  root     1466:        sprintf (url, TC_APPLINK_SECURE "&dest=err-report%s&os=Windows&osver=%d.%d.%d&arch=%s&cpus=%d&app=%s&cksum=%x&dlg=%s&err=%x&addr=%x"
1.1.1.12  root     1467:                , lpack
1.1.1.10  root     1468:                , CurrentOSMajor
                   1469:                , CurrentOSMinor
                   1470:                , CurrentOSServicePack
1.1.1.13  root     1471:                , Is64BitOs () ? "x64" : "x86"
1.1.1.11  root     1472:                , si.dwNumberOfProcessors
1.1.1.9   root     1473: #ifdef TCMOUNT
                   1474:                ,"main"
                   1475: #endif
                   1476: #ifdef VOLFORMAT
                   1477:                ,"format"
                   1478: #endif
                   1479: #ifdef SETUP
                   1480:                ,"setup"
                   1481: #endif
1.1.1.12  root     1482:                , crc
1.1.1.10  root     1483:                , LastDialogId ? LastDialogId : "-"
                   1484:                , exCode
                   1485:                , addr);
                   1486: 
                   1487:        for (i = 0; i < MAX_RET_ADDR_COUNT && retAddr[i]; i++)
1.1.1.11  root     1488:                sprintf (url + strlen(url), "&st%d=%x", i, retAddr[i]);
                   1489: 
                   1490:        swprintf (msg, GetString ("EXCEPTION_REPORT"), url);
1.1.1.9   root     1491: 
1.1.1.11  root     1492:        if (IDYES == MessageBoxW (0, msg, GetString ("EXCEPTION_REPORT_TITLE"), MB_ICONERROR | MB_YESNO | MB_DEFBUTTON1 | MB_SETFOREGROUND | MB_TOPMOST))
1.1.1.9   root     1493:                ShellExecute (NULL, "open", (LPCTSTR) url, NULL, NULL, SW_SHOWNORMAL);
                   1494:        else
                   1495:                UnhandledExceptionFilter (ep);
1.1.1.11  root     1496: }
                   1497: 
                   1498: 
                   1499: LONG __stdcall ExceptionHandler (EXCEPTION_POINTERS *ep)
                   1500: {
                   1501:        SetUnhandledExceptionFilter (NULL);
                   1502:        WaitForSingleObject ((HANDLE) _beginthread (ExceptionHandlerThread, 0, (void *)ep), INFINITE);
1.1.1.9   root     1503: 
                   1504:        return EXCEPTION_EXECUTE_HANDLER;
                   1505: }
                   1506: 
                   1507: 
1.1.1.11  root     1508: static LRESULT CALLBACK NonInstallUacWndProc (HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
                   1509: {
                   1510:        return DefWindowProc (hWnd, message, wParam, lParam);
                   1511: }
                   1512: 
                   1513: 
1.1.1.13  root     1514: // Mutex handling to prevent multiple instances of the wizard or main app from dealing with system encryption.
                   1515: // Returns TRUE if the mutex is (or had been) successfully acquired (otherwise FALSE). 
                   1516: BOOL CreateSysEncMutex (void)
                   1517: {
                   1518:        if (hSysEncMutex != NULL)
                   1519:                return TRUE;    // We already are the privileged instance
                   1520: 
                   1521:        hSysEncMutex = CreateMutex (NULL, TRUE, "Global\\TrueCrypt System Encryption Wizard");
                   1522:        if (hSysEncMutex == NULL)
                   1523:        {
                   1524:                // In multi-user configurations, the OS returns "Access is denied" here when a user attempts
                   1525:                // to acquire the mutex if another user already has.
                   1526:                handleWin32Error (NULL);
                   1527:                return FALSE;
                   1528:        }
                   1529: 
                   1530:        if (GetLastError () == ERROR_ALREADY_EXISTS)
                   1531:        {
                   1532:                ReleaseMutex (hSysEncMutex);
                   1533:                CloseHandle (hSysEncMutex);
                   1534: 
                   1535:                hSysEncMutex = NULL;
                   1536:                return FALSE;
                   1537:        }
                   1538: 
                   1539:        return TRUE;
                   1540: }
                   1541: 
                   1542: 
                   1543: // Mutex handling to prevent multiple instances of the wizard from dealing with system encryption
                   1544: void CloseSysEncMutex (void)
                   1545: {
                   1546:        if (hSysEncMutex != NULL)
                   1547:        {
                   1548:                if (ReleaseMutex (hSysEncMutex)
                   1549:                        && CloseHandle (hSysEncMutex))
                   1550:                        hSysEncMutex = NULL;
                   1551:        }
                   1552: }
                   1553: 
                   1554: BOOL LoadSysEncSettings (HWND hwndDlg)
                   1555: {
                   1556:        BOOL status = TRUE;
                   1557:        DWORD size = 0;
                   1558:        char *sysEncCfgFileBuf = LoadFile (GetConfigPath (FILE_SYSTEM_ENCRYPTION_CFG), &size);
                   1559:        char *xml = sysEncCfgFileBuf;
                   1560:        char paramName[100], paramVal[MAX_PATH];
                   1561: 
                   1562:        // Defaults
                   1563:        int newSystemEncryptionStatus = SYSENC_STATUS_NONE;
                   1564:        WipeAlgorithmId newnWipeMode = TC_WIPE_NONE;
                   1565: 
                   1566:        if (!FileExists (GetConfigPath (FILE_SYSTEM_ENCRYPTION_CFG)))
                   1567:        {
                   1568:                SystemEncryptionStatus = newSystemEncryptionStatus;
                   1569:                nWipeMode = newnWipeMode;
                   1570:        }
                   1571: 
                   1572:        if (xml == NULL)
                   1573:        {
                   1574:                return FALSE;
                   1575:        }
                   1576: 
                   1577:        while (xml = XmlFindElement (xml, "config"))
                   1578:        {
                   1579:                XmlGetAttributeText (xml, "key", paramName, sizeof (paramName));
                   1580:                XmlGetNodeText (xml, paramVal, sizeof (paramVal));
                   1581: 
                   1582:                if (strcmp (paramName, "SystemEncryptionStatus") == 0)
                   1583:                {
                   1584:                        newSystemEncryptionStatus = atoi (paramVal);
                   1585:                }
                   1586:                else if (strcmp (paramName, "WipeMode") == 0)
                   1587:                {
                   1588:                        newnWipeMode = (WipeAlgorithmId) atoi (paramVal);
                   1589:                }
                   1590: 
                   1591:                xml++;
                   1592:        }
                   1593: 
                   1594:        SystemEncryptionStatus = newSystemEncryptionStatus;
                   1595:        nWipeMode = newnWipeMode;
                   1596: 
                   1597:        free (sysEncCfgFileBuf);
                   1598:        return status;
                   1599: }
                   1600: 
1.1       root     1601: /* InitApp - initialize the application, this function is called once in the
                   1602:    applications WinMain function, but before the main dialog has been created */
1.1.1.13  root     1603: void InitApp (HINSTANCE hInstance, char *lpszCommandLine)
1.1       root     1604: {
                   1605:        WNDCLASS wc;
                   1606:        OSVERSIONINFO os;
1.1.1.7   root     1607:        char langId[6];
1.1       root     1608: 
                   1609:        /* Save the instance handle for later */
                   1610:        hInst = hInstance;
                   1611: 
                   1612:        /* Pull down the windows version */
                   1613:        os.dwOSVersionInfoSize = sizeof (OSVERSIONINFO);
1.1.1.7   root     1614: 
1.1       root     1615:        if (GetVersionEx (&os) == FALSE)
1.1.1.7   root     1616:                AbortProcess ("NO_OS_VER");
                   1617: 
                   1618:        CurrentOSMajor = os.dwMajorVersion;
                   1619:        CurrentOSMinor = os.dwMinorVersion;
                   1620: 
                   1621:        if (os.dwPlatformId == VER_PLATFORM_WIN32_NT && CurrentOSMajor == 5 && CurrentOSMinor == 0)
                   1622:                nCurrentOS = WIN_2000;
                   1623:        else if (os.dwPlatformId == VER_PLATFORM_WIN32_NT && CurrentOSMajor == 5 && CurrentOSMinor == 1)
                   1624:                nCurrentOS = WIN_XP;
                   1625:        else if (os.dwPlatformId == VER_PLATFORM_WIN32_NT && CurrentOSMajor == 5 && CurrentOSMinor == 2)
1.1.1.11  root     1626:        {
                   1627:                OSVERSIONINFOEX osEx;
                   1628: 
                   1629:                osEx.dwOSVersionInfoSize = sizeof (OSVERSIONINFOEX);
                   1630:                GetVersionEx ((LPOSVERSIONINFOA) &osEx);
                   1631: 
                   1632:                if (osEx.wProductType == VER_NT_SERVER || osEx.wProductType == VER_NT_DOMAIN_CONTROLLER)
                   1633:                        nCurrentOS = WIN_SERVER_2003;
                   1634:                else
                   1635:                        nCurrentOS = WIN_XP64;
                   1636:        }
                   1637:        else if (os.dwPlatformId == VER_PLATFORM_WIN32_NT && CurrentOSMajor >= 6)
                   1638:                nCurrentOS = WIN_VISTA_OR_LATER;
1.1.1.7   root     1639:        else if (os.dwPlatformId == VER_PLATFORM_WIN32_NT && CurrentOSMajor == 4)
                   1640:                nCurrentOS = WIN_NT4;
1.1       root     1641:        else if (os.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS && os.dwMajorVersion == 4 && os.dwMinorVersion == 0)
                   1642:                nCurrentOS = WIN_95;
                   1643:        else if (os.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS && os.dwMajorVersion == 4 && os.dwMinorVersion == 10)
                   1644:                nCurrentOS = WIN_98;
1.1.1.7   root     1645:        else if (os.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS && os.dwMajorVersion == 4 && os.dwMinorVersion == 90)
                   1646:                nCurrentOS = WIN_ME;
                   1647:        else if (os.dwPlatformId == VER_PLATFORM_WIN32s)
                   1648:                nCurrentOS = WIN_31;
                   1649:        else
                   1650:                nCurrentOS = WIN_UNKNOWN;
1.1       root     1651: 
1.1.1.11  root     1652:        CoInitialize (NULL);
                   1653: 
                   1654:        langId[0] = 0;
                   1655:        SetPreferredLangId (ConfigReadString ("Language", "", langId, sizeof (langId)));
                   1656:        
                   1657:        if (langId[0] == 0)
                   1658:                DialogBoxParamW (hInst, MAKEINTRESOURCEW (IDD_LANGUAGE), NULL,
                   1659:                        (DLGPROC) LanguageDlgProc, (LPARAM) 1);
                   1660: 
                   1661:        LoadLanguageFile ();
                   1662: 
                   1663: #ifndef SETUP
1.1.1.13  root     1664:        // UAC elevation moniker cannot be used in traveler mode.
1.1.1.11  root     1665:        // A new instance of the application must be created with elevated privileges.
                   1666:        if (IsNonInstallMode () && !IsAdmin () && IsUacSupported ())
                   1667:        {
                   1668:                char modPath[MAX_PATH], newCmdLine[4096];
                   1669:                WNDCLASSEX wcex;
                   1670:                HWND hWnd;
                   1671: 
                   1672:                if (strstr (lpszCommandLine, "/q UAC ") == lpszCommandLine)
                   1673:                {
                   1674:                        Error ("UAC_INIT_ERROR");
                   1675:                        exit (1);
                   1676:                }
                   1677: 
                   1678:                memset (&wcex, 0, sizeof (wcex));
                   1679:                wcex.cbSize = sizeof(WNDCLASSEX); 
                   1680:                wcex.lpfnWndProc = (WNDPROC) NonInstallUacWndProc;
                   1681:                wcex.hInstance = hInstance;
                   1682:                wcex.lpszClassName = "TrueCrypt";
                   1683:                RegisterClassEx (&wcex);
                   1684: 
                   1685:                // A small transparent window is necessary to bring the new instance to foreground
                   1686:                hWnd = CreateWindowEx (WS_EX_TOOLWINDOW | WS_EX_LAYERED,
                   1687:                        "TrueCrypt", "TrueCrypt", 0,
                   1688:                        GetSystemMetrics (SM_CXSCREEN)/2,
                   1689:                        GetSystemMetrics (SM_CYSCREEN)/2,
                   1690:                        1, 1, NULL, NULL, hInstance, NULL);
                   1691: 
                   1692:                SetLayeredWindowAttributes (hWnd, 0, 0, LWA_ALPHA);
                   1693:                ShowWindow (hWnd, SW_SHOWNORMAL);
                   1694: 
                   1695:                GetModuleFileName (NULL, modPath, sizeof (modPath));
                   1696: 
                   1697:                strcpy (newCmdLine, "/q UAC ");
                   1698:                strcat_s (newCmdLine, sizeof (newCmdLine), lpszCommandLine);
                   1699: 
                   1700:                if ((int)ShellExecute (hWnd, "runas", modPath, newCmdLine, NULL, SW_SHOWNORMAL) <= 32)
                   1701:                        exit (1);
                   1702: 
                   1703:                Sleep (2000);
                   1704:                exit (0);
                   1705:        }
                   1706: #endif
                   1707: 
                   1708:        SetUnhandledExceptionFilter (ExceptionHandler);
                   1709: 
1.1.1.10  root     1710:        RemoteSession = GetSystemMetrics (SM_REMOTESESSION) != 0;
                   1711: 
1.1.1.6   root     1712:        // OS version check
                   1713:        if (CurrentOSMajor < 5)
                   1714:        {
1.1.1.7   root     1715:                MessageBoxW (NULL, GetString ("UNSUPPORTED_OS"), lpszTitle, MB_ICONSTOP);
1.1.1.6   root     1716:                exit (1);
                   1717:        }
1.1.1.7   root     1718:        else
                   1719:        {
                   1720:                OSVERSIONINFOEX osEx;
                   1721: 
1.1.1.12  root     1722:                // Service pack check & warnings about critical MS issues
1.1.1.7   root     1723:                osEx.dwOSVersionInfoSize = sizeof (OSVERSIONINFOEX);
                   1724:                if (GetVersionEx ((LPOSVERSIONINFOA) &osEx) != 0)
                   1725:                {
1.1.1.10  root     1726:                        CurrentOSServicePack = osEx.wServicePackMajor;
1.1.1.7   root     1727:                        switch (nCurrentOS)
                   1728:                        {
                   1729:                        case WIN_2000:
                   1730:                                if (osEx.wServicePackMajor < 3)
                   1731:                                        Warning ("LARGE_IDE_WARNING_2K");
1.1.1.12  root     1732:                                else
                   1733:                                {
                   1734:                                        DWORD val = 0, size = sizeof(val);
                   1735:                                        HKEY hkey;
                   1736: 
                   1737:                                        if (RegOpenKeyEx (HKEY_LOCAL_MACHINE, "SYSTEM\\CurrentControlSet\\Services\\Atapi\\Parameters", 0, KEY_READ, &hkey) == ERROR_SUCCESS
                   1738:                                                && (RegQueryValueEx (hkey, "EnableBigLba", 0, 0, (LPBYTE) &val, &size) != ERROR_SUCCESS
                   1739:                                                || val != 1))
                   1740: 
                   1741:                                        {
                   1742:                                                Warning ("LARGE_IDE_WARNING_2K_REGISTRY");
                   1743:                                        }
                   1744:                                        RegCloseKey (hkey);
                   1745:                                }
1.1.1.7   root     1746:                                break;
1.1.1.12  root     1747: 
1.1.1.7   root     1748:                        case WIN_XP:
                   1749:                                if (osEx.wServicePackMajor < 1)
1.1.1.11  root     1750:                                {
                   1751:                                        HKEY k;
                   1752:                                        // PE environment does not report version of SP
                   1753:                                        if (RegOpenKeyEx (HKEY_LOCAL_MACHINE, "System\\CurrentControlSet\\Control\\minint", 0, KEY_READ, &k) != ERROR_SUCCESS)
                   1754:                                                Warning ("LARGE_IDE_WARNING_XP");
                   1755:                                        else
                   1756:                                                RegCloseKey (k);
                   1757:                                }
1.1.1.7   root     1758:                                break;
                   1759:                        }
                   1760:                }
1.1.1.11  root     1761: 
                   1762: #ifndef SETUP
                   1763:                if (CurrentOSMajor == 6 && CurrentOSMinor == 0 && osEx.dwBuildNumber < 6000)
                   1764:                {
                   1765:                        Error ("UNSUPPORTED_BETA_OS");
                   1766:                        exit (0);
                   1767:                }
                   1768: #endif
1.1.1.7   root     1769:        }
1.1       root     1770: 
                   1771:        /* Get the attributes for the standard dialog class */
                   1772:        if ((GetClassInfo (hInst, WINDOWS_DIALOG_CLASS, &wc)) == 0)
1.1.1.7   root     1773:                AbortProcess ("INIT_REGISTER");
1.1       root     1774: 
                   1775: #ifndef SETUP
                   1776:        wc.hIcon = LoadIcon (hInstance, MAKEINTRESOURCE (IDI_TRUECRYPT_ICON));
                   1777: #else
                   1778: #include "../setup/resource.h"
                   1779:        wc.hIcon = LoadIcon (hInstance, MAKEINTRESOURCE (IDI_SETUP));
                   1780: #endif
                   1781:        wc.lpszClassName = TC_DLG_CLASS;
                   1782:        wc.lpfnWndProc = &CustomDlgProc;
                   1783:        wc.hCursor = LoadCursor (NULL, IDC_ARROW);
                   1784:        wc.cbWndExtra = DLGWINDOWEXTRA;
                   1785: 
                   1786:        hDlgClass = RegisterClass (&wc);
                   1787:        if (hDlgClass == 0)
1.1.1.7   root     1788:                AbortProcess ("INIT_REGISTER");
1.1       root     1789: 
                   1790:        wc.lpszClassName = TC_SPLASH_CLASS;
                   1791:        wc.lpfnWndProc = &SplashDlgProc;
                   1792:        wc.hCursor = LoadCursor (NULL, IDC_ARROW);
                   1793:        wc.cbWndExtra = DLGWINDOWEXTRA;
                   1794: 
                   1795:        hSplashClass = RegisterClass (&wc);
                   1796:        if (hSplashClass == 0)
1.1.1.7   root     1797:                AbortProcess ("INIT_REGISTER");
                   1798: 
1.1.1.12  root     1799:        // DPI and GUI aspect ratio
                   1800:        DialogBoxParamW (hInst, MAKEINTRESOURCEW (IDD_AUXILIARY_DLG), NULL,
                   1801:                (DLGPROC) AuxiliaryDlgProc, (LPARAM) 1);
                   1802: 
1.1.1.7   root     1803:        InitHelpFileName ();
                   1804: }
                   1805: 
                   1806: void InitHelpFileName (void)
                   1807: {
                   1808:        char *lpszTmp;
1.1       root     1809: 
                   1810:        GetModuleFileName (NULL, szHelpFile, sizeof (szHelpFile));
                   1811:        lpszTmp = strrchr (szHelpFile, '\\');
                   1812:        if (lpszTmp)
                   1813:        {
1.1.1.7   root     1814:                char szTemp[TC_MAX_PATH];
1.1       root     1815: 
1.1.1.7   root     1816:                // Primary file name
                   1817:                if (strcmp (GetPreferredLangId(), "en") == 0
                   1818:                        || GetPreferredLangId() == NULL)
1.1       root     1819:                {
1.1.1.7   root     1820:                        strcpy (++lpszTmp, "TrueCrypt User Guide.pdf");
1.1       root     1821:                }
                   1822:                else
1.1.1.7   root     1823:                {
                   1824:                        sprintf (szTemp, "TrueCrypt User Guide.%s.pdf", GetPreferredLangId());
                   1825:                        strcpy (++lpszTmp, szTemp);
                   1826:                }
                   1827: 
                   1828:                // Secondary file name (used when localized documentation is not found).
                   1829:                GetModuleFileName (NULL, szHelpFile2, sizeof (szHelpFile2));
                   1830:                lpszTmp = strrchr (szHelpFile2, '\\');
                   1831:                if (lpszTmp)
                   1832:                {
                   1833:                        strcpy (++lpszTmp, "TrueCrypt User Guide.pdf");
                   1834:                }
1.1       root     1835:        }
                   1836: }
                   1837: 
                   1838: BOOL
                   1839: OpenDevice (char *lpszPath, OPEN_TEST_STRUCT * driver)
                   1840: {
                   1841:        DWORD dwResult;
                   1842:        BOOL bResult;
                   1843: 
                   1844:        strcpy ((char *) &driver->wszFileName[0], lpszPath);
1.1.1.7   root     1845:        ToUNICODE ((char *) &driver->wszFileName[0]);
1.1       root     1846: 
1.1.1.13  root     1847:        bResult = DeviceIoControl (hDriver, TC_IOCTL_OPEN_TEST,
1.1       root     1848:                                   driver, sizeof (OPEN_TEST_STRUCT),
1.1.1.7   root     1849:                                   NULL, 0,
1.1       root     1850:                                   &dwResult, NULL);
                   1851: 
                   1852:        if (bResult == FALSE)
                   1853:        {
                   1854:                dwResult = GetLastError ();
1.1.1.7   root     1855: 
1.1       root     1856:                if (dwResult == ERROR_SHARING_VIOLATION)
                   1857:                        return TRUE;
                   1858:                else
                   1859:                        return FALSE;
                   1860:        }
1.1.1.7   root     1861:                
                   1862:        return TRUE;
1.1       root     1863: }
                   1864: 
                   1865: 
1.1.1.11  root     1866: BOOL GetDriveLabel (int driveNo, wchar_t *label, int labelSize)
                   1867: {
                   1868:        DWORD fileSystemFlags;
                   1869:        wchar_t root[] = { L'A' + driveNo, L':', L'\\', 0 };
                   1870: 
                   1871:        return GetVolumeInformationW (root, label, labelSize / 2, NULL, NULL, &fileSystemFlags, NULL, 0);
                   1872: }
                   1873: 
                   1874: 
1.1.1.13  root     1875: // This function also populates SysPartitionDevicePath and SysDriveDevicePath for later use.
                   1876: int GetAvailableFixedDisks (HWND hComboBox, char *lpszRootPath)
1.1       root     1877: {
                   1878:        int i, n;
                   1879:        int line = 0;
1.1.1.5   root     1880:        LVITEM LvItem;
1.1.1.6   root     1881:        __int64 deviceSize = 0;
1.1       root     1882: 
                   1883:        for (i = 0; i < 64; i++)
                   1884:        {
                   1885:                BOOL drivePresent = FALSE;
1.1.1.6   root     1886:                BOOL removable = FALSE;
1.1       root     1887: 
1.1.1.11  root     1888:                LvItem.lParam = 0;
                   1889: 
1.1.1.5   root     1890:                for (n = 0; n <= 32; n++)
1.1       root     1891:                {
1.1.1.7   root     1892:                        char szTmp[TC_MAX_PATH];
1.1.1.12  root     1893:                        wchar_t size[100] = {0};
1.1       root     1894:                        OPEN_TEST_STRUCT driver;
                   1895: 
                   1896:                        sprintf (szTmp, lpszRootPath, i, n);
1.1.1.13  root     1897: 
1.1.1.7   root     1898:                        if (OpenDevice (szTmp, &driver))
1.1       root     1899:                        {
                   1900:                                BOOL bResult;
                   1901:                                PARTITION_INFORMATION diskInfo;
1.1.1.6   root     1902:                                DISK_GEOMETRY driveInfo;
1.1       root     1903: 
                   1904:                                drivePresent = TRUE;
1.1.1.11  root     1905:                                bResult = GetPartitionInfo (szTmp, &diskInfo);
1.1       root     1906: 
1.1.1.7   root     1907:                                // Test if device is removable
1.1.1.11  root     1908:                                if (n == 0 && GetDriveGeometry (szTmp, &driveInfo))
1.1.1.7   root     1909:                                        removable = driveInfo.MediaType == RemovableMedia;
1.1       root     1910: 
1.1.1.7   root     1911:                                if (bResult)
                   1912:                                {
1.1.1.6   root     1913: 
1.1.1.7   root     1914:                                        // System creates a virtual partition1 for some storage devices without
                   1915:                                        // partition table. We try to detect this case by comparing sizes of
                   1916:                                        // partition0 and partition1. If they match, no partition of the device
                   1917:                                        // is displayed to the user to avoid confusion. Drive letter assigned by
                   1918:                                        // system to partition1 is displayed as subitem of partition0
1.1.1.6   root     1919: 
1.1.1.7   root     1920:                                        if (n == 1 && diskInfo.PartitionLength.QuadPart == deviceSize)
                   1921:                                        {
                   1922:                                                char drive[] = { 0, ':', 0 };
                   1923:                                                char device[MAX_PATH * 2];
                   1924:                                                int driveNo;
                   1925: 
                   1926:                                                // Drive letter
                   1927:                                                strcpy (device, szTmp);
                   1928:                                                ToUNICODE (device);
                   1929:                                                driveNo = GetDiskDeviceDriveLetter ((PWSTR) device);
                   1930:                                                drive[0] = driveNo == -1 ? 0 : 'A' + driveNo;
1.1.1.11  root     1931:                                                
1.1.1.7   root     1932:                                                LvItem.iSubItem = 1;
                   1933:                                                LvItem.pszText = drive;
1.1.1.12  root     1934:                                                LvItem.mask = LVIF_TEXT;
1.1.1.7   root     1935:                                                SendMessage (hComboBox,LVM_SETITEM,0,(LPARAM)&LvItem);
1.1       root     1936: 
1.1.1.11  root     1937:                                                // Label                                
                   1938:                                                if (driveNo != -1)
                   1939:                                                {
                   1940:                                                        wchar_t name[64];
                   1941: 
                   1942:                                                        if (GetDriveLabel (driveNo, name, sizeof (name)))
                   1943:                                                                ListSubItemSetW (hComboBox, LvItem.iItem, 3, name);
                   1944:                                                }
                   1945: 
                   1946:                                                // Mark the device as containing a virtual partition
                   1947:                                                LvItem.iSubItem = 0;
                   1948:                                                LvItem.mask = LVIF_PARAM;
                   1949:                                                LvItem.lParam |= SELDEVFLAG_VIRTUAL_PARTITION;
                   1950:                                                SendMessage (hComboBox, LVM_SETITEM, 0, (LPARAM) &LvItem);
                   1951: 
1.1.1.7   root     1952:                                                break;
                   1953:                                        }
1.1       root     1954: 
1.1.1.7   root     1955:                                        GetSizeString (diskInfo.PartitionLength.QuadPart, size);
1.1.1.6   root     1956:                                }
1.1.1.5   root     1957: 
1.1.1.7   root     1958: 
1.1.1.5   root     1959:                                memset (&LvItem,0,sizeof(LvItem));
                   1960:                                LvItem.mask = LVIF_TEXT;   
                   1961:                                LvItem.iItem = line++;   
                   1962: 
                   1963:                                // Device Name
1.1.1.7   root     1964:                                if (n == 0)
                   1965:                                {
                   1966:                                        wchar_t s[1024];
                   1967:                                        deviceSize = diskInfo.PartitionLength.QuadPart;
1.1.1.12  root     1968: 
                   1969:                                        if (removable)
                   1970:                                                wsprintfW (s, L"Harddisk %d (%s):", i, GetString ("REMOVABLE"));
                   1971:                                        else
                   1972:                                                wsprintfW (s, L"Harddisk %d:", i);
                   1973: 
1.1.1.7   root     1974:                                        ListItemAddW (hComboBox, LvItem.iItem, s);
                   1975:                                }
                   1976:                                else
                   1977:                                {
                   1978:                                        LvItem.pszText = szTmp;
                   1979:                                        SendMessage (hComboBox,LVM_INSERTITEM,0,(LPARAM)&LvItem);
                   1980:                                }
1.1.1.5   root     1981: 
                   1982:                                // Size
1.1.1.11  root     1983:                                ListSubItemSetW (hComboBox, LvItem.iItem, 2, size);
1.1.1.5   root     1984: 
1.1.1.6   root     1985:                                // Device type removable
1.1.1.12  root     1986:                                if (removable)
1.1.1.6   root     1987:                                {
1.1.1.12  root     1988:                                        // Mark as removable
                   1989:                                        LvItem.iSubItem = 0;
                   1990:                                        LvItem.mask = LVIF_PARAM;
                   1991:                                        LvItem.lParam |= SELDEVFLAG_REMOVABLE_HOST_DEVICE;
                   1992:                                        SendMessage (hComboBox, LVM_SETITEM, 0, (LPARAM) &LvItem);
                   1993: 
                   1994:                                        LvItem.mask = LVIF_TEXT;   
1.1.1.6   root     1995:                                }
                   1996: 
1.1.1.5   root     1997:                                if (n > 0)
                   1998:                                {
                   1999:                                        char drive[] = { 0, ':', 0 };
                   2000:                                        char device[MAX_PATH * 2];
                   2001:                                        int driveNo;
                   2002: 
                   2003:                                        // Drive letter
                   2004:                                        strcpy (device, szTmp);
                   2005:                                        ToUNICODE (device);
                   2006:                                        driveNo = GetDiskDeviceDriveLetter ((PWSTR) device);
                   2007:                                        drive[0] = driveNo == -1 ? 0 : 'A' + driveNo;
                   2008: 
1.1.1.13  root     2009:                                        if (driveNo != -1
                   2010:                                                && GetSystemDriveLetter() == 'A' + driveNo)
                   2011:                                        {
                   2012:                                                // Mark the partition as the system partition
                   2013: 
                   2014:                                                LvItem.iSubItem = 0;
                   2015:                                                LvItem.mask = LVIF_PARAM;
                   2016:                                                LvItem.lParam |= SELDEVFLAG_SYSTEM_PARTITION;
                   2017:                                                SendMessage (hComboBox, LVM_SETITEM, 0, (LPARAM) &LvItem);
                   2018: 
                   2019:                                                LvItem.mask = LVIF_TEXT; 
                   2020: 
                   2021:                                                // Store the device path of the system partition for later use (to save time significantly)
                   2022:                                                sprintf (SysPartitionDevicePath, lpszRootPath, i, n);
                   2023: 
                   2024:                                                // Find the line of the drive containing this partition
                   2025:                                                {
                   2026:                                                        char tmpDevicePath [TC_MAX_PATH];
                   2027:                                                        char *ptrTmpDevicePath = tmpDevicePath;
                   2028:                                                        LVITEM tmpLvItem;
                   2029: 
                   2030:                                                        memset (&tmpLvItem, 0, sizeof(tmpLvItem));
                   2031: 
                   2032:                                                        tmpLvItem.mask = LVIF_TEXT | LVIF_PARAM;   
                   2033:                                                        tmpLvItem.pszText = ptrTmpDevicePath;
                   2034:                                                        tmpLvItem.cchTextMax = TC_MAX_PATH;
                   2035: 
                   2036:                                                        for (tmpLvItem.iItem = LvItem.iItem - 1;
                   2037:                                                                tmpLvItem.iItem >= 0
                   2038:                                                                && tmpLvItem.iItem >= LvItem.iItem - n;
                   2039:                                                        tmpLvItem.iItem--)
                   2040:                                                        {
                   2041:                                                                SendMessage (hComboBox, LVM_GETITEM, tmpLvItem.iItem, (LPARAM) &tmpLvItem);
                   2042: 
                   2043:                                                                if (ptrTmpDevicePath[0] == 'H')
                   2044:                                                                {
                   2045:                                                                        if (sscanf (ptrTmpDevicePath, "Harddisk %d", &i) == 1)
                   2046:                                                                        {
                   2047:                                                                                // Mark the drive as a system drive
                   2048:                                                                                tmpLvItem.iSubItem = 0;
                   2049:                                                                                tmpLvItem.mask = LVIF_TEXT | LVIF_PARAM;
                   2050:                                                                                tmpLvItem.lParam |= SELDEVFLAG_SYSTEM_DRIVE;
                   2051:                                                                                SendMessage (hComboBox, LVM_SETITEM, 0, (LPARAM) &tmpLvItem);
                   2052: 
                   2053:                                                                                // Store the device path of the system drive for later use (to save time significantly)
                   2054:                                                                                sprintf (SysDriveDevicePath, lpszRootPath, i, 0);
                   2055: 
                   2056:                                                                                if (bRawDevicesDlgProcInstantExit
                   2057:                                                                                        && strlen (SysPartitionDevicePath) > 1)
                   2058:                                                                                {
                   2059:                                                                                        bCachedSysDevicePathsValid = TRUE;
                   2060:                                                                                        return 1;
                   2061:                                                                                }
                   2062: 
                   2063:                                                                                break;
                   2064:                                                                        }
                   2065:                                                                }
                   2066:                                                        }
                   2067:                                                }
                   2068:                                        }
                   2069: 
1.1.1.5   root     2070:                                        LvItem.iSubItem = 1;
                   2071:                                        LvItem.pszText = drive;
1.1.1.12  root     2072:                                        LvItem.mask = LVIF_TEXT;   
1.1.1.5   root     2073:                                        SendMessage (hComboBox,LVM_SETITEM,0,(LPARAM)&LvItem);
                   2074: 
1.1.1.11  root     2075:                                        // Label                                
                   2076:                                        if (driveNo != -1)
                   2077:                                        {
                   2078:                                                wchar_t name[64];
                   2079: 
                   2080:                                                if (GetDriveLabel (driveNo, name, sizeof (name)))
                   2081:                                                        ListSubItemSetW (hComboBox, LvItem.iItem, 3, name);
                   2082:                                        }
1.1.1.5   root     2083:                                }
1.1       root     2084: 
1.1.1.11  root     2085:                                if (n == 1)
1.1.1.5   root     2086:                                {
1.1.1.13  root     2087:                                        // Mark the device as containing a partition
                   2088:                                        {
                   2089:                                                // Retrieve the whole-device item data so that we preserve its existing flags in LvItem.lParam
1.1.1.12  root     2090: 
1.1.1.13  root     2091:                                                char tmpDevicePath [TC_MAX_PATH];
                   2092:                                                char *ptrTmpDevicePath = tmpDevicePath;
                   2093:                                                LVITEM tmpLvItem;
                   2094: 
                   2095:                                                memset (&tmpLvItem, 0, sizeof(tmpLvItem));
                   2096: 
                   2097:                                                tmpLvItem.iSubItem = 0;
                   2098:                                                tmpLvItem.iItem = line - 2;
                   2099:                                                tmpLvItem.mask = LVIF_TEXT | LVIF_PARAM;   
                   2100:                                                tmpLvItem.pszText = ptrTmpDevicePath;
                   2101:                                                tmpLvItem.cchTextMax = sizeof(tmpDevicePath);
                   2102: 
                   2103:                                                SendMessage (hComboBox, LVM_GETITEM, tmpLvItem.iItem, (LPARAM) &tmpLvItem);
                   2104: 
                   2105:                                                if (ptrTmpDevicePath[0] == 'H')
                   2106:                                                {
                   2107:                                                        if (sscanf (ptrTmpDevicePath, "Harddisk %d", &i) == 1)
                   2108:                                                        {
                   2109:                                                                tmpLvItem.iSubItem = 0;
                   2110:                                                                tmpLvItem.mask = LVIF_TEXT | LVIF_PARAM;
                   2111: 
                   2112:                                                                // Mark the device as containing partition preserving existing flags
                   2113:                                                                tmpLvItem.lParam |= SELDEVFLAG_CONTAINS_PARTITIONS;
                   2114:                                                                SendMessage (hComboBox, LVM_SETITEM, 0, (LPARAM) &tmpLvItem);
                   2115:                                                        }
                   2116:                                                }
                   2117:                                        }
1.1.1.5   root     2118:                                }
                   2119:                        }
1.1.1.6   root     2120:                        else if (n == 0)
                   2121:                                break;
1.1.1.2   root     2122:                }
                   2123: 
1.1.1.5   root     2124:                if (drivePresent)
1.1.1.2   root     2125:                {
1.1.1.5   root     2126:                        memset (&LvItem,0,sizeof(LvItem));
                   2127:                        LvItem.mask = LVIF_TEXT;   
                   2128:                        LvItem.iItem = line++;   
1.1.1.2   root     2129: 
1.1.1.5   root     2130:                        LvItem.pszText = "";
                   2131:                        SendMessage (hComboBox,LVM_INSERTITEM,0,(LPARAM)&LvItem);
1.1       root     2132:                }
                   2133:        }
                   2134: 
                   2135:        i = SendMessage (hComboBox, LVM_GETITEMCOUNT, 0, 0);
                   2136:        if (i != CB_ERR)
                   2137:                return i;
                   2138:        else
                   2139:                return 0;
                   2140: }
                   2141: 
1.1.1.13  root     2142: int GetAvailableRemovables (HWND hComboBox, char *lpszRootPath)
1.1       root     2143: {
                   2144:        char szTmp[TC_MAX_PATH];
                   2145:        int i;
                   2146:        LVITEM LvItem;
                   2147: 
                   2148:        if (lpszRootPath);      /* Remove unused parameter warning */
                   2149: 
1.1.1.5   root     2150:        memset (&LvItem,0,sizeof(LvItem));
1.1       root     2151:        LvItem.mask = LVIF_TEXT;   
                   2152:        LvItem.iItem = SendMessage (hComboBox, LVM_GETITEMCOUNT, 0, 0)+1;   
                   2153: 
1.1.1.11  root     2154:        if (QueryDosDevice ("A:", szTmp, sizeof (szTmp)) != 0 && GetDriveType ("A:\\") == DRIVE_REMOVABLE)
1.1       root     2155:        {
1.1.1.5   root     2156:                LvItem.pszText = "\\Device\\Floppy0";
                   2157:                LvItem.iItem = SendMessage (hComboBox,LVM_INSERTITEM,0,(LPARAM)&LvItem);
                   2158: 
                   2159:                LvItem.iSubItem = 1;
                   2160:                LvItem.pszText = "A:";
                   2161:                SendMessage (hComboBox,LVM_SETITEM,0,(LPARAM)&LvItem);
                   2162: 
1.1       root     2163:        }
1.1.1.11  root     2164:        if (QueryDosDevice ("B:", szTmp, sizeof (szTmp)) != 0 && GetDriveType ("B:\\") == DRIVE_REMOVABLE)
1.1       root     2165:        {
1.1.1.5   root     2166:                LvItem.pszText = "\\Device\\Floppy1";
                   2167:                LvItem.iSubItem = 0;
                   2168:                LvItem.iItem = SendMessage (hComboBox, LVM_GETITEMCOUNT, 0, 0)+1;   
                   2169:                LvItem.iItem = SendMessage (hComboBox,LVM_INSERTITEM,0,(LPARAM)&LvItem);
                   2170: 
                   2171:                LvItem.iSubItem = 1;
                   2172:                LvItem.pszText = "B:";
                   2173:                SendMessage (hComboBox,LVM_SETITEM,0,(LPARAM)&LvItem);
1.1       root     2174:        }
                   2175: 
                   2176:        i = SendMessage (hComboBox, LVM_GETITEMCOUNT, 0, 0);
                   2177:        if (i != CB_ERR)
                   2178:                return i;
                   2179:        else
                   2180:                return 0;
                   2181: }
                   2182: 
1.1.1.13  root     2183: /* Stores the device path of the system partition in SysPartitionDevicePath and the device path of the system drive
                   2184: in SysDriveDevicePath. 
                   2185: IMPORTANT: As this may take a very long time if called for the first time, it should be called only before performing 
                   2186:            a dangerous operation (such as header backup restore or formatting a supposedly non-system device) never 
                   2187:                   at WM_INITDIALOG or any other GUI events -- instead call IsSystemDevicePath (path, hwndDlg, FALSE) for 
                   2188:                   very fast preliminary GUI checks; also note that right after the "Select Device" dialog exits with an OK 
                   2189:                   return code, you can use the global flags bSysPartitionSelected and bSysDriveSelected to see if the user
                   2190:                   selected the system partition/device.
                   2191: After this function completes successfully, the results are cached for the rest of the session and repeated
                   2192: executions complete very fast. Returns TRUE if successful (otherwise FALSE). */
                   2193: BOOL GetSysDevicePaths (HWND hwndDlg)
                   2194: {
                   2195:        if (!bCachedSysDevicePathsValid
                   2196:                || strlen (SysPartitionDevicePath) <= 1 
                   2197:                || strlen (SysDriveDevicePath) <= 1)
                   2198:        {
                   2199:                int nResult;
                   2200:                char tmp [TC_MAX_PATH];
                   2201: 
                   2202:                bRawDevicesDlgProcInstantExit = TRUE;
                   2203: 
                   2204:                nResult = DialogBoxParamW (hInst, MAKEINTRESOURCEW (IDD_RAWDEVICES_DLG), hwndDlg,
                   2205:                        (DLGPROC) RawDevicesDlgProc, (LPARAM) & tmp[0]);
                   2206: 
                   2207:                bRawDevicesDlgProcInstantExit = FALSE;
                   2208:        }
                   2209: 
                   2210:        return (bCachedSysDevicePathsValid 
                   2211:                && strlen (SysPartitionDevicePath) > 1 
                   2212:                && strlen (SysDriveDevicePath) > 1);
                   2213: }
                   2214: 
                   2215: /* Determines whether the device path is the path of the system partition or of the system drive. 
                   2216: If bReliableRequired is TRUE, very fast execution is guaranteed, but the results cannot be relied upon. 
                   2217: If it's FALSE and the function is called for the first time, execution may take up to one minute but the
                   2218: results are reliable.
                   2219: IMPORTANT: As the execution may take a very long time if called for the first time with bReliableRequired set
                   2220:            to TRUE, it should be called with bReliableRequired set to TRUE only before performing a dangerous
                   2221:                   operation (such as header backup restore or formatting a supposedly non-system device) never at 
                   2222:                   WM_INITDIALOG or any other GUI events (use IsSystemDevicePath(path, hwndDlg, FALSE) for fast 
                   2223:                   preliminary GUI checks; also note that right after the "Select Device" dialog exits with an OK 
                   2224:                   return code, you can use the global flags bSysPartitionSelected and bSysDriveSelected to see if the
                   2225:                   user selected the system partition/device).
                   2226: After this function completes successfully, the results are cached for the rest of the session, bReliableRequired
                   2227: is ignored (TRUE implied), repeated executions complete very fast, and the results are always reliable. 
                   2228: Return codes:
                   2229: 1  - it is the system partition path (e.g. \Device\Harddisk0\Partition1)
                   2230: 2  - it is the system drive path (e.g. \Device\Harddisk0\Partition0)
                   2231: 0  - it's not the system partition/drive path
                   2232: -1 - the result can't be determined, isn't reliable, or there was an error. */
                   2233: int IsSystemDevicePath (char *path, HWND hwndDlg, BOOL bReliableRequired)
                   2234: {
                   2235:        if (!bCachedSysDevicePathsValid
                   2236:                && bReliableRequired)
                   2237:        {
                   2238:                if (!GetSysDevicePaths (hwndDlg))
                   2239:                        return -1;
                   2240:        }
                   2241: 
                   2242:        if (strlen (SysPartitionDevicePath) <= 1 || strlen (SysDriveDevicePath) <= 1)
                   2243:                return -1;
                   2244: 
                   2245:        if (strncmp (path, SysPartitionDevicePath, max (strlen(path), strlen(SysPartitionDevicePath))) == 0)
                   2246:                return 1;
                   2247:        else if (strncmp (path, SysDriveDevicePath, max (strlen(path), strlen(SysDriveDevicePath))) == 0)
                   2248:                return 2;
                   2249: 
                   2250:        return 0;
                   2251: }
                   2252: 
                   2253: BOOL TextInfoDialogBox (int nID)
                   2254: {
                   2255:        return DialogBoxParamW (hInst, MAKEINTRESOURCEW (IDD_TEXT_INFO_DIALOG_BOX_DLG), MainDlg, (DLGPROC) TextInfoDialogBoxDlgProc, (LPARAM) nID);
                   2256: }
                   2257: 
                   2258: BOOL CALLBACK TextInfoDialogBoxDlgProc (HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam)
1.1       root     2259: {
                   2260:        WORD lw = LOWORD (wParam);
1.1.1.13  root     2261:        static int nID = 0;
1.1       root     2262: 
                   2263:        switch (msg)
                   2264:        {
                   2265:        case WM_INITDIALOG:
                   2266:                {
1.1.1.13  root     2267:                        nID = (int) lParam;
1.1.1.11  root     2268: 
1.1.1.13  root     2269:                        ShowWindow(GetDlgItem(hwndDlg, IDC_PRINT), SW_HIDE);
                   2270: 
                   2271:                        switch (nID)
1.1.1.11  root     2272:                        {
1.1.1.13  root     2273:                        case TC_TBXID_LEGAL_NOTICES:
                   2274:                                LocalizeDialog (hwndDlg, "LEGAL_NOTICES_DLG_TITLE");
                   2275:                                break;
                   2276: 
                   2277:                        case TC_TBXID_SYS_ENCRYPTION_PRETEST:
                   2278:                                LocalizeDialog (hwndDlg, NULL);
                   2279:                                ShowWindow(GetDlgItem(hwndDlg, IDC_PRINT), SW_SHOW);
                   2280:                                break;
                   2281: 
                   2282:                        case TC_TBXID_SYS_ENC_RESCUE_DISK:
                   2283:                                LocalizeDialog (hwndDlg, NULL);
                   2284:                                ShowWindow(GetDlgItem(hwndDlg, IDC_PRINT), SW_SHOW);
                   2285:                                break;
1.1.1.11  root     2286:                        }
1.1.1.13  root     2287: 
                   2288:                        SendMessage (hwndDlg, TC_APPMSG_LOAD_TEXT_BOX_CONTENT, 0, 0);
1.1.1.7   root     2289:                }
1.1.1.13  root     2290:                return 0;
1.1       root     2291: 
1.1.1.7   root     2292:        case WM_COMMAND:
                   2293:                if (lw == IDOK || lw == IDCANCEL)
                   2294:                {
1.1.1.13  root     2295:                        NormalCursor ();
1.1.1.7   root     2296:                        EndDialog (hwndDlg, 0);
                   2297:                        return 1;
                   2298:                }
1.1       root     2299: 
1.1.1.13  root     2300:                if (lw == IDC_PRINT)
                   2301:                {
                   2302:                        switch (nID)
                   2303:                        {
                   2304:                        case TC_TBXID_SYS_ENCRYPTION_PRETEST:
                   2305:                                PrintHardCopyTextUTF16 (GetString ("SYS_ENCRYPTION_PRETEST_INFO2"), "Pre-Boot Troubleshooting", wcslen (GetString ("SYS_ENCRYPTION_PRETEST_INFO2")) * 2);
                   2306:                                break;
                   2307: 
                   2308:                        case TC_TBXID_SYS_ENC_RESCUE_DISK:
                   2309:                                PrintHardCopyTextUTF16 (GetString ("RESCUE_DISK_HELP"), "TrueCrypt Rescue Disk Help", wcslen (GetString ("RESCUE_DISK_HELP")) * 2);
                   2310:                                break;
                   2311:                        }
                   2312:                        return 1;
                   2313:                }
                   2314: 
1.1.1.7   root     2315:                // Disallow modification
                   2316:                if (HIWORD (wParam) == EN_UPDATE)
                   2317:                {
1.1.1.13  root     2318:                        SendMessage (hwndDlg, TC_APPMSG_LOAD_TEXT_BOX_CONTENT, 0, 0);
1.1.1.7   root     2319:                        return 1;
                   2320:                }
                   2321: 
                   2322:                return 0;
                   2323: 
1.1.1.13  root     2324:        case TC_APPMSG_LOAD_TEXT_BOX_CONTENT:
                   2325:                {
                   2326:                        char *r = NULL;
                   2327: 
                   2328:                        switch (nID)
                   2329:                        {
                   2330:                        case TC_TBXID_LEGAL_NOTICES:
                   2331:                                LocalizeDialog (hwndDlg, "LEGAL_NOTICES_DLG_TITLE");
                   2332:                                r = GetLegalNotices ();
                   2333:                                if (r != NULL)
                   2334:                                {
                   2335:                                        SetWindowText (GetDlgItem (hwndDlg, IDC_INFO_BOX_TEXT), r);
                   2336:                                        free (r);
                   2337:                                }
                   2338:                                break;
                   2339: 
                   2340:                        case TC_TBXID_SYS_ENCRYPTION_PRETEST:
                   2341:                                LocalizeDialog (hwndDlg, NULL);
                   2342:                                SetWindowTextW (GetDlgItem (hwndDlg, IDC_INFO_BOX_TEXT), GetString ("SYS_ENCRYPTION_PRETEST_INFO2"));
                   2343:                                break;
                   2344: 
                   2345:                        case TC_TBXID_SYS_ENC_RESCUE_DISK:
                   2346:                                LocalizeDialog (hwndDlg, NULL);
                   2347:                                SetWindowTextW (GetDlgItem (hwndDlg, IDC_INFO_BOX_TEXT), GetString ("RESCUE_DISK_HELP"));
                   2348:                                break;
                   2349:                        }
                   2350:                }
                   2351:                return 1;
                   2352: 
1.1.1.7   root     2353:        case WM_CLOSE:
1.1.1.13  root     2354:                NormalCursor ();
1.1.1.7   root     2355:                EndDialog (hwndDlg, 0);
                   2356:                return 1;
                   2357:        }
                   2358: 
                   2359:        return 0;
                   2360: }
                   2361: 
                   2362: 
                   2363: char * GetLegalNotices ()
                   2364: {
                   2365:        static char *resource;
1.1.1.11  root     2366:        static DWORD size;
                   2367:        char *buf;
1.1.1.7   root     2368: 
                   2369:        if (resource == NULL)
1.1.1.11  root     2370:                resource = MapResource ("Text", IDR_LICENSE, &size);
1.1.1.7   root     2371: 
1.1.1.11  root     2372:        if (resource != NULL)
                   2373:        {
                   2374:                buf = malloc (size + 1);
                   2375:                if (buf != NULL)
                   2376:                {
                   2377:                        memcpy (buf, resource, size);
                   2378:                        buf[size] = 0;
                   2379:                }
                   2380:        }
                   2381: 
                   2382:        return buf;
1.1.1.7   root     2383: }
                   2384: 
                   2385: 
1.1.1.12  root     2386: BOOL CALLBACK
1.1.1.7   root     2387: RawDevicesDlgProc (HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam)
                   2388: {
1.1.1.13  root     2389:        static char *lpszFileName;              // This is actually a pointer to a GLOBAL array
1.1.1.7   root     2390:        WORD lw = LOWORD (wParam);
                   2391: 
                   2392:        if (lParam);            /* remove warning */
                   2393: 
                   2394:        switch (msg)
                   2395:        {
                   2396:        case WM_INITDIALOG:
                   2397:                {
                   2398:                        int nCount;
                   2399:                        LVCOLUMNW LvCol;
                   2400:                        HWND hList = GetDlgItem (hwndDlg, IDC_DEVICELIST);
                   2401: 
                   2402:                        LocalizeDialog (hwndDlg, "IDD_RAWDEVICES_DLG");
                   2403: 
                   2404:                        SendMessage (hList,LVM_SETEXTENDEDLISTVIEWSTYLE,0,
                   2405:                                LVS_EX_FULLROWSELECT|LVS_EX_HEADERDRAGDROP|LVS_EX_TWOCLICKACTIVATE|LVS_EX_LABELTIP 
                   2406:                                ); 
                   2407: 
                   2408:                        memset (&LvCol,0,sizeof(LvCol));               
                   2409:                        LvCol.mask = LVCF_TEXT|LVCF_WIDTH|LVCF_SUBITEM|LVCF_FMT;  
                   2410:                        LvCol.pszText = GetString ("DEVICE");
1.1.1.12  root     2411:                        LvCol.cx = CompensateXDPI (186);
1.1.1.7   root     2412:                        LvCol.fmt = LVCFMT_LEFT;
                   2413:                        SendMessage (hList,LVM_INSERTCOLUMNW,0,(LPARAM)&LvCol);
1.1.1.5   root     2414: 
1.1.1.7   root     2415:                        LvCol.pszText = GetString ("DRIVE");  
1.1.1.13  root     2416:                        LvCol.cx = CompensateXDPI (38);
1.1.1.11  root     2417:                        LvCol.fmt = LVCFMT_LEFT;
1.1.1.7   root     2418:                        SendMessage (hList,LVM_INSERTCOLUMNW,1,(LPARAM)&LvCol);
1.1       root     2419: 
1.1.1.13  root     2420:                        LvCol.pszText = GetString ("SIZE");
                   2421:                        LvCol.cx = CompensateXDPI (64);
1.1       root     2422:                        LvCol.fmt = LVCFMT_RIGHT;
1.1.1.7   root     2423:                        SendMessage (hList,LVM_INSERTCOLUMNW,2,(LPARAM)&LvCol);
1.1       root     2424: 
1.1.1.13  root     2425:                        LvCol.pszText = GetString ("VOLUME_LABEL");
                   2426:                        LvCol.cx = CompensateXDPI (128);
1.1       root     2427:                        LvCol.fmt = LVCFMT_LEFT;
1.1.1.7   root     2428:                        SendMessage (hList,LVM_INSERTCOLUMNW,3,(LPARAM)&LvCol);
1.1       root     2429: 
                   2430:                        nCount = GetAvailableFixedDisks (hList, "\\Device\\Harddisk%d\\Partition%d");
1.1.1.13  root     2431: 
                   2432:                        if (bRawDevicesDlgProcInstantExit)
                   2433:                        {
                   2434:                                EndDialog (hwndDlg, IDCANCEL);
                   2435:                                return 1;
                   2436:                        }
                   2437: 
1.1       root     2438:                        nCount += GetAvailableRemovables (hList, "\\Device\\Floppy%d");
                   2439: 
                   2440:                        if (nCount == 0)
                   2441:                        {
                   2442:                                handleWin32Error (hwndDlg);
1.1.1.7   root     2443:                                MessageBoxW (hwndDlg, GetString ("RAWDEVICES"), lpszTitle, ICON_HAND);
1.1.1.13  root     2444:                                NormalCursor ();
1.1       root     2445:                                EndDialog (hwndDlg, IDCANCEL);
                   2446:                        }
                   2447: 
                   2448:                        lpszFileName = (char *) lParam;
1.1.1.12  root     2449: 
                   2450: #ifdef VOLFORMAT
                   2451:                        EnableWindow (GetDlgItem (hwndDlg, IDOK), FALSE);
                   2452: #endif
1.1       root     2453:                        return 1;
                   2454:                }
                   2455: 
                   2456:        case WM_COMMAND:
                   2457:        case WM_NOTIFY:
                   2458:                // catch non-device line selected
                   2459:                if (msg == WM_NOTIFY && ((LPNMHDR) lParam)->code == LVN_ITEMCHANGED && (((LPNMLISTVIEW) lParam)->uNewState & LVIS_FOCUSED ))
                   2460:                {
                   2461:                        LVITEM LvItem;
                   2462:                        memset(&LvItem,0,sizeof(LvItem));
1.1.1.11  root     2463:                        LvItem.mask = LVIF_TEXT | LVIF_PARAM;   
1.1       root     2464:                        LvItem.iItem = ((LPNMLISTVIEW) lParam)->iItem;
                   2465:                        LvItem.pszText = lpszFileName;
                   2466:                        LvItem.cchTextMax = TC_MAX_PATH;
                   2467: 
1.1.1.11  root     2468:                        SendMessage (GetDlgItem (hwndDlg, IDC_DEVICELIST), LVM_GETITEM, LvItem.iItem, (LPARAM) &LvItem);
1.1       root     2469:                        EnableWindow (GetDlgItem ((HWND) hwndDlg, IDOK), lpszFileName[0] != 0 && lpszFileName[0] != ' ');
1.1.1.11  root     2470: 
1.1       root     2471:                        return 1;
                   2472:                }
                   2473: 
                   2474:                if (msg == WM_COMMAND && lw == IDOK || msg == WM_NOTIFY && ((NMHDR *)lParam)->code == LVN_ITEMACTIVATE)
                   2475:                {
                   2476:                        LVITEM LvItem;
1.1.1.5   root     2477:                        memset (&LvItem,0,sizeof(LvItem));
1.1.1.11  root     2478:                        LvItem.mask = LVIF_TEXT | LVIF_PARAM;   
1.1       root     2479:                        LvItem.iItem =  SendMessage (GetDlgItem (hwndDlg, IDC_DEVICELIST), LVM_GETSELECTIONMARK, 0, 0);
                   2480:                        LvItem.pszText = lpszFileName;
                   2481:                        LvItem.cchTextMax = TC_MAX_PATH;
                   2482: 
1.1.1.13  root     2483:                        SendMessage (GetDlgItem (hwndDlg, IDC_DEVICELIST), LVM_GETITEM, LvItem.iItem, (LPARAM) &LvItem);
                   2484: 
1.1.1.12  root     2485:                        if (lpszFileName[0] == 0)
                   2486:                                return 1; // non-device line selected
                   2487: 
                   2488: #ifdef VOLFORMAT
1.1.1.13  root     2489:                        if (LvItem.lParam & SELDEVFLAG_SYSTEM_PARTITION)
1.1.1.12  root     2490:                        {
1.1.1.13  root     2491:                                if (WizardMode != WIZARD_MODE_SYS_DEVICE)
                   2492:                                {
                   2493:                                        if (AskYesNo ("CONFIRM_SYSTEM_ENCRYPTION_MODE") == IDNO)
                   2494:                                        {
                   2495:                                                EndDialog (hwndDlg, IDCANCEL);
                   2496:                                                return 1;
                   2497:                                        }
                   2498: 
                   2499:                                        bSysPartitionSelected = TRUE;
                   2500:                                        bSysDriveSelected = FALSE;
                   2501:                                        lpszFileName[0] = 0;
                   2502:                                        SwitchWizardToSysEncMode ();
                   2503: 
                   2504:                                        NormalCursor ();
                   2505:                                        EndDialog (hwndDlg, IDOK);
                   2506:                                        return 1;
                   2507:                                }
                   2508:                                else
                   2509:                                {
                   2510:                                        // This should never be the case because the Select Device dialog is not available in this wizard mode
                   2511:                                        bSysPartitionSelected = TRUE;
                   2512:                                        bSysDriveSelected = FALSE;
                   2513:                                        lpszFileName[0] = 0;
                   2514:                                        SwitchWizardToSysEncMode ();
                   2515:                                        NormalCursor ();
                   2516:                                        EndDialog (hwndDlg, IDCANCEL);
                   2517:                                        return 1;
                   2518:                                }
1.1.1.12  root     2519:                        }
                   2520: 
1.1.1.13  root     2521:                        if (!(LvItem.lParam & SELDEVFLAG_SYSTEM_DRIVE))
                   2522:                        {
                   2523:                                if (bWarnDeviceFormatAdvanced
                   2524:                                        && !bHiddenVolDirect
                   2525:                                        && AskWarnNoYes("FORMAT_DEVICE_FOR_ADVANCED_ONLY") == IDNO)
                   2526:                                {
                   2527:                                        if (AskNoYes("CONFIRM_CHANGE_WIZARD_MODE_TO_FILE_CONTAINER") == IDYES)
                   2528:                                        {
                   2529:                                                SwitchWizardToFileContainerMode ();
                   2530:                                        }
                   2531:                                        EndDialog (hwndDlg, IDCANCEL);
                   2532:                                        return 1;
                   2533:                                }
1.1.1.12  root     2534: 
1.1.1.13  root     2535:                                if (!bHiddenVolDirect)
                   2536:                                        bWarnDeviceFormatAdvanced = FALSE;
                   2537:                        }
                   2538: 
                   2539: #else  // #ifdef VOLFORMAT
                   2540: 
                   2541:                        bSysPartitionSelected = LvItem.lParam & SELDEVFLAG_SYSTEM_PARTITION;
                   2542:                        bSysDriveSelected = FALSE;
                   2543: 
                   2544: #endif // #ifdef VOLFORMAT
1.1       root     2545: 
1.1.1.5   root     2546:                        if (lpszFileName[0] == 'H')
                   2547:                        {
                   2548:                                // Whole device selected
                   2549:                                int driveNo;
                   2550: 
1.1.1.11  root     2551:                                if (sscanf (lpszFileName, "Harddisk %d", &driveNo) != 1)
                   2552:                                {
                   2553:                                        EnableWindow (GetDlgItem (hwndDlg, IDOK), FALSE);
                   2554:                                        return 1;
                   2555:                                }
                   2556: 
1.1.1.5   root     2557: #ifdef VOLFORMAT
1.1.1.13  root     2558:                                if (LvItem.lParam & SELDEVFLAG_SYSTEM_DRIVE)
                   2559:                                {
                   2560:                                        if (WizardMode != WIZARD_MODE_SYS_DEVICE)
                   2561:                                        {
                   2562:                                                if (AskYesNo ("CONFIRM_SYSTEM_ENCRYPTION_MODE") == IDNO)
                   2563:                                                {
                   2564:                                                        NormalCursor ();
                   2565:                                                        EndDialog (hwndDlg, IDCANCEL);
                   2566:                                                        return 1;
                   2567:                                                }
                   2568: 
                   2569:                                                bSysDriveSelected = TRUE;
                   2570:                                                bSysPartitionSelected = FALSE;
                   2571:                                                lpszFileName[0] = 0;
                   2572:                                                SwitchWizardToSysEncMode ();
                   2573: 
                   2574:                                                NormalCursor ();
                   2575:                                                EndDialog (hwndDlg, IDOK);
                   2576:                                                return 1;
                   2577:                                        }
                   2578:                                        else
                   2579:                                        {
                   2580:                                                // This should never be the case because the Select Device dialog is not available in this wizard mode
                   2581:                                                bSysDriveSelected = TRUE;
                   2582:                                                bSysPartitionSelected = FALSE;
                   2583:                                                lpszFileName[0] = 0;
                   2584:                                                SwitchWizardToSysEncMode ();
                   2585:                                                NormalCursor ();
                   2586:                                                EndDialog (hwndDlg, IDCANCEL);
                   2587:                                                return 1;
                   2588:                                        }
                   2589:                                }
                   2590: 
                   2591:                                // Disallow format if the device contains partitions, but not if the partition is virtual or system 
1.1.1.11  root     2592:                                if (!(LvItem.lParam & SELDEVFLAG_VIRTUAL_PARTITION)
                   2593:                                        && !bHiddenVolDirect)
1.1.1.5   root     2594:                                {
1.1.1.11  root     2595:                                        if (LvItem.lParam & SELDEVFLAG_CONTAINS_PARTITIONS)
1.1.1.7   root     2596:                                        {
1.1.1.11  root     2597:                                                EnableWindow (GetDlgItem (hwndDlg, IDOK), FALSE);
                   2598:                                                Error ("DEVICE_PARTITIONS_ERR");
                   2599:                                                return 1;
                   2600:                                        }
1.1.1.7   root     2601: 
1.1.1.11  root     2602:                                        if (AskWarnNoYes ("WHOLE_DEVICE_WARNING") == IDNO)
                   2603:                                                return 1;
1.1.1.7   root     2604: 
1.1.1.11  root     2605:                                        Warning ("WHOLE_DEVICE_NOTE");
1.1.1.5   root     2606:                                }
1.1.1.13  root     2607: #else  // #ifdef VOLFORMAT
                   2608: 
                   2609:                                bSysDriveSelected = LvItem.lParam & SELDEVFLAG_SYSTEM_DRIVE;
                   2610:                                bSysPartitionSelected = FALSE;
                   2611: 
                   2612: #endif // #ifdef VOLFORMAT
                   2613: 
                   2614:                                sprintf (lpszFileName, 
                   2615:                                        (LvItem.lParam & SELDEVFLAG_VIRTUAL_PARTITION) ? 
                   2616:                                        "\\Device\\Harddisk%d\\Partition1" : "\\Device\\Harddisk%d\\Partition0",
                   2617:                                        driveNo);
1.1.1.5   root     2618:                        }
1.1.1.13  root     2619:                        else 
                   2620:                                bSysDriveSelected = FALSE;
1.1.1.5   root     2621: 
1.1.1.12  root     2622: #ifdef VOLFORMAT
                   2623:                        bRemovableHostDevice = LvItem.lParam & SELDEVFLAG_REMOVABLE_HOST_DEVICE;
                   2624: #endif
1.1.1.13  root     2625:                        NormalCursor ();
1.1       root     2626:                        EndDialog (hwndDlg, IDOK);
1.1.1.7   root     2627:                        return 1;
1.1       root     2628:                }
                   2629: 
                   2630:                if (lw == IDCANCEL)
                   2631:                {
1.1.1.13  root     2632:                        NormalCursor ();
1.1       root     2633:                        EndDialog (hwndDlg, IDCANCEL);
1.1.1.7   root     2634:                        return 1;
1.1       root     2635:                }
                   2636:                return 0;
                   2637:        }
                   2638:        return 0;
                   2639: }
                   2640: 
1.1.1.6   root     2641: 
                   2642: // Install and start driver service and mark it for removal (non-install mode)
                   2643: static int DriverLoad ()
                   2644: {
                   2645:        HANDLE file;
                   2646:        WIN32_FIND_DATA find;
                   2647:        SC_HANDLE hManager, hService = NULL;
                   2648:        char driverPath[TC_MAX_PATH*2];
                   2649:        BOOL res;
                   2650:        char *tmp;
                   2651: 
                   2652:        GetModuleFileName (NULL, driverPath, sizeof (driverPath));
                   2653:        tmp = strrchr (driverPath, '\\');
                   2654:        if (!tmp)
                   2655:        {
                   2656:                strcpy (driverPath, ".");
                   2657:                tmp = driverPath + 1;
                   2658:        }
                   2659: 
1.1.1.7   root     2660:        strcpy (tmp, !Is64BitOs () ? "\\truecrypt.sys" : "\\truecrypt-x64.sys");
1.1.1.6   root     2661: 
                   2662:        file = FindFirstFile (driverPath, &find);
                   2663: 
                   2664:        if (file == INVALID_HANDLE_VALUE)
                   2665:        {
1.1.1.7   root     2666:                MessageBoxW (0, GetString ("DRIVER_NOT_FOUND"), lpszTitle, ICON_HAND);
1.1.1.6   root     2667:                return ERR_DONT_REPORT;
                   2668:        }
                   2669: 
                   2670:        FindClose (file);
                   2671: 
                   2672:        hManager = OpenSCManager (NULL, NULL, SC_MANAGER_ALL_ACCESS);
                   2673:        if (hManager == NULL)
1.1.1.7   root     2674:        {
                   2675:                if (GetLastError () == ERROR_ACCESS_DENIED)
                   2676:                {
                   2677:                        MessageBoxW (0, GetString ("ADMIN_PRIVILEGES_DRIVER"), lpszTitle, ICON_HAND);
                   2678:                        return ERR_DONT_REPORT;
                   2679:                }
                   2680: 
1.1.1.6   root     2681:                return ERR_OS_ERROR;
1.1.1.7   root     2682:        }
1.1.1.6   root     2683: 
1.1.1.12  root     2684:        hService = OpenService (hManager, "truecrypt", SERVICE_ALL_ACCESS);
                   2685:        if (hService != NULL)
                   2686:        {
                   2687:                // Remove stale service (driver is not loaded but service exists)
                   2688:                DeleteService (hService);
                   2689:                CloseServiceHandle (hService);
                   2690:                Sleep (500);
                   2691:        }
                   2692: 
1.1.1.6   root     2693:        hService = CreateService (hManager, "truecrypt", "truecrypt",
                   2694:                SERVICE_ALL_ACCESS, SERVICE_KERNEL_DRIVER, SERVICE_DEMAND_START, SERVICE_ERROR_NORMAL,
                   2695:                driverPath, NULL, NULL, NULL, NULL, NULL);
                   2696: 
                   2697:        if (hService == NULL)
                   2698:        {
                   2699:                CloseServiceHandle (hManager);
                   2700:                return ERR_OS_ERROR;
                   2701:        }
                   2702: 
                   2703:        res = StartService (hService, 0, NULL);
                   2704:        DeleteService (hService);
1.1.1.12  root     2705: 
1.1.1.6   root     2706:        CloseServiceHandle (hManager);
                   2707:        CloseServiceHandle (hService);
                   2708: 
                   2709:        return !res ? ERR_OS_ERROR : ERROR_SUCCESS;
                   2710: }
                   2711: 
                   2712: 
1.1.1.7   root     2713: BOOL DriverUnload ()
                   2714: {
                   2715:        MOUNT_LIST_STRUCT driver;
                   2716:        int refCount;
1.1.1.13  root     2717:        int volumesMounted;
1.1.1.7   root     2718:        DWORD dwResult;
                   2719:        BOOL bResult;
                   2720: 
                   2721:        SC_HANDLE hManager, hService = NULL;
                   2722:        BOOL bRet;
                   2723:        SERVICE_STATUS status;
                   2724:        int x;
                   2725: 
                   2726:        if (hDriver == INVALID_HANDLE_VALUE)
                   2727:                return TRUE;
                   2728: 
                   2729:        // Test for mounted volumes
1.1.1.13  root     2730:        bResult = DeviceIoControl (hDriver, TC_IOCTL_IS_ANY_VOLUME_MOUNTED, NULL, 0, &volumesMounted, sizeof (volumesMounted), &dwResult, NULL);
                   2731: 
                   2732:        if (!bResult)
                   2733:        {
                   2734:                bResult = DeviceIoControl (hDriver, TC_IOCTL_LEGACY_GET_MOUNTED_VOLUMES, NULL, 0, &driver, sizeof (driver), &dwResult, NULL);
                   2735:                if (bResult)
                   2736:                        volumesMounted = driver.ulMountedDrives;
                   2737:        }
1.1.1.7   root     2738: 
                   2739:        if (bResult)
                   2740:        {
1.1.1.13  root     2741:                if (volumesMounted != 0)
1.1.1.7   root     2742:                        return FALSE;
                   2743:        }
                   2744:        else
                   2745:                return TRUE;
                   2746: 
                   2747:        // Test for any applications attached to driver
                   2748:        refCount = GetDriverRefCount ();
                   2749: 
                   2750:        if (refCount > 1)
                   2751:                return FALSE;
                   2752: 
                   2753:        CloseHandle (hDriver);
1.1.1.12  root     2754:        hDriver = INVALID_HANDLE_VALUE;
1.1.1.7   root     2755: 
                   2756:        // Stop driver service
                   2757: 
                   2758:        hManager = OpenSCManager (NULL, NULL, SC_MANAGER_ALL_ACCESS);
                   2759:        if (hManager == NULL)
                   2760:                goto error;
                   2761: 
                   2762:        hService = OpenService (hManager, "truecrypt", SERVICE_ALL_ACCESS);
                   2763:        if (hService == NULL)
                   2764:                goto error;
                   2765: 
                   2766:        bRet = QueryServiceStatus (hService, &status);
                   2767:        if (bRet != TRUE)
                   2768:                goto error;
                   2769: 
                   2770:        if (status.dwCurrentState != SERVICE_STOPPED)
                   2771:        {
                   2772:                ControlService (hService, SERVICE_CONTROL_STOP, &status);
                   2773: 
                   2774:                for (x = 0; x < 5; x++)
                   2775:                {
                   2776:                        bRet = QueryServiceStatus (hService, &status);
                   2777:                        if (bRet != TRUE)
                   2778:                                goto error;
                   2779: 
                   2780:                        if (status.dwCurrentState == SERVICE_STOPPED)
                   2781:                                break;
                   2782: 
                   2783:                        Sleep (200);
                   2784:                }
                   2785:        }
                   2786: 
                   2787: error:
                   2788:        if (hService != NULL)
                   2789:                CloseServiceHandle (hService);
                   2790: 
                   2791:        if (hManager != NULL)
                   2792:                CloseServiceHandle (hManager);
                   2793: 
                   2794:        if (status.dwCurrentState == SERVICE_STOPPED)
                   2795:        {
                   2796:                hDriver = INVALID_HANDLE_VALUE;
                   2797:                return TRUE;
                   2798:        }
                   2799: 
                   2800:        return FALSE;
                   2801: }
                   2802: 
                   2803: 
1.1       root     2804: int
                   2805: DriverAttach (void)
                   2806: {
1.1.1.6   root     2807:        /* Try to open a handle to the device driver. It will be closed later. */
1.1       root     2808: 
1.1.1.6   root     2809:        hDriver = CreateFile (WIN32_ROOT_PREFIX, 0, 0, NULL, OPEN_EXISTING, 0, NULL);
1.1       root     2810: 
                   2811:        if (hDriver == INVALID_HANDLE_VALUE)
                   2812:        {
1.1.1.6   root     2813: #ifndef SETUP
1.1.1.12  root     2814: load:
1.1.1.6   root     2815:                // Attempt to load driver (non-install mode)
1.1.1.8   root     2816:                {
                   2817:                        BOOL res = DriverLoad ();
1.1.1.6   root     2818: 
1.1.1.8   root     2819:                        if (res != ERROR_SUCCESS)
                   2820:                                return res;
1.1.1.6   root     2821: 
1.1.1.8   root     2822:                        hDriver = CreateFile (WIN32_ROOT_PREFIX, 0, 0, NULL, OPEN_EXISTING, 0, NULL);
                   2823:                }
1.1.1.6   root     2824: #endif
                   2825:                if (hDriver == INVALID_HANDLE_VALUE)
                   2826:                        return ERR_OS_ERROR;
1.1       root     2827:        }
1.1.1.7   root     2828: 
                   2829:        if (hDriver != INVALID_HANDLE_VALUE)
1.1       root     2830:        {
                   2831:                DWORD dwResult;
                   2832: 
1.1.1.13  root     2833:                BOOL bResult = DeviceIoControl (hDriver, TC_IOCTL_GET_DRIVER_VERSION, NULL, 0, &DriverVersion, sizeof (DriverVersion), &dwResult, NULL);
                   2834: 
                   2835:                if (!bResult)
                   2836:                        bResult = DeviceIoControl (hDriver, TC_IOCTL_LEGACY_GET_DRIVER_VERSION, NULL, 0, &DriverVersion, sizeof (DriverVersion), &dwResult, NULL);
1.1       root     2837: 
1.1.1.11  root     2838: #ifndef SETUP // Don't check version during setup to allow removal of another version
1.1       root     2839:                if (bResult == FALSE)
1.1.1.12  root     2840:                {
1.1       root     2841:                        return ERR_OS_ERROR;
1.1.1.12  root     2842:                }
1.1.1.11  root     2843:                else if (DriverVersion != VERSION_NUM)
1.1.1.12  root     2844:                {
                   2845:                        // Unload an incompatbile version of the driver loaded in non-install mode and load the required version
                   2846:                        if (IsNonInstallMode () && DriverUnload ())
                   2847:                                goto load;
                   2848: 
                   2849:                        CloseHandle (hDriver);
                   2850:                        hDriver = INVALID_HANDLE_VALUE;
1.1       root     2851:                        return ERR_DRIVER_VERSION;
1.1.1.12  root     2852:                }
1.1.1.11  root     2853: #else
                   2854:                if (!bResult)
                   2855:                        DriverVersion = 0;
1.1       root     2856: #endif
1.1.1.11  root     2857:        }
1.1       root     2858: 
                   2859:        return 0;
                   2860: }
                   2861: 
1.1.1.5   root     2862: 
                   2863: // Sets file pointer to hidden volume header
                   2864: BOOL SeekHiddenVolHeader (HFILE dev, unsigned __int64 volSize, BOOL deviceFlag)
                   2865: {
                   2866:        LARGE_INTEGER offset, offsetNew;
                   2867: 
                   2868:        if (deviceFlag)
                   2869:        {
                   2870:                // Partition/device
                   2871: 
                   2872:                offset.QuadPart = volSize - HIDDEN_VOL_HEADER_OFFSET;
                   2873: 
                   2874:                if (SetFilePointerEx ((HANDLE) dev, offset, &offsetNew, FILE_BEGIN) == 0)
                   2875:                        return FALSE;
                   2876: 
                   2877:                if (offsetNew.QuadPart != offset.QuadPart)
                   2878:                        return FALSE;
                   2879:        }
                   2880:        else
                   2881:        {
                   2882:                // File-hosted volume
                   2883: 
                   2884:                offset.QuadPart = - HIDDEN_VOL_HEADER_OFFSET;
                   2885: 
                   2886:                if (SetFilePointerEx ((HANDLE) dev, offset, &offsetNew, FILE_END) == 0)
                   2887:                        return FALSE;
                   2888:        }
                   2889: 
                   2890:        return TRUE;
                   2891: }
                   2892: 
1.1.1.10  root     2893: 
                   2894: void ResetCurrentDirectory ()
                   2895: {
                   2896:        char p[MAX_PATH];
                   2897:        if (!IsNonInstallMode () && SHGetFolderPath (NULL, CSIDL_PROFILE, NULL, 0, p) == ERROR_SUCCESS)
                   2898:        {
                   2899:                SetCurrentDirectory (p);
                   2900:        }
                   2901:        else
                   2902:        {
1.1.1.11  root     2903:                GetModPath (p, sizeof (p));
1.1.1.10  root     2904:                SetCurrentDirectory (p);
                   2905:        }
                   2906: }
                   2907: 
                   2908: 
1.1.1.11  root     2909: BOOL BrowseFiles (HWND hwndDlg, char *stringId, char *lpszFileName, BOOL keepHistory, BOOL saveMode)
                   2910: {
                   2911:        return BrowseFilesInDir (hwndDlg, stringId, NULL, lpszFileName, keepHistory, saveMode);
                   2912: }
                   2913: 
                   2914: 
                   2915: BOOL BrowseFilesInDir (HWND hwndDlg, char *stringId, char *initialDir, char *lpszFileName, BOOL keepHistory, BOOL saveMode)
1.1       root     2916: {
1.1.1.7   root     2917:        OPENFILENAMEW ofn;
                   2918:        wchar_t file[TC_MAX_PATH] = { 0 };
1.1.1.11  root     2919:        wchar_t wInitialDir[TC_MAX_PATH] = { 0 };
1.1.1.7   root     2920:        wchar_t filter[1024];
                   2921: 
1.1.1.10  root     2922:        ZeroMemory (&ofn, sizeof (ofn));
1.1.1.7   root     2923:        *lpszFileName = 0;
1.1.1.11  root     2924: 
                   2925:        if (initialDir)
                   2926:        {
                   2927:                swprintf_s (wInitialDir, sizeof (wInitialDir) / 2, L"%hs", initialDir);
                   2928:                ofn.lpstrInitialDir                     = wInitialDir;
                   2929:        }
                   2930: 
1.1.1.10  root     2931:        ofn.lStructSize                         = sizeof (ofn);
                   2932:        ofn.hwndOwner                           = hwndDlg;
                   2933:        wsprintfW (filter, L"%ls (*.*)%c*.*%c%ls (*.tc)%c*.tc%c%c",
                   2934:                GetString ("ALL_FILES"), 0, 0, GetString ("TC_VOLUMES"), 0, 0, 0);
                   2935:        ofn.lpstrFilter                         = filter;
                   2936:        ofn.nFilterIndex                        = 1;
                   2937:        ofn.lpstrFile                           = file;
                   2938:        ofn.nMaxFile                            = sizeof (file) / sizeof (file[0]);
                   2939:        ofn.lpstrTitle                          = GetString (stringId);
                   2940:        ofn.Flags                                       = OFN_HIDEREADONLY
1.1.1.8   root     2941:                | OFN_PATHMUSTEXIST
                   2942:                | (keepHistory ? 0 : OFN_DONTADDTORECENT)
                   2943:                | (saveMode ? OFN_OVERWRITEPROMPT : 0);
                   2944:        
1.1.1.11  root     2945:        if (!keepHistory)
                   2946:                CleanLastVisitedMRU ();
                   2947: 
1.1.1.8   root     2948:        if (!saveMode)
1.1.1.7   root     2949:        {
1.1.1.8   root     2950:                if (!GetOpenFileNameW (&ofn))
                   2951:                        return FALSE;
1.1.1.7   root     2952:        }
1.1       root     2953:        else
1.1.1.7   root     2954:        {
1.1.1.8   root     2955:                if (!GetSaveFileNameW (&ofn))
                   2956:                        return FALSE;
1.1.1.7   root     2957:        }
1.1.1.8   root     2958: 
                   2959:        WideCharToMultiByte (CP_ACP, 0, file, -1, lpszFileName, MAX_PATH, NULL, NULL);
1.1.1.9   root     2960: 
1.1.1.11  root     2961:        if (!keepHistory)
                   2962:                CleanLastVisitedMRU ();
1.1.1.9   root     2963: 
1.1.1.11  root     2964:        ResetCurrentDirectory ();
1.1.1.9   root     2965: 
                   2966:        return TRUE;
                   2967: }
                   2968: 
                   2969: 
                   2970: static char SelectMultipleFilesPath[MAX_PATH];
                   2971: static int SelectMultipleFilesOffset;
                   2972: 
                   2973: BOOL SelectMultipleFiles (HWND hwndDlg, char *stringId, char *lpszFileName, BOOL keepHistory)
                   2974: {
                   2975:        OPENFILENAMEW ofn;
                   2976:        wchar_t file[TC_MAX_PATH] = { 0 };
                   2977:        wchar_t filter[1024];
                   2978: 
1.1.1.10  root     2979:        ZeroMemory (&ofn, sizeof (ofn));
1.1.1.9   root     2980: 
                   2981:        *lpszFileName = 0;
1.1.1.10  root     2982:        ofn.lStructSize                         = sizeof (ofn);
                   2983:        ofn.hwndOwner                           = hwndDlg;
                   2984:        wsprintfW (filter, L"%ls (*.*)%c*.*%c%ls (*.tc)%c*.tc%c%c",
                   2985:                GetString ("ALL_FILES"), 0, 0, GetString ("TC_VOLUMES"), 0, 0, 0);
                   2986:        ofn.lpstrFilter                         = filter;
                   2987:        ofn.nFilterIndex                        = 1;
                   2988:        ofn.lpstrFile                           = file;
                   2989:        ofn.nMaxFile                            = sizeof (file) / sizeof (file[0]);
                   2990:        ofn.lpstrTitle                          = GetString (stringId);
                   2991:        ofn.Flags                                       = OFN_HIDEREADONLY
1.1.1.9   root     2992:                | OFN_EXPLORER
                   2993:                | OFN_PATHMUSTEXIST
                   2994:                | OFN_ALLOWMULTISELECT
                   2995:                | (keepHistory ? 0 : OFN_DONTADDTORECENT);
                   2996:        
                   2997:        if (!keepHistory)
                   2998:                CleanLastVisitedMRU ();
                   2999: 
1.1.1.11  root     3000:        if (!GetOpenFileNameW (&ofn))
                   3001:                return FALSE;
                   3002: 
1.1.1.9   root     3003:        if (file[ofn.nFileOffset - 1] != 0)
                   3004:        {
                   3005:                // Single file selected
                   3006:                WideCharToMultiByte (CP_ACP, 0, file, -1, lpszFileName, MAX_PATH, NULL, NULL);
                   3007:                SelectMultipleFilesOffset = 0;
                   3008:        }
                   3009:        else
                   3010:        {
                   3011:                // Multiple files selected
                   3012:                int n;
                   3013:                wchar_t *f = file;
                   3014:                char *s = SelectMultipleFilesPath;
                   3015:                while ((n = WideCharToMultiByte (CP_ACP, 0, f, -1, s, MAX_PATH, NULL, NULL)) > 1)
                   3016:                {
                   3017:                        f += n;
                   3018:                        s += n;
                   3019:                }
                   3020: 
                   3021:                SelectMultipleFilesOffset = ofn.nFileOffset;
                   3022:                SelectMultipleFilesNext (lpszFileName);
                   3023:        }
                   3024: 
1.1.1.11  root     3025:        if (!keepHistory)
                   3026:                CleanLastVisitedMRU ();
1.1.1.9   root     3027: 
1.1.1.11  root     3028:        ResetCurrentDirectory ();
1.1.1.9   root     3029: 
                   3030:        return TRUE;
                   3031: }
                   3032: 
                   3033: 
                   3034: BOOL SelectMultipleFilesNext (char *lpszFileName)
                   3035: {
                   3036:        if (SelectMultipleFilesOffset == 0)
                   3037:                return FALSE;
                   3038: 
                   3039:        strncpy (lpszFileName, SelectMultipleFilesPath, sizeof (SelectMultipleFilesPath));
                   3040: 
                   3041:        if (lpszFileName[strlen (lpszFileName) - 1] != '\\')
                   3042:                strcat (lpszFileName, "\\");
                   3043: 
                   3044:        strcat (lpszFileName, SelectMultipleFilesPath + SelectMultipleFilesOffset);
                   3045: 
                   3046:        SelectMultipleFilesOffset += strlen (SelectMultipleFilesPath + SelectMultipleFilesOffset) + 1;
                   3047:        if (SelectMultipleFilesPath[SelectMultipleFilesOffset] == 0)
                   3048:                SelectMultipleFilesOffset = 0;
                   3049: 
1.1.1.8   root     3050:        return TRUE;
1.1       root     3051: }
                   3052: 
                   3053: 
1.1.1.7   root     3054: static int CALLBACK
                   3055: BrowseCallbackProc(HWND hwnd,UINT uMsg,LPARAM lp, LPARAM pData) 
1.1.1.6   root     3056: {
1.1.1.7   root     3057:        switch(uMsg) {
                   3058:        case BFFM_INITIALIZED: 
                   3059:        {
                   3060:          /* WParam is TRUE since we are passing a path.
                   3061:           It would be FALSE if we were passing a pidl. */
                   3062:           SendMessage (hwnd,BFFM_SETSELECTION,TRUE,(LPARAM)pData);
                   3063:           break;
                   3064:        }
                   3065: 
                   3066:        case BFFM_SELCHANGED: 
                   3067:        {
                   3068:                char szDir[TC_MAX_PATH];
                   3069: 
                   3070:           /* Set the status window to the currently selected path. */
                   3071:           if (SHGetPathFromIDList((LPITEMIDLIST) lp ,szDir)) 
                   3072:           {
                   3073:                  SendMessage (hwnd,BFFM_SETSTATUSTEXT,0,(LPARAM)szDir);
                   3074:           }
                   3075:           break;
                   3076:        }
                   3077: 
                   3078:        default:
                   3079:           break;
                   3080:        }
                   3081: 
                   3082:        return 0;
                   3083: }
1.1.1.6   root     3084: 
                   3085: 
1.1.1.7   root     3086: BOOL
                   3087: BrowseDirectories (HWND hwndDlg, char *lpszTitle, char *dirName)
                   3088: {
                   3089:        BROWSEINFOW bi;
                   3090:        LPITEMIDLIST pidl;
                   3091:        LPMALLOC pMalloc;
                   3092:        BOOL bOK  = FALSE;
1.1.1.6   root     3093: 
1.1.1.7   root     3094:        if (SUCCEEDED(SHGetMalloc(&pMalloc))) 
1.1.1.6   root     3095:        {
1.1.1.7   root     3096:                ZeroMemory(&bi,sizeof(bi));
                   3097:                bi.hwndOwner = hwndDlg;
                   3098:                bi.pszDisplayName = 0;
                   3099:                bi.lpszTitle = GetString (lpszTitle);
                   3100:                bi.pidlRoot = 0;
                   3101:                bi.ulFlags = BIF_RETURNONLYFSDIRS | BIF_STATUSTEXT /*| BIF_EDITBOX*/;
                   3102:                bi.lpfn = BrowseCallbackProc;
                   3103:                bi.lParam = (LPARAM)dirName;
1.1.1.6   root     3104: 
1.1.1.7   root     3105:                pidl = SHBrowseForFolderW (&bi);
                   3106:                if (pidl!=NULL) 
                   3107:                {
                   3108:                        if (SHGetPathFromIDList(pidl, dirName)) 
                   3109:                        {
                   3110:                                bOK = TRUE;
                   3111:                        }
                   3112: 
                   3113:                        pMalloc->lpVtbl->Free(pMalloc,pidl);
                   3114:                        pMalloc->lpVtbl->Release(pMalloc);
                   3115:                }
1.1.1.6   root     3116:        }
                   3117: 
1.1.1.7   root     3118:        return bOK;
1.1.1.6   root     3119: }
                   3120: 
                   3121: 
1.1       root     3122: void
                   3123: handleError (HWND hwndDlg, int code)
                   3124: {
1.1.1.11  root     3125:        WCHAR szTmp[4096];
1.1.1.7   root     3126: 
                   3127:        if (Silent) return;
1.1       root     3128: 
                   3129:        switch (code)
                   3130:        {
                   3131:        case ERR_OS_ERROR:
                   3132:                handleWin32Error (hwndDlg);
                   3133:                break;
                   3134:        case ERR_OUTOFMEMORY:
1.1.1.7   root     3135:                MessageBoxW (hwndDlg, GetString ("OUTOFMEMORY"), lpszTitle, ICON_HAND);
1.1       root     3136:                break;
1.1.1.7   root     3137: 
1.1       root     3138:        case ERR_PASSWORD_WRONG:
1.1.1.11  root     3139:                swprintf (szTmp, GetString (KeyFilesEnable ? "PASSWORD_OR_KEYFILE_WRONG" : "PASSWORD_WRONG"));
1.1.1.7   root     3140:                if (CheckCapsLock (hwndDlg, TRUE))
                   3141:                        wcscat (szTmp, GetString ("PASSWORD_WRONG_CAPSLOCK_ON"));
                   3142: 
                   3143:                MessageBoxW (hwndDlg, szTmp, lpszTitle, MB_ICONWARNING);
1.1       root     3144:                break;
1.1.1.7   root     3145: 
1.1       root     3146:        case ERR_DRIVE_NOT_FOUND:
1.1.1.7   root     3147:                MessageBoxW (hwndDlg, GetString ("NOT_FOUND"), lpszTitle, ICON_HAND);
1.1       root     3148:                break;
                   3149:        case ERR_FILES_OPEN:
1.1.1.7   root     3150:                MessageBoxW (hwndDlg, GetString ("OPENFILES_DRIVER"), lpszTitle, ICON_HAND);
1.1       root     3151:                break;
                   3152:        case ERR_FILES_OPEN_LOCK:
1.1.1.7   root     3153:                MessageBoxW (hwndDlg, GetString ("OPENFILES_LOCK"), lpszTitle, ICON_HAND);
1.1       root     3154:                break;
                   3155:        case ERR_VOL_SIZE_WRONG:
1.1.1.7   root     3156:                MessageBoxW (hwndDlg, GetString ("VOL_SIZE_WRONG"), lpszTitle, ICON_HAND);
1.1       root     3157:                break;
                   3158:        case ERR_COMPRESSION_NOT_SUPPORTED:
1.1.1.7   root     3159:                MessageBoxW (hwndDlg, GetString ("COMPRESSION_NOT_SUPPORTED"), lpszTitle, ICON_HAND);
1.1       root     3160:                break;
                   3161:        case ERR_PASSWORD_CHANGE_VOL_TYPE:
1.1.1.7   root     3162:                MessageBoxW (hwndDlg, GetString ("WRONG_VOL_TYPE"), lpszTitle, ICON_HAND);
1.1       root     3163:                break;
                   3164:        case ERR_VOL_SEEKING:
1.1.1.7   root     3165:                MessageBoxW (hwndDlg, GetString ("VOL_SEEKING"), lpszTitle, ICON_HAND);
1.1       root     3166:                break;
                   3167:        case ERR_VOL_WRITING:
1.1.1.7   root     3168:                MessageBoxW (hwndDlg, GetString ("VOL_WRITING"), lpszTitle, ICON_HAND);
1.1       root     3169:                break;
                   3170:        case ERR_VOL_READING:
1.1.1.7   root     3171:                MessageBoxW (hwndDlg, GetString ("VOL_READING"), lpszTitle, ICON_HAND);
                   3172:                break;
                   3173:        case ERR_CIPHER_INIT_FAILURE:
                   3174:                MessageBoxW (hwndDlg, GetString ("ERR_CIPHER_INIT_FAILURE"), lpszTitle, ICON_HAND);
                   3175:                break;
                   3176:        case ERR_CIPHER_INIT_WEAK_KEY:
                   3177:                MessageBoxW (hwndDlg, GetString ("ERR_CIPHER_INIT_WEAK_KEY"), lpszTitle, ICON_HAND);
1.1       root     3178:                break;
                   3179:        case ERR_VOL_ALREADY_MOUNTED:
1.1.1.7   root     3180:                MessageBoxW (hwndDlg, GetString ("VOL_ALREADY_MOUNTED"), lpszTitle, ICON_HAND);
1.1       root     3181:                break;
                   3182:        case ERR_FILE_OPEN_FAILED:
1.1.1.7   root     3183:                MessageBoxW (hwndDlg, GetString ("FILE_OPEN_FAILED"), lpszTitle, ICON_HAND);
1.1       root     3184:                break;
                   3185:        case ERR_VOL_MOUNT_FAILED:
1.1.1.7   root     3186:                MessageBoxW (hwndDlg, GetString  ("VOL_MOUNT_FAILED"), lpszTitle, ICON_HAND);
1.1       root     3187:                break;
                   3188:        case ERR_NO_FREE_DRIVES:
1.1.1.7   root     3189:                MessageBoxW (hwndDlg, GetString ("NO_FREE_DRIVES"), lpszTitle, ICON_HAND);
1.1       root     3190:                break;
                   3191:        case ERR_INVALID_DEVICE:
1.1.1.7   root     3192:                MessageBoxW (hwndDlg, GetString ("INVALID_DEVICE"), lpszTitle, ICON_HAND);
1.1       root     3193:                break;
                   3194:        case ERR_ACCESS_DENIED:
1.1.1.7   root     3195:                MessageBoxW (hwndDlg, GetString ("ACCESS_DENIED"), lpszTitle, ICON_HAND);
1.1       root     3196:                break;
                   3197: 
                   3198:        case ERR_DRIVER_VERSION:
1.1.1.7   root     3199:                wsprintfW (szTmp, GetString ("DRIVER_VERSION"), VERSION_STRING);
                   3200:                MessageBoxW (hwndDlg, szTmp, lpszTitle, ICON_HAND);
1.1       root     3201:                break;
                   3202: 
                   3203:        case ERR_NEW_VERSION_REQUIRED:
1.1.1.7   root     3204:                MessageBoxW (hwndDlg, GetString ("NEW_VERSION_REQUIRED"), lpszTitle, ICON_HAND);
                   3205:                break;
                   3206: 
                   3207:        case ERR_SELF_TESTS_FAILED:
                   3208:                Error ("ERR_SELF_TESTS_FAILED");
1.1       root     3209:                break;
                   3210: 
1.1.1.13  root     3211:        case ERR_VOL_FORMAT_BAD:
                   3212:                Error ("ERR_VOL_FORMAT_BAD");
                   3213:                break;
                   3214: 
1.1.1.6   root     3215:        case ERR_DONT_REPORT:
                   3216:                break;
                   3217: 
1.1       root     3218:        default:
1.1.1.7   root     3219:                wsprintfW (szTmp, GetString ("ERR_UNKNOWN"), code);
                   3220:                MessageBoxW (hwndDlg, szTmp, lpszTitle, ICON_HAND);
1.1       root     3221:        }
                   3222: }
                   3223: 
1.1.1.7   root     3224: static BOOL CALLBACK LocalizeDialogEnum( HWND hwnd, LPARAM font)
1.1       root     3225: {
1.1.1.7   root     3226:        // Localization of controls
1.1       root     3227: 
1.1.1.7   root     3228:        if (LocalizationActive)
1.1       root     3229:        {
1.1.1.7   root     3230:                int ctrlId = GetDlgCtrlID (hwnd);
                   3231:                if (ctrlId != 0)
                   3232:                {
                   3233:                        char name[10] = { 0 };
                   3234:                        GetClassName (hwnd, name, sizeof (name));
1.1.1.5   root     3235: 
1.1.1.7   root     3236:                        if (_stricmp (name, "Button") == 0 || _stricmp (name, "Static") == 0)
                   3237:                        {
                   3238:                                wchar_t *str = GetDictionaryValueByInt (ctrlId);
                   3239:                                if (str != NULL)
                   3240:                                        SetWindowTextW (hwnd, str);
                   3241:                        }
                   3242:                }
                   3243:        }
1.1       root     3244: 
1.1.1.7   root     3245:        // Font
                   3246:        SendMessage (hwnd, WM_SETFONT, (WPARAM) font, 0);
                   3247:        
                   3248:        return TRUE;
                   3249: }
1.1       root     3250: 
1.1.1.7   root     3251: void LocalizeDialog (HWND hwnd, char *stringId)
                   3252: {
1.1.1.10  root     3253:        LastDialogId = stringId;
1.1.1.7   root     3254:        SetWindowLongPtr (hwnd, GWLP_USERDATA, (LONG_PTR) 'TRUE');
1.1.1.8   root     3255:        SendMessage (hwnd, WM_SETFONT, (WPARAM) hUserFont, 0);
1.1.1.5   root     3256: 
1.1.1.11  root     3257:        if (stringId == NULL)
                   3258:                SetWindowText (hwnd, "TrueCrypt");
1.1.1.13  root     3259:        else
1.1.1.7   root     3260:                SetWindowTextW (hwnd, GetString (stringId));
1.1.1.11  root     3261:        
1.1.1.7   root     3262:        if (hUserFont != 0)
                   3263:                EnumChildWindows (hwnd, LocalizeDialogEnum, (LPARAM) hUserFont);
1.1.1.5   root     3264: }
                   3265: 
                   3266: void OpenVolumeExplorerWindow (int driveNo)
                   3267: {
                   3268:        char dosName[5];
                   3269:        SHFILEINFO fInfo;
                   3270: 
                   3271:        sprintf (dosName, "%c:\\", (char) driveNo + 'A');
                   3272: 
                   3273:        // Force explorer to discover the drive
                   3274:        SHGetFileInfo (dosName, 0, &fInfo, sizeof (fInfo), 0);
                   3275: 
                   3276:        ShellExecute (NULL, "open", dosName, NULL, NULL, SW_SHOWNORMAL);
                   3277: }
                   3278: 
                   3279: static BOOL explorerCloseSent;
1.1.1.11  root     3280: static HWND explorerTopLevelWindow;
1.1.1.5   root     3281: 
1.1.1.11  root     3282: static BOOL CALLBACK CloseVolumeExplorerWindowsChildEnum (HWND hwnd, LPARAM driveStr)
1.1.1.5   root     3283: {
1.1.1.11  root     3284:        char s[MAX_PATH];
                   3285:        SendMessage (hwnd, WM_GETTEXT, sizeof (s), (LPARAM) s);
1.1.1.5   root     3286: 
1.1.1.11  root     3287:        if (strstr (s, (char *) driveStr) != NULL)
1.1.1.5   root     3288:        {
1.1.1.11  root     3289:                PostMessage (explorerTopLevelWindow, WM_CLOSE, 0, 0);
                   3290:                explorerCloseSent = TRUE;
                   3291:                return FALSE;
                   3292:        }
1.1.1.5   root     3293: 
1.1.1.11  root     3294:        return TRUE;
                   3295: }
                   3296: 
                   3297: static BOOL CALLBACK CloseVolumeExplorerWindowsEnum (HWND hwnd, LPARAM driveNo)
                   3298: {
                   3299:        char driveStr[10];
                   3300:        char s[MAX_PATH];
                   3301: 
                   3302:        sprintf (driveStr, "%c:\\", driveNo + 'A');
                   3303: 
                   3304:        GetClassName (hwnd, s, sizeof s);
                   3305:        if (strcmp (s, "CabinetWClass") == 0)
                   3306:        {
                   3307:                GetWindowText (hwnd, s, sizeof s);
                   3308:                if (strstr (s, driveStr) != NULL)
1.1.1.5   root     3309:                {
                   3310:                        PostMessage (hwnd, WM_CLOSE, 0, 0);
                   3311:                        explorerCloseSent = TRUE;
1.1.1.12  root     3312:                        return TRUE;
1.1.1.5   root     3313:                }
                   3314: 
1.1.1.11  root     3315:                explorerTopLevelWindow = hwnd;
                   3316:                EnumChildWindows (hwnd, CloseVolumeExplorerWindowsChildEnum, (LPARAM) driveStr);
1.1.1.5   root     3317:        }
1.1.1.11  root     3318: 
1.1.1.5   root     3319:        return TRUE;
                   3320: }
                   3321: 
                   3322: BOOL CloseVolumeExplorerWindows (HWND hwnd, int driveNo)
                   3323: {
                   3324:        explorerCloseSent = FALSE;
                   3325:        EnumWindows (CloseVolumeExplorerWindowsEnum, (LPARAM) driveNo);
                   3326: 
                   3327:        return explorerCloseSent;
                   3328: }
                   3329: 
1.1.1.7   root     3330: void GetSizeString (unsigned __int64 size, wchar_t *str)
                   3331: {
                   3332:        static wchar_t *b, *kb, *mb, *gb, *tb, *pb;
                   3333:        static int serNo;
                   3334: 
                   3335:        if (b == NULL || serNo != LocalizationSerialNo)
                   3336:        {
                   3337:                serNo = LocalizationSerialNo;
                   3338:                kb = GetString ("KB");
                   3339:                mb = GetString ("MB");
                   3340:                gb = GetString ("GB");
                   3341:                tb = GetString ("TB");
                   3342:                pb = GetString ("PB");
                   3343:                b = GetString ("BYTE");
                   3344:        }
                   3345: 
                   3346:        if (size > 1024I64*1024*1024*1024*1024*99)
                   3347:                swprintf (str, L"%I64d %s", size/1024/1024/1024/1024/1024, pb);
                   3348:        else if (size > 1024I64*1024*1024*1024*1024)
                   3349:                swprintf (str, L"%.1f %s",(double)(size/1024.0/1024/1024/1024/1024), pb);
                   3350:        else if (size > 1024I64*1024*1024*1024*99)
                   3351:                swprintf (str, L"%I64d %s",size/1024/1024/1024/1024, tb);
                   3352:        else if (size > 1024I64*1024*1024*1024)
                   3353:                swprintf (str, L"%.1f %s",(double)(size/1024.0/1024/1024/1024), tb);
                   3354:        else if (size > 1024I64*1024*1024*99)
                   3355:                swprintf (str, L"%I64d %s",size/1024/1024/1024, gb);
                   3356:        else if (size > 1024I64*1024*1024)
                   3357:                swprintf (str, L"%.1f %s",(double)(size/1024.0/1024/1024), gb);
                   3358:        else if (size > 1024I64*1024*99)
                   3359:                swprintf (str, L"%I64d %s", size/1024/1024, mb);
                   3360:        else if (size > 1024I64*1024)
                   3361:                swprintf (str, L"%.1f %s",(double)(size/1024.0/1024), mb);
                   3362:        else if (size > 1024I64)
                   3363:                swprintf (str, L"%I64d %s", size/1024, kb);
                   3364:        else
                   3365:                swprintf (str, L"%I64d %s", size, b);
                   3366: }
1.1.1.5   root     3367: 
                   3368: #ifndef SETUP
1.1.1.7   root     3369: void GetSpeedString (unsigned __int64 speed, wchar_t *str)
1.1.1.5   root     3370: {
1.1.1.7   root     3371:        static wchar_t *b, *kb, *mb, *gb, *tb, *pb;
                   3372:        static int serNo;
                   3373:        
                   3374:        if (b == NULL || serNo != LocalizationSerialNo)
                   3375:        {
                   3376:                serNo = LocalizationSerialNo;
                   3377:                kb = GetString ("KB_PER_SEC");
                   3378:                mb = GetString ("MB_PER_SEC");
                   3379:                gb = GetString ("GB_PER_SEC");
                   3380:                tb = GetString ("TB_PER_SEC");
                   3381:                pb = GetString ("PB_PER_SEC");
                   3382:                b = GetString ("B_PER_SEC");
                   3383:        }
                   3384: 
1.1.1.5   root     3385:        if (speed > 1024I64*1024*1024*1024*1024*99)
1.1.1.7   root     3386:                swprintf (str, L"%I64d %s", speed/1024/1024/1024/1024/1024, pb);
1.1.1.5   root     3387:        else if (speed > 1024I64*1024*1024*1024*1024)
1.1.1.7   root     3388:                swprintf (str, L"%.1f %s",(double)(speed/1024.0/1024/1024/1024/1024), pb);
1.1.1.5   root     3389:        else if (speed > 1024I64*1024*1024*1024*99)
1.1.1.7   root     3390:                swprintf (str, L"%I64d %s",speed/1024/1024/1024/1024, tb);
1.1.1.5   root     3391:        else if (speed > 1024I64*1024*1024*1024)
1.1.1.7   root     3392:                swprintf (str, L"%.1f %s",(double)(speed/1024.0/1024/1024/1024), tb);
1.1.1.5   root     3393:        else if (speed > 1024I64*1024*1024*99)
1.1.1.7   root     3394:                swprintf (str, L"%I64d %s",speed/1024/1024/1024, gb);
1.1.1.5   root     3395:        else if (speed > 1024I64*1024*1024)
1.1.1.7   root     3396:                swprintf (str, L"%.1f %s",(double)(speed/1024.0/1024/1024), gb);
1.1.1.5   root     3397:        else if (speed > 1024I64*1024*99)
1.1.1.7   root     3398:                swprintf (str, L"%I64d %s", speed/1024/1024, mb);
1.1.1.5   root     3399:        else if (speed > 1024I64*1024)
1.1.1.7   root     3400:                swprintf (str, L"%.1f %s",(double)(speed/1024.0/1024), mb);
                   3401:        else if (speed > 1024I64)
                   3402:                swprintf (str, L"%I64d %s", speed/1024, kb);
1.1.1.5   root     3403:        else
1.1.1.7   root     3404:                swprintf (str, L"%I64d %s", speed, b);
1.1.1.5   root     3405: }
                   3406: 
                   3407: static void DisplayBenchmarkResults (HWND hwndDlg)
                   3408: {
1.1.1.7   root     3409:        wchar_t item1[100]={0};
                   3410:        LVITEMW LvItem;
1.1.1.5   root     3411:        HWND hList = GetDlgItem (hwndDlg, IDC_RESULTS);
                   3412:        int ea, i;
                   3413:        BOOL unsorted = TRUE;
1.1.1.7   root     3414:        BENCHMARK_REC tmp_line;
1.1.1.5   root     3415: 
                   3416:        /* Sort the list */
                   3417: 
                   3418:        switch (benchmarkSortMethod)
                   3419:        {
                   3420:        case BENCHMARK_SORT_BY_SPEED:
                   3421: 
                   3422:                while (unsorted)
                   3423:                {
                   3424:                        unsorted = FALSE;
                   3425:                        for (i = 0; i < benchmarkTotalItems - 1; i++)
                   3426:                        {
                   3427:                                if (benchmarkTable[i].meanBytesPerSec < benchmarkTable[i+1].meanBytesPerSec)
                   3428:                                {
                   3429:                                        unsorted = TRUE;
                   3430:                                        memcpy (&tmp_line, &benchmarkTable[i], sizeof(BENCHMARK_REC));
                   3431:                                        memcpy (&benchmarkTable[i], &benchmarkTable[i+1], sizeof(BENCHMARK_REC));
                   3432:                                        memcpy (&benchmarkTable[i+1], &tmp_line, sizeof(BENCHMARK_REC));
                   3433:                                }
                   3434:                        }
                   3435:                }
                   3436:                break;
                   3437: 
                   3438:        case BENCHMARK_SORT_BY_NAME:
                   3439: 
                   3440:                while (unsorted)
                   3441:                {
                   3442:                        unsorted = FALSE;
                   3443:                        for (i = 0; i < benchmarkTotalItems - 1; i++)
                   3444:                        {
                   3445:                                if (benchmarkTable[i].id > benchmarkTable[i+1].id)
                   3446:                                {
                   3447:                                        unsorted = TRUE;
                   3448:                                        memcpy (&tmp_line, &benchmarkTable[i], sizeof(BENCHMARK_REC));
                   3449:                                        memcpy (&benchmarkTable[i], &benchmarkTable[i+1], sizeof(BENCHMARK_REC));
                   3450:                                        memcpy (&benchmarkTable[i+1], &tmp_line, sizeof(BENCHMARK_REC));
                   3451:                                }
                   3452:                        }
                   3453:                }
                   3454:                break;
                   3455:        }
                   3456:   
                   3457:        /* Render the results */
                   3458: 
                   3459:        SendMessage (hList,LVM_DELETEALLITEMS,0,(LPARAM)&LvItem);
                   3460: 
                   3461:        for (i = 0; i < benchmarkTotalItems; i++)
                   3462:        {
                   3463:                ea = benchmarkTable[i].id;
                   3464: 
                   3465:                memset (&LvItem,0,sizeof(LvItem));
                   3466:                LvItem.mask = LVIF_TEXT;
1.1.1.7   root     3467:                LvItem.iItem = i;
1.1.1.5   root     3468:                LvItem.iSubItem = 0;
1.1.1.7   root     3469:                LvItem.pszText = (LPWSTR) benchmarkTable[i].name;
                   3470:                SendMessageW (hList, LVM_INSERTITEM, 0, (LPARAM)&LvItem); 
1.1.1.5   root     3471: 
1.1.1.7   root     3472: #if PKCS5_BENCHMARKS
                   3473:                wcscpy (item1, L"-");
                   3474: #else
                   3475:                GetSpeedString ((unsigned __int64) (benchmarkLastBufferSize / ((float) benchmarkTable[i].encSpeed / benchmarkPerformanceFrequency.QuadPart)), item1);
                   3476: #endif
1.1.1.5   root     3477:                LvItem.iSubItem = 1;
                   3478:                LvItem.pszText = item1;
                   3479: 
1.1.1.7   root     3480:                SendMessageW (hList, LVM_SETITEMW, 0, (LPARAM)&LvItem); 
                   3481: 
                   3482: #if PKCS5_BENCHMARKS
                   3483:                wcscpy (item1, L"-");
                   3484: #else
                   3485:                GetSpeedString ((unsigned __int64) (benchmarkLastBufferSize / ((float) benchmarkTable[i].decSpeed / benchmarkPerformanceFrequency.QuadPart)), item1);
                   3486: #endif
1.1.1.5   root     3487:                LvItem.iSubItem = 2;
                   3488:                LvItem.pszText = item1;
                   3489: 
1.1.1.7   root     3490:                SendMessageW (hList, LVM_SETITEMW, 0, (LPARAM)&LvItem); 
1.1.1.5   root     3491: 
1.1.1.7   root     3492: #if PKCS5_BENCHMARKS
                   3493:                swprintf (item1, L"%d t", benchmarkTable[i].encSpeed);
                   3494: #else
                   3495:                GetSpeedString (benchmarkTable[i].meanBytesPerSec, item1);
                   3496: #endif
1.1.1.5   root     3497:                LvItem.iSubItem = 3;
                   3498:                LvItem.pszText = item1;
                   3499: 
1.1.1.7   root     3500:                SendMessageW (hList, LVM_SETITEMW, 0, (LPARAM)&LvItem); 
1.1.1.5   root     3501:        }
                   3502: }
                   3503: 
                   3504: static BOOL PerformBenchmark(HWND hwndDlg)
                   3505: {
                   3506:     LARGE_INTEGER performanceCountStart, performanceCountEnd;
                   3507:        BYTE *lpTestBuffer;
1.1.1.8   root     3508:        PCRYPTO_INFO ci = NULL;
1.1.1.7   root     3509: #if !(PKCS5_BENCHMARKS || HASH_FNC_BENCHMARKS)
1.1.1.8   root     3510:        ci = crypto_open ();
                   3511:        if (!ci)
                   3512:                return FALSE;
1.1.1.7   root     3513: #endif
1.1.1.5   root     3514: 
                   3515:        if (QueryPerformanceFrequency (&benchmarkPerformanceFrequency) == 0)
                   3516:        {
1.1.1.7   root     3517:                MessageBoxW (hwndDlg, GetString ("ERR_PERF_COUNTER"), lpszTitle, ICON_HAND);
1.1.1.5   root     3518:                return FALSE;
                   3519:        }
                   3520: 
                   3521:        lpTestBuffer = malloc(benchmarkBufferSize - (benchmarkBufferSize % 16));
                   3522:        if (lpTestBuffer == NULL)
                   3523:        {
1.1.1.7   root     3524:                MessageBoxW (hwndDlg, GetString ("ERR_MEM_ALLOC"), lpszTitle, ICON_HAND);
1.1.1.5   root     3525:                return FALSE;
                   3526:        }
                   3527:        VirtualLock (lpTestBuffer, benchmarkBufferSize - (benchmarkBufferSize % 16));
                   3528: 
1.1.1.11  root     3529:        WaitCursor ();
1.1.1.5   root     3530:        benchmarkTotalItems = 0;
1.1.1.7   root     3531: 
1.1.1.13  root     3532: #if !(PKCS5_BENCHMARKS || HASH_FNC_BENCHMARKS)
1.1.1.11  root     3533:        // CPU "warm up" (an attempt to prevent skewed results on systems where CPU frequency
                   3534:        // gradually changes depending on CPU load).
                   3535:        ci->ea = EAGetFirst();
1.1.1.13  root     3536:        if (!EAInit (ci->ea, ci->master_keydata, ci->ks))
1.1.1.11  root     3537:        {
1.1.1.13  root     3538:                ci->mode = FIRST_MODE_OF_OPERATION_ID;
1.1.1.11  root     3539:                if (EAInitMode (ci))
                   3540:                {
                   3541:                        int i;
                   3542: 
                   3543:                        for (i = 0; i < 2; i++)
                   3544:                        {
1.1.1.13  root     3545:                                EncryptBuffer (lpTestBuffer, (TC_LARGEST_COMPILER_UINT) benchmarkBufferSize, ci);
                   3546:                                DecryptBuffer (lpTestBuffer, (TC_LARGEST_COMPILER_UINT) benchmarkBufferSize, ci);
1.1.1.11  root     3547:                        }
                   3548:                }
                   3549:        }
1.1.1.13  root     3550: #endif
1.1.1.11  root     3551: 
1.1.1.7   root     3552: #if HASH_FNC_BENCHMARKS
                   3553: 
                   3554:        /* Measures the speed at which each of the hash algorithms processes the message to produce
                   3555:           a single digest. 
                   3556: 
                   3557:           The hash algorithm benchmarks are included here for development purposes only. Do not enable 
                   3558:           them when building a public release (the benchmark GUI strings wouldn't make sense). */
                   3559: 
                   3560:        {
                   3561:                BYTE *digest [MAX_DIGESTSIZE];
                   3562:                WHIRLPOOL_CTX   wctx;
                   3563:                RMD160_CTX              rctx;
                   3564:                sha1_ctx                sctx;
1.1.1.13  root     3565:                sha512_ctx              s2ctx;
1.1.1.7   root     3566:                int hid;
                   3567: 
1.1.1.13  root     3568:                for (hid = FIRST_PRF_ID; hid <= LAST_PRF_ID; hid++) 
1.1.1.7   root     3569:                {
                   3570:                        if (QueryPerformanceCounter (&performanceCountStart) == 0)
                   3571:                                goto counter_error;
                   3572: 
                   3573:                        switch (hid)
                   3574:                        {
                   3575:                        case SHA1:
                   3576:                                sha1_begin (&sctx);
                   3577:                                sha1_hash (lpTestBuffer, benchmarkBufferSize, &sctx);
                   3578:                                sha1_end ((unsigned char *) digest, &sctx);
                   3579:                                break;
                   3580: 
1.1.1.13  root     3581:                        case SHA512:
                   3582:                                sha512_begin (&s2ctx);
                   3583:                                sha512_hash (lpTestBuffer, benchmarkBufferSize, &s2ctx);
                   3584:                                sha512_end ((unsigned char *) digest, &s2ctx);
                   3585:                                break;
                   3586: 
1.1.1.7   root     3587:                        case RIPEMD160:
                   3588:                                RMD160Init(&rctx);
                   3589:                                RMD160Update(&rctx, lpTestBuffer, benchmarkBufferSize);
                   3590:                                RMD160Final((unsigned char *) digest, &rctx);
                   3591:                                break;
                   3592: 
                   3593:                        case WHIRLPOOL:
                   3594:                                WHIRLPOOL_init (&wctx);
                   3595:                                WHIRLPOOL_add (lpTestBuffer, benchmarkBufferSize * 8, &wctx);
                   3596:                                WHIRLPOOL_finalize (&wctx, (unsigned char *) digest);
                   3597:                                break;
                   3598:                        }
                   3599: 
                   3600:                        if (QueryPerformanceCounter (&performanceCountEnd) == 0)
                   3601:                                goto counter_error;
                   3602: 
                   3603:                        benchmarkTable[benchmarkTotalItems].encSpeed = performanceCountEnd.QuadPart - performanceCountStart.QuadPart;
                   3604: 
                   3605:                        benchmarkTable[benchmarkTotalItems].decSpeed = benchmarkTable[benchmarkTotalItems].encSpeed;
                   3606:                        benchmarkTable[benchmarkTotalItems].id = hid;
                   3607:                        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     3608:                        sprintf (benchmarkTable[benchmarkTotalItems].name, "%s", HashGetName(hid));
1.1.1.7   root     3609: 
                   3610:                        benchmarkTotalItems++;
                   3611:                }
                   3612:        }
                   3613: 
                   3614: #elif PKCS5_BENCHMARKS // #if HASH_FNC_BENCHMARKS
                   3615: 
                   3616:        /* Measures the time that it takes for the PKCS-5 routine to derive a header key using
                   3617:           each of the implemented PRF algorithms. 
                   3618: 
                   3619:           The PKCS-5 benchmarks are included here for development purposes only. Do not enable 
                   3620:           them when building a public release (the benchmark GUI strings wouldn't make sense). */
                   3621:        {
                   3622:                int thid, i;
1.1.1.13  root     3623:                char dk[MASTER_KEYDATA_SIZE];
1.1.1.7   root     3624:                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"};
                   3625: 
1.1.1.13  root     3626:                for (thid = FIRST_PRF_ID; thid <= LAST_PRF_ID; thid++) 
1.1.1.7   root     3627:                {
                   3628:                        if (QueryPerformanceCounter (&performanceCountStart) == 0)
                   3629:                                goto counter_error;
                   3630: 
                   3631:                        for (i = 1; i <= 5; i++) 
                   3632:                        {
                   3633:                                switch (thid)
                   3634:                                {
                   3635:                                case SHA1:
                   3636:                                        /* PKCS-5 test with HMAC-SHA-1 used as the PRF */
1.1.1.13  root     3637:                                        derive_key_sha1 ("passphrase-1234567890", 21, tmp_salt, 64, get_pkcs5_iteration_count(thid, FALSE), dk, MASTER_KEYDATA_SIZE);
                   3638:                                        break;
                   3639: 
                   3640:                                case SHA512:
                   3641:                                        /* PKCS-5 test with HMAC-SHA-512 used as the PRF */
                   3642:                                        derive_key_sha512 ("passphrase-1234567890", 21, tmp_salt, 64, get_pkcs5_iteration_count(thid, FALSE), dk, MASTER_KEYDATA_SIZE);
1.1.1.7   root     3643:                                        break;
                   3644: 
                   3645:                                case RIPEMD160:
                   3646:                                        /* PKCS-5 test with HMAC-RIPEMD-160 used as the PRF */
1.1.1.13  root     3647:                                        derive_key_ripemd160 ("passphrase-1234567890", 21, tmp_salt, 64, get_pkcs5_iteration_count(thid, FALSE), dk, MASTER_KEYDATA_SIZE);
1.1.1.7   root     3648:                                        break;
                   3649: 
                   3650:                                case WHIRLPOOL:
                   3651:                                        /* PKCS-5 test with HMAC-Whirlpool used as the PRF */
1.1.1.13  root     3652:                                        derive_key_whirlpool ("passphrase-1234567890", 21, tmp_salt, 64, get_pkcs5_iteration_count(thid, FALSE), dk, MASTER_KEYDATA_SIZE);
1.1.1.7   root     3653:                                        break;
                   3654:                                }
                   3655:                        }
                   3656: 
                   3657:                        if (QueryPerformanceCounter (&performanceCountEnd) == 0)
                   3658:                                goto counter_error;
                   3659: 
                   3660:                        benchmarkTable[benchmarkTotalItems].encSpeed = performanceCountEnd.QuadPart - performanceCountStart.QuadPart;
                   3661:                        benchmarkTable[benchmarkTotalItems].id = thid;
                   3662:                        sprintf (benchmarkTable[benchmarkTotalItems].name, "%s", get_pkcs5_prf_name (thid));
                   3663: 
                   3664:                        benchmarkTotalItems++;
                   3665:                }
                   3666:        }
                   3667: 
                   3668: #else  // #elif PKCS5_BENCHMARKS
                   3669: 
                   3670:        /* Encryption algorithm benchmarks */
1.1.1.8   root     3671:                
                   3672:        for (ci->ea = EAGetFirst(); ci->ea != 0; ci->ea = EAGetNext(ci->ea))
1.1.1.5   root     3673:        {
1.1.1.11  root     3674:                if (!EAIsFormatEnabled (ci->ea))
1.1.1.8   root     3675:                        continue;
                   3676: 
1.1.1.13  root     3677:                EAInit (ci->ea, ci->master_keydata, ci->ks);
1.1.1.8   root     3678: 
1.1.1.13  root     3679:                ci->mode = FIRST_MODE_OF_OPERATION_ID;
1.1.1.8   root     3680:                if (!EAInitMode (ci))
                   3681:                        break;
                   3682: 
1.1.1.5   root     3683:                if (QueryPerformanceCounter (&performanceCountStart) == 0)
                   3684:                        goto counter_error;
                   3685: 
1.1.1.13  root     3686:                EncryptBuffer (lpTestBuffer, (TC_LARGEST_COMPILER_UINT) benchmarkBufferSize, ci);
1.1.1.5   root     3687: 
                   3688:                if (QueryPerformanceCounter (&performanceCountEnd) == 0)
                   3689:                        goto counter_error;
                   3690: 
                   3691:                benchmarkTable[benchmarkTotalItems].encSpeed = performanceCountEnd.QuadPart - performanceCountStart.QuadPart;
                   3692: 
                   3693:                if (QueryPerformanceCounter (&performanceCountStart) == 0)
                   3694:                        goto counter_error;
                   3695: 
1.1.1.13  root     3696:                DecryptBuffer (lpTestBuffer, (TC_LARGEST_COMPILER_UINT) benchmarkBufferSize, ci);
1.1.1.8   root     3697: 
1.1.1.5   root     3698:                if (QueryPerformanceCounter (&performanceCountEnd) == 0)
                   3699:                        goto counter_error;
                   3700: 
                   3701:                benchmarkTable[benchmarkTotalItems].decSpeed = performanceCountEnd.QuadPart - performanceCountStart.QuadPart;
1.1.1.8   root     3702:                benchmarkTable[benchmarkTotalItems].id = ci->ea;
1.1.1.5   root     3703:                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     3704:                EAGetName (benchmarkTable[benchmarkTotalItems].name, ci->ea);
1.1.1.5   root     3705: 
                   3706:                benchmarkTotalItems++;
                   3707:        }
                   3708: 
1.1.1.7   root     3709: #endif // #elif PKCS5_BENCHMARKS (#else)
                   3710: 
1.1.1.8   root     3711:        if (ci)
                   3712:                crypto_close (ci);
                   3713: 
1.1.1.5   root     3714:        VirtualUnlock (lpTestBuffer, benchmarkBufferSize - (benchmarkBufferSize % 16));
                   3715: 
                   3716:        free(lpTestBuffer);
                   3717: 
                   3718:        benchmarkLastBufferSize = benchmarkBufferSize;
                   3719: 
                   3720:        DisplayBenchmarkResults(hwndDlg);
                   3721: 
1.1.1.7   root     3722:        EnableWindow (GetDlgItem (hwndDlg, IDC_PERFORM_BENCHMARK), TRUE);
1.1.1.5   root     3723:        EnableWindow (GetDlgItem (hwndDlg, IDCLOSE), TRUE);
                   3724: 
                   3725:        NormalCursor ();
                   3726:        return TRUE;
                   3727: 
                   3728: counter_error:
1.1.1.8   root     3729:        
                   3730:        if (ci)
                   3731:                crypto_close (ci);
1.1.1.5   root     3732: 
                   3733:        VirtualUnlock (lpTestBuffer, benchmarkBufferSize - (benchmarkBufferSize % 16));
                   3734: 
                   3735:        free(lpTestBuffer);
                   3736: 
                   3737:        NormalCursor ();
                   3738: 
1.1.1.7   root     3739:        EnableWindow (GetDlgItem (hwndDlg, IDC_PERFORM_BENCHMARK), TRUE);
1.1.1.5   root     3740:        EnableWindow (GetDlgItem (hwndDlg, IDCLOSE), TRUE);
                   3741: 
1.1.1.7   root     3742:        MessageBoxW (hwndDlg, GetString ("ERR_PERF_COUNTER"), lpszTitle, ICON_HAND);
1.1.1.5   root     3743:        return FALSE;
                   3744: }
                   3745: 
                   3746: 
1.1.1.12  root     3747: BOOL CALLBACK BenchmarkDlgProc (HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam)
1.1.1.5   root     3748: {
                   3749:        WORD lw = LOWORD (wParam);
                   3750:        LPARAM nIndex;
                   3751:        HWND hCboxSortMethod = GetDlgItem (hwndDlg, IDC_BENCHMARK_SORT_METHOD);
                   3752:        HWND hCboxBufferSize = GetDlgItem (hwndDlg, IDC_BENCHMARK_BUFFER_SIZE);
                   3753: 
                   3754:        switch (msg)
                   3755:        {
                   3756:        case WM_INITDIALOG:
                   3757:                {
1.1.1.7   root     3758:                        LVCOLUMNW LvCol;
                   3759:                        wchar_t s[128];
1.1.1.5   root     3760:                        HWND hList = GetDlgItem (hwndDlg, IDC_RESULTS);
                   3761: 
1.1.1.7   root     3762:                        LocalizeDialog (hwndDlg, "IDD_BENCHMARK_DLG");
1.1.1.5   root     3763: 
                   3764:                        benchmarkBufferSize = BENCHMARK_DEFAULT_BUF_SIZE;
                   3765:                        benchmarkSortMethod = BENCHMARK_SORT_BY_SPEED;
                   3766: 
                   3767:                        SendMessage (hList,LVM_SETEXTENDEDLISTVIEWSTYLE,0,
1.1.1.7   root     3768:                                LVS_EX_FULLROWSELECT|LVS_EX_HEADERDRAGDROP|LVS_EX_LABELTIP 
1.1.1.5   root     3769:                                ); 
                   3770: 
                   3771:                        memset (&LvCol,0,sizeof(LvCol));               
                   3772:                        LvCol.mask = LVCF_TEXT|LVCF_WIDTH|LVCF_SUBITEM|LVCF_FMT;  
1.1.1.12  root     3773:                        LvCol.pszText = GetString ("ALGORITHM");
                   3774:                        LvCol.cx = CompensateXDPI (114);
1.1.1.5   root     3775:                        LvCol.fmt = LVCFMT_LEFT;
1.1.1.7   root     3776:                        SendMessage (hList,LVM_INSERTCOLUMNW,0,(LPARAM)&LvCol);
1.1.1.5   root     3777: 
1.1.1.12  root     3778:                        LvCol.pszText = GetString ("ENCRYPTION");
                   3779:                        LvCol.cx = CompensateXDPI (80);
1.1.1.5   root     3780:                        LvCol.fmt = LVCFMT_RIGHT;
1.1.1.7   root     3781:                        SendMessageW (hList,LVM_INSERTCOLUMNW,1,(LPARAM)&LvCol);
1.1.1.5   root     3782: 
1.1.1.12  root     3783:                        LvCol.pszText = GetString ("DECRYPTION");
                   3784:                        LvCol.cx = CompensateXDPI (80);
1.1.1.5   root     3785:                        LvCol.fmt = LVCFMT_RIGHT;
1.1.1.7   root     3786:                        SendMessageW (hList,LVM_INSERTCOLUMNW,2,(LPARAM)&LvCol);
1.1.1.5   root     3787: 
1.1.1.12  root     3788:                        LvCol.pszText = GetString ("MEAN");
                   3789:                        LvCol.cx = CompensateXDPI (80);
1.1.1.5   root     3790:                        LvCol.fmt = LVCFMT_RIGHT;
1.1.1.7   root     3791:                        SendMessageW (hList,LVM_INSERTCOLUMNW,3,(LPARAM)&LvCol);
1.1.1.5   root     3792: 
                   3793:                        /* Combo boxes */
                   3794: 
                   3795:                        // Sort method
                   3796: 
                   3797:                        SendMessage (hCboxSortMethod, CB_RESETCONTENT, 0, 0);
                   3798: 
1.1.1.7   root     3799:                        nIndex = SendMessageW (hCboxSortMethod, CB_ADDSTRING, 0, (LPARAM) GetString ("ALPHABETICAL_CATEGORIZED"));
1.1.1.5   root     3800:                        SendMessage (hCboxSortMethod, CB_SETITEMDATA, nIndex, (LPARAM) 0);
                   3801: 
1.1.1.7   root     3802:                        nIndex = SendMessageW (hCboxSortMethod, CB_ADDSTRING, 0, (LPARAM) GetString ("MEAN_SPEED"));
1.1.1.5   root     3803:                        SendMessage (hCboxSortMethod, CB_SETITEMDATA, nIndex, (LPARAM) 0);
                   3804: 
                   3805:                        SendMessage (hCboxSortMethod, CB_SETCURSEL, 1, 0);              // Default sort method
                   3806: 
                   3807:                        // Buffer size
                   3808: 
                   3809:                        SendMessage (hCboxBufferSize, CB_RESETCONTENT, 0, 0);
                   3810: 
1.1.1.7   root     3811:                        swprintf (s, L"5 %s", GetString ("KB"));
                   3812:                        nIndex = SendMessageW (hCboxBufferSize, CB_ADDSTRING, 0, (LPARAM) s);
1.1.1.5   root     3813:                        SendMessage (hCboxBufferSize, CB_SETITEMDATA, nIndex, (LPARAM) 5 * BYTES_PER_KB);
                   3814: 
1.1.1.7   root     3815:                        swprintf (s, L"100 %s", GetString ("KB"));
                   3816:                        nIndex = SendMessageW (hCboxBufferSize, CB_ADDSTRING, 0, (LPARAM) s);
1.1.1.5   root     3817:                        SendMessage (hCboxBufferSize, CB_SETITEMDATA, nIndex, (LPARAM) 100 * BYTES_PER_KB);
                   3818: 
1.1.1.7   root     3819:                        swprintf (s, L"500 %s", GetString ("KB"));
                   3820:                        nIndex = SendMessageW (hCboxBufferSize, CB_ADDSTRING, 0, (LPARAM) s);
1.1.1.5   root     3821:                        SendMessage (hCboxBufferSize, CB_SETITEMDATA, nIndex, (LPARAM) 500 * BYTES_PER_KB);
                   3822: 
1.1.1.7   root     3823:                        swprintf (s, L"1 %s", GetString ("MB"));
                   3824:                        nIndex = SendMessageW (hCboxBufferSize, CB_ADDSTRING, 0, (LPARAM) s);
1.1.1.5   root     3825:                        SendMessage (hCboxBufferSize, CB_SETITEMDATA, nIndex, (LPARAM) 1 * BYTES_PER_MB);
                   3826: 
1.1.1.7   root     3827:                        swprintf (s, L"5 %s", GetString ("MB"));
                   3828:                        nIndex = SendMessageW (hCboxBufferSize, CB_ADDSTRING, 0, (LPARAM) s);
1.1.1.5   root     3829:                        SendMessage (hCboxBufferSize, CB_SETITEMDATA, nIndex, (LPARAM) 5 * BYTES_PER_MB);
                   3830: 
1.1.1.7   root     3831:                        swprintf (s, L"10 %s", GetString ("MB"));
                   3832:                        nIndex = SendMessageW (hCboxBufferSize, CB_ADDSTRING, 0, (LPARAM) s);
1.1.1.5   root     3833:                        SendMessage (hCboxBufferSize, CB_SETITEMDATA, nIndex, (LPARAM) 10 * BYTES_PER_MB);
                   3834: 
1.1.1.7   root     3835:                        swprintf (s, L"50 %s", GetString ("MB"));
                   3836:                        nIndex = SendMessageW (hCboxBufferSize, CB_ADDSTRING, 0, (LPARAM) s);
1.1.1.5   root     3837:                        SendMessage (hCboxBufferSize, CB_SETITEMDATA, nIndex, (LPARAM) 50 * BYTES_PER_MB);
                   3838: 
1.1.1.7   root     3839:                        swprintf (s, L"100 %s", GetString ("MB"));
                   3840:                        nIndex = SendMessageW (hCboxBufferSize, CB_ADDSTRING, 0, (LPARAM) s);
1.1.1.5   root     3841:                        SendMessage (hCboxBufferSize, CB_SETITEMDATA, nIndex, (LPARAM) 100 * BYTES_PER_MB);
                   3842: 
1.1.1.7   root     3843:                        swprintf (s, L"200 %s", GetString ("MB"));
                   3844:                        nIndex = SendMessageW (hCboxBufferSize, CB_ADDSTRING, 0, (LPARAM) s);
1.1.1.5   root     3845:                        SendMessage (hCboxBufferSize, CB_SETITEMDATA, nIndex, (LPARAM) 200 * BYTES_PER_MB);
                   3846: 
1.1.1.7   root     3847:                        swprintf (s, L"500 %s", GetString ("MB"));
                   3848:                        nIndex = SendMessageW (hCboxBufferSize, CB_ADDSTRING, 0, (LPARAM) s);
1.1.1.5   root     3849:                        SendMessage (hCboxBufferSize, CB_SETITEMDATA, nIndex, (LPARAM) 500 * BYTES_PER_MB);
                   3850: 
1.1.1.7   root     3851:                        swprintf (s, L"1 %s", GetString ("GB"));
                   3852:                        nIndex = SendMessageW (hCboxBufferSize, CB_ADDSTRING, 0, (LPARAM) s);
1.1.1.5   root     3853:                        SendMessage (hCboxBufferSize, CB_SETITEMDATA, nIndex, (LPARAM) 1 * BYTES_PER_GB);
                   3854: 
                   3855:                        SendMessage (hCboxBufferSize, CB_SETCURSEL, 3, 0);              // Default size
                   3856: 
                   3857:                        return 1;
                   3858:                }
                   3859:                break;
                   3860: 
                   3861:        case WM_COMMAND:
                   3862:        case WM_NOTIFY:
                   3863: 
                   3864:                if (lw == IDC_BENCHMARK_SORT_METHOD)
                   3865:                {
                   3866:                        nIndex = SendMessage (hCboxSortMethod, CB_GETCURSEL, 0, 0);
                   3867:                        if (nIndex != benchmarkSortMethod)
                   3868:                        {
                   3869:                                benchmarkSortMethod = nIndex;
                   3870:                                DisplayBenchmarkResults (hwndDlg);
                   3871:                        }
1.1.1.7   root     3872:                        return 1;
1.1.1.5   root     3873:                }
                   3874: 
1.1.1.7   root     3875:                if (lw == IDC_PERFORM_BENCHMARK)
1.1.1.5   root     3876:                {
                   3877:                        nIndex = SendMessage (hCboxBufferSize, CB_GETCURSEL, 0, 0);
                   3878:                        benchmarkBufferSize = SendMessage (hCboxBufferSize, CB_GETITEMDATA, nIndex, 0);
                   3879: 
1.1.1.11  root     3880:                        if (PerformBenchmark (hwndDlg) == FALSE)
1.1.1.5   root     3881:                        {
                   3882:                                EndDialog (hwndDlg, IDCLOSE);
                   3883:                        }
1.1.1.7   root     3884:                        return 1;
1.1.1.5   root     3885:                }
1.1.1.7   root     3886:                if (lw == IDCLOSE || lw == IDCANCEL)
1.1.1.5   root     3887:                {
                   3888:                        EndDialog (hwndDlg, IDCLOSE);
1.1.1.7   root     3889:                        return 1;
1.1.1.5   root     3890:                }
                   3891:                return 0;
                   3892: 
                   3893:                break;
                   3894: 
                   3895:        case WM_CLOSE:
                   3896:                EndDialog (hwndDlg, IDCLOSE);
1.1.1.7   root     3897:                return 1;
1.1.1.5   root     3898: 
                   3899:                break;
                   3900: 
                   3901:        }
                   3902:        return 0;
                   3903: }
                   3904: 
                   3905: 
1.1.1.7   root     3906: 
                   3907: /* Except in response to the WM_INITDIALOG message, the dialog box procedure
                   3908:    should return nonzero if it processes the message, and zero if it does
                   3909:    not. - see DialogProc */
1.1.1.12  root     3910: BOOL CALLBACK
1.1.1.7   root     3911: KeyfileGeneratorDlgProc (HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam)
                   3912: {
                   3913:        WORD lw = LOWORD (wParam);
                   3914:        WORD hw = HIWORD (wParam);
                   3915:        static unsigned char randPool [RNG_POOL_SIZE];
                   3916:        static unsigned char lastRandPool [RNG_POOL_SIZE];
1.1.1.13  root     3917:        static char outputDispBuffer [RNG_POOL_SIZE * 3 + RANDPOOL_DISPLAY_ROWS + 2];
1.1.1.10  root     3918:        static BOOL bDisplayPoolContents = TRUE;
1.1.1.13  root     3919:        static BOOL bRandPoolDispAscii = FALSE;
1.1.1.7   root     3920:        int hash_algo = RandGetHashFunction();
                   3921:        int hid;
                   3922: 
                   3923:        if (lParam);            /* remove warning */
                   3924: 
                   3925:        switch (msg)
                   3926:        {
                   3927:        case WM_INITDIALOG:
1.1.1.5   root     3928:                {
1.1.1.7   root     3929:                        HWND hComboBox = GetDlgItem (hwndDlg, IDC_PRF_ID);
                   3930: 
                   3931:                        VirtualLock (randPool, sizeof(randPool));
                   3932:                        VirtualLock (lastRandPool, sizeof(lastRandPool));
                   3933:                        VirtualLock (outputDispBuffer, sizeof(outputDispBuffer));
                   3934: 
                   3935:                        LocalizeDialog (hwndDlg, "IDD_KEYFILE_GENERATOR_DLG");
                   3936: 
                   3937:                        SendMessage (hComboBox, CB_RESETCONTENT, 0, 0);
1.1.1.13  root     3938:                        for (hid = FIRST_PRF_ID; hid <= LAST_PRF_ID; hid++)
1.1.1.7   root     3939:                        {
1.1.1.13  root     3940:                                if (!HashIsDeprecated (hid))
                   3941:                                        AddComboPair (hComboBox, HashGetName(hid), hid);
1.1.1.7   root     3942:                        }
                   3943:                        SelectAlgo (hComboBox, &hash_algo);
                   3944: 
                   3945:                        SetCheckBox (hwndDlg, IDC_DISPLAY_POOL_CONTENTS, bDisplayPoolContents);
                   3946: 
                   3947: #ifndef VOLFORMAT                      
                   3948:                        if (Randinit ()) 
                   3949:                        {
                   3950:                                Error ("INIT_RAND");
                   3951:                                EndDialog (hwndDlg, IDCLOSE);
                   3952:                        }
                   3953: #endif
1.1.1.13  root     3954:                        SetTimer (hwndDlg, 0xfd, RANDPOOL_DISPLAY_REFRESH_INTERVAL, NULL);
1.1.1.7   root     3955:                        SendMessage (GetDlgItem (hwndDlg, IDC_POOL_CONTENTS), WM_SETFONT, (WPARAM) hFixedDigitFont, (LPARAM) TRUE);
                   3956:                        return 1;
                   3957:                }
                   3958: 
                   3959:        case WM_TIMER:
                   3960:                {
                   3961:                        char tmp[4];
1.1.1.13  root     3962:                        unsigned char tmpByte;
1.1.1.7   root     3963:                        int col, row;
                   3964: 
                   3965:                        if (bDisplayPoolContents)
                   3966:                        {
                   3967:                                RandpeekBytes (randPool, sizeof (randPool));
                   3968: 
                   3969:                                if (memcmp (lastRandPool, randPool, sizeof(lastRandPool)) != 0)
                   3970:                                {
                   3971:                                        outputDispBuffer[0] = 0;
                   3972: 
1.1.1.13  root     3973:                                        for (row = 0; row < RANDPOOL_DISPLAY_ROWS; row++)
1.1.1.7   root     3974:                                        {
1.1.1.13  root     3975:                                                for (col = 0; col < RANDPOOL_DISPLAY_COLUMNS; col++)
1.1.1.7   root     3976:                                                {
1.1.1.13  root     3977:                                                        tmpByte = randPool[row * RANDPOOL_DISPLAY_COLUMNS + col];
                   3978: 
                   3979:                                                        sprintf (tmp, bRandPoolDispAscii ? ((tmpByte >= 32 && tmpByte < 255 && tmpByte != '&') ? " %c " : " . ") : "%02X ", tmpByte);
1.1.1.7   root     3980:                                                        strcat (outputDispBuffer, tmp);
                   3981:                                                }
                   3982:                                                strcat (outputDispBuffer, "\n");
                   3983:                                        }
                   3984:                                        SetWindowText (GetDlgItem (hwndDlg, IDC_POOL_CONTENTS), outputDispBuffer);
                   3985: 
                   3986:                                        memcpy (lastRandPool, randPool, sizeof(lastRandPool));
                   3987:                                }
                   3988:                        }
                   3989:                        return 1;
                   3990:                }
                   3991: 
                   3992:        case WM_COMMAND:
                   3993: 
                   3994:                if (lw == IDCLOSE || lw == IDCANCEL)
                   3995:                {
                   3996:                        goto exit;
                   3997:                }
                   3998: 
                   3999:                if (lw == IDC_PRF_ID && hw == CBN_SELCHANGE)
                   4000:                {
                   4001:                        hid = (int) SendMessage (GetDlgItem (hwndDlg, IDC_PRF_ID), CB_GETCURSEL, 0, 0);
                   4002:                        hash_algo = (int) SendMessage (GetDlgItem (hwndDlg, IDC_PRF_ID), CB_GETITEMDATA, hid, 0);
                   4003:                        RandSetHashFunction (hash_algo);
                   4004:                        return 1;
                   4005:                }
                   4006: 
                   4007:                if (lw == IDC_DISPLAY_POOL_CONTENTS)
                   4008:                {
                   4009:                        if (!(bDisplayPoolContents = GetCheckBox (hwndDlg, IDC_DISPLAY_POOL_CONTENTS)))
1.1.1.13  root     4010:                        {
                   4011:                                char tmp[RNG_POOL_SIZE+1];
                   4012: 
                   4013:                                memset (tmp, ' ', sizeof(tmp));
                   4014:                                tmp [RNG_POOL_SIZE] = 0;
                   4015:                                SetWindowText (GetDlgItem (hwndDlg, IDC_POOL_CONTENTS), tmp);
                   4016:                        }
1.1.1.7   root     4017:                }
                   4018: 
                   4019:                if (lw == IDC_GENERATE_AND_SAVE_KEYFILE)
                   4020:                {
                   4021:                        char szFileName [TC_MAX_PATH];
                   4022:                        unsigned char keyfile [MAX_PASSWORD];
                   4023:                        int fhKeyfile = -1;
                   4024: 
                   4025:                        /* Select filename */
1.1.1.10  root     4026:                        if (!BrowseFiles (hwndDlg, "OPEN_TITLE", szFileName, bHistory, TRUE))
1.1.1.7   root     4027:                                return 1;
                   4028: 
                   4029:                        /* Conceive the file */
                   4030:                        if ((fhKeyfile = _open(szFileName, _O_CREAT|_O_TRUNC|_O_WRONLY|_O_BINARY, _S_IREAD|_S_IWRITE)) == -1)
                   4031:                        {
1.1.1.10  root     4032:                                handleWin32Error (hwndDlg);
1.1.1.7   root     4033:                                return 1;
                   4034:                        }
                   4035: 
                   4036:                        /* Generate the keyfile */ 
                   4037:                        WaitCursor();
1.1.1.10  root     4038:                        if (!RandgetBytes (keyfile, sizeof(keyfile), TRUE))
                   4039:                        {
                   4040:                                _close (fhKeyfile);
                   4041:                                DeleteFile (szFileName);
                   4042:                                NormalCursor();
                   4043:                                return 1;
                   4044:                        }
1.1.1.7   root     4045:                        NormalCursor();
                   4046: 
                   4047:                        /* Write the keyfile */
                   4048:                        if (_write (fhKeyfile, keyfile, sizeof(keyfile)) == -1)
                   4049:                                handleWin32Error (hwndDlg);
                   4050:                        else
                   4051:                                Info("KEYFILE_CREATED");
                   4052: 
1.1.1.13  root     4053:                        burn (keyfile, sizeof(keyfile));
1.1.1.7   root     4054:                        _close (fhKeyfile);
                   4055:                        return 1;
                   4056:                }
                   4057:                return 0;
                   4058: 
                   4059:        case WM_CLOSE:
                   4060:                {
                   4061:                        char tmp[RNG_POOL_SIZE+1];
                   4062: exit:
1.1.1.13  root     4063:                        WaitCursor();
1.1.1.7   root     4064:                        KillTimer (hwndDlg, 0xfd);
                   4065: 
                   4066: #ifndef VOLFORMAT                      
                   4067:                        Randfree ();
                   4068: #endif
                   4069:                        /* Cleanup */
                   4070: 
1.1.1.13  root     4071:                        burn (randPool, sizeof(randPool));
                   4072:                        burn (lastRandPool, sizeof(lastRandPool));
                   4073:                        burn (outputDispBuffer, sizeof(outputDispBuffer));
1.1.1.7   root     4074: 
                   4075:                        // Attempt to wipe the pool contents in the GUI text area
                   4076:                        memset (tmp, 'X', RNG_POOL_SIZE);
                   4077:                        tmp [RNG_POOL_SIZE] = 0;
                   4078:                        SetWindowText (GetDlgItem (hwndDlg, IDC_POOL_CONTENTS), tmp);
                   4079: 
                   4080:                        EndDialog (hwndDlg, IDCLOSE);
1.1.1.13  root     4081:                        NormalCursor ();
1.1.1.7   root     4082:                        return 1;
1.1.1.5   root     4083:                }
                   4084:        }
1.1.1.7   root     4085:        return 0;
1.1.1.5   root     4086: }
                   4087: 
                   4088: 
1.1.1.7   root     4089: 
                   4090: /* Except in response to the WM_INITDIALOG message, the dialog box procedure
                   4091: should return nonzero if it processes the message, and zero if it does
                   4092: not. - see DialogProc */
                   4093: BOOL CALLBACK
                   4094: CipherTestDialogProc (HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
1.1.1.5   root     4095: {
1.1.1.8   root     4096:        static int idTestCipher = -1;           /* Currently selected cipher for the test vector facility (none = -1). */
1.1.1.13  root     4097:        static BOOL bXTSTestEnabled = FALSE;
1.1.1.8   root     4098: 
                   4099:        PCRYPTO_INFO ci;
1.1.1.7   root     4100:        WORD lw = LOWORD (wParam);
                   4101:        WORD hw = HIWORD (wParam);
                   4102: 
                   4103:        if (lParam);            /* Remove unused parameter warning */
                   4104: 
                   4105:        switch (uMsg)
1.1.1.5   root     4106:        {
1.1.1.7   root     4107:        case WM_INITDIALOG:
                   4108:                {
                   4109:                        int ea;
                   4110:                        char buf[100];
                   4111: 
                   4112:                        LocalizeDialog (hwndDlg, "IDD_CIPHER_TEST_DLG");
                   4113: 
                   4114:                        SendMessage(GetDlgItem(hwndDlg, IDC_TESTS_MESSAGE), WM_SETFONT, (WPARAM)hBoldFont, MAKELPARAM(TRUE,0));
                   4115:                        SendMessage(GetDlgItem(hwndDlg, IDC_KEY), EM_LIMITTEXT, 128,0);
                   4116:                        SendMessage(GetDlgItem(hwndDlg, IDC_KEY), WM_SETFONT, (WPARAM)hFixedDigitFont, MAKELPARAM(1,0));
1.1.1.13  root     4117:                        SendMessage(GetDlgItem(hwndDlg, IDC_PLAINTEXT), EM_LIMITTEXT,64,0);
1.1.1.7   root     4118:                        SendMessage(GetDlgItem(hwndDlg, IDC_PLAINTEXT), WM_SETFONT, (WPARAM)hFixedDigitFont, MAKELPARAM(1,0));
1.1.1.13  root     4119:                        SendMessage(GetDlgItem(hwndDlg, IDC_CIPHERTEXT), EM_LIMITTEXT,64,0);
1.1.1.7   root     4120:                        SendMessage(GetDlgItem(hwndDlg, IDC_CIPHERTEXT), WM_SETFONT, (WPARAM)hFixedDigitFont, MAKELPARAM(1,0));
1.1.1.13  root     4121:                        SendMessage(GetDlgItem(hwndDlg, IDC_SECONDARY_KEY), EM_LIMITTEXT, 128,0);
                   4122:                        SendMessage(GetDlgItem(hwndDlg, IDC_SECONDARY_KEY), WM_SETFONT, (WPARAM)hFixedDigitFont, MAKELPARAM(1,0));
                   4123:                        SendMessage(GetDlgItem(hwndDlg, IDC_TEST_DATA_UNIT_NUMBER), EM_LIMITTEXT,32,0);
                   4124:                        SendMessage(GetDlgItem(hwndDlg, IDC_TEST_DATA_UNIT_NUMBER), WM_SETFONT, (WPARAM)hFixedDigitFont, MAKELPARAM(1,0));
                   4125:                        SetCheckBox (hwndDlg, IDC_XTS_MODE_ENABLED, bXTSTestEnabled);
                   4126:                        EnableWindow (GetDlgItem (hwndDlg, IDC_SECONDARY_KEY), bXTSTestEnabled);
                   4127:                        EnableWindow (GetDlgItem (hwndDlg, IDT_SECONDARY_KEY), bXTSTestEnabled);
                   4128:                        EnableWindow (GetDlgItem (hwndDlg, IDC_TEST_BLOCK_NUMBER), bXTSTestEnabled);
                   4129:                        EnableWindow (GetDlgItem (hwndDlg, IDT_TEST_BLOCK_NUMBER), bXTSTestEnabled);
                   4130:                        EnableWindow (GetDlgItem (hwndDlg, IDC_TEST_DATA_UNIT_NUMBER), bXTSTestEnabled);
                   4131:                        EnableWindow (GetDlgItem (hwndDlg, IDT_TEST_DATA_UNIT_NUMBER), bXTSTestEnabled);
1.1.1.7   root     4132: 
                   4133:                        if (idTestCipher == -1)
                   4134:                                idTestCipher = (int) lParam;
                   4135: 
                   4136:                        SendMessage (GetDlgItem (hwndDlg, IDC_CIPHER), CB_RESETCONTENT, 0, 0);
                   4137:                        for (ea = EAGetFirst (); ea != 0; ea = EAGetNext (ea))
                   4138:                        {
1.1.1.11  root     4139:                                if (EAGetCipherCount (ea) == 1 && EAIsFormatEnabled (ea))
1.1.1.12  root     4140:                                        AddComboPair (GetDlgItem (hwndDlg, IDC_CIPHER), EAGetName (buf, ea), EAGetFirstCipher (ea));
1.1.1.7   root     4141:                        }
                   4142: 
                   4143:                        ResetCipherTest(hwndDlg, idTestCipher);
                   4144: 
                   4145:                        SelectAlgo (GetDlgItem (hwndDlg, IDC_CIPHER), &idTestCipher);
                   4146: 
                   4147:                        return 1;
                   4148:                }
                   4149: 
                   4150:        case WM_COMMAND:
                   4151: 
                   4152:                if (hw == CBN_SELCHANGE && lw == IDC_CIPHER)
                   4153:                {
                   4154:                        idTestCipher = (int) SendMessage (GetDlgItem (hwndDlg, IDC_CIPHER), CB_GETITEMDATA, SendMessage (GetDlgItem (hwndDlg, IDC_CIPHER), CB_GETCURSEL, 0, 0), 0);
                   4155:                        ResetCipherTest(hwndDlg, idTestCipher);
                   4156:                        SendMessage (hwndDlg, WM_INITDIALOG, 0, 0);
                   4157:                        return 1;
                   4158:                }
                   4159: 
                   4160:                if (hw == CBN_SELCHANGE && lw == IDC_KEY_SIZE)
                   4161:                {
                   4162:                        // NOP
                   4163:                        return 1;
                   4164:                }
                   4165: 
                   4166:                if (lw == IDC_RESET)
                   4167:                {
                   4168:                        ResetCipherTest(hwndDlg, idTestCipher);
                   4169: 
                   4170:                        return 1;
                   4171:                }
                   4172: 
                   4173:                if (lw == IDC_AUTO)
                   4174:                {
1.1.1.13  root     4175:                        WaitCursor ();
1.1.1.7   root     4176:                        if (!AutoTestAlgorithms())
                   4177:                        {
                   4178:                                ShowWindow(GetDlgItem(hwndDlg, IDC_TESTS_MESSAGE), SW_SHOWNORMAL);
                   4179:                                SetWindowTextW(GetDlgItem(hwndDlg, IDC_TESTS_MESSAGE), GetString ("TESTS_FAILED"));
                   4180:                        } 
                   4181:                        else
                   4182:                        {
                   4183:                                ShowWindow(GetDlgItem(hwndDlg, IDC_TESTS_MESSAGE), SW_SHOWNORMAL);
                   4184:                                SetWindowTextW(GetDlgItem(hwndDlg, IDC_TESTS_MESSAGE), GetString ("TESTS_PASSED"));
                   4185:                                ShowWindow(GetDlgItem(hwndDlg, IDC_REDTICK), SW_SHOWNORMAL);
                   4186:                        }
1.1.1.13  root     4187:                        NormalCursor ();
1.1.1.7   root     4188: 
                   4189:                        return 1;
                   4190: 
                   4191:                }
                   4192: 
1.1.1.13  root     4193:                if (lw == IDC_XTS_MODE_ENABLED)
1.1.1.8   root     4194:                {
1.1.1.13  root     4195:                        bXTSTestEnabled = GetCheckBox (hwndDlg, IDC_XTS_MODE_ENABLED);
                   4196:                        EnableWindow (GetDlgItem (hwndDlg, IDC_SECONDARY_KEY), bXTSTestEnabled);
                   4197:                        EnableWindow (GetDlgItem (hwndDlg, IDT_SECONDARY_KEY), bXTSTestEnabled);
                   4198:                        EnableWindow (GetDlgItem (hwndDlg, IDC_TEST_BLOCK_NUMBER), bXTSTestEnabled);
                   4199:                        EnableWindow (GetDlgItem (hwndDlg, IDT_TEST_BLOCK_NUMBER), bXTSTestEnabled);
                   4200:                        EnableWindow (GetDlgItem (hwndDlg, IDT_TEST_DATA_UNIT_NUMBER), bXTSTestEnabled);
                   4201:                        EnableWindow (GetDlgItem (hwndDlg, IDC_TEST_DATA_UNIT_NUMBER), bXTSTestEnabled);
                   4202:                        if (bXTSTestEnabled)
1.1.1.9   root     4203:                                SendMessage(GetDlgItem(hwndDlg, IDC_KEY_SIZE), CB_SETCURSEL, 0,0);
1.1.1.8   root     4204:                }
                   4205: 
1.1.1.7   root     4206:                if (lw == IDOK || lw == IDC_ENCRYPT || lw == IDC_DECRYPT)
                   4207:                {
1.1.1.13  root     4208:                        char key[128+1], inputtext[128+1], secondaryKey[64+1], dataUnitNo[16+1], szTmp[128+1];
                   4209:                        int ks, pt, n, tlen, blockNo = 0;
1.1.1.7   root     4210:                        BOOL bEncrypt;
                   4211: 
                   4212:                        ShowWindow(GetDlgItem(hwndDlg, IDC_TESTS_MESSAGE), SW_HIDE);
                   4213:                        ShowWindow(GetDlgItem(hwndDlg, IDC_REDTICK), SW_HIDE);
                   4214: 
                   4215:                        ks = (int) SendMessage(GetDlgItem(hwndDlg, IDC_KEY_SIZE), CB_GETCURSEL, 0,0);
                   4216:                        ks = (int) SendMessage(GetDlgItem(hwndDlg, IDC_KEY_SIZE), CB_GETITEMDATA, ks,0);
                   4217:                        pt = (int) SendMessage(GetDlgItem(hwndDlg, IDC_PLAINTEXT_SIZE), CB_GETITEMDATA, 0,0);
                   4218: 
                   4219:                        bEncrypt = lw == IDC_ENCRYPT;
                   4220: 
                   4221:                        memset(key,0,sizeof(key));
                   4222:                        memset(szTmp,0,sizeof(szTmp));
                   4223:                        n = GetWindowText(GetDlgItem(hwndDlg, IDC_KEY), szTmp, sizeof(szTmp));
                   4224:                        if (n != ks * 2)
                   4225:                        {
1.1.1.13  root     4226:                                Warning ("TEST_KEY_SIZE");
1.1.1.7   root     4227:                                return 1;
                   4228:                        }
                   4229: 
                   4230:                        for (n = 0; n < ks; n ++)
                   4231:                        {
                   4232:                                char szTmp2[3], *ptr;
                   4233:                                long x;
                   4234: 
                   4235:                                szTmp2[2] = 0;
                   4236:                                szTmp2[0] = szTmp[n * 2];
                   4237:                                szTmp2[1] = szTmp[n * 2 + 1];
                   4238: 
                   4239:                                x = strtol(szTmp2, &ptr, 16);
                   4240: 
                   4241:                                key[n] = (char) x;
                   4242:                        }
                   4243: 
1.1.1.8   root     4244:                        memset(inputtext, 0, sizeof(inputtext));
1.1.1.13  root     4245:                        memset(secondaryKey, 0, sizeof(secondaryKey));
                   4246:                        memset(dataUnitNo, 0, sizeof(dataUnitNo));
1.1.1.8   root     4247:                        memset(szTmp, 0, sizeof(szTmp));
1.1.1.7   root     4248: 
                   4249:                        if (bEncrypt)
                   4250:                        {
                   4251:                                n = GetWindowText(GetDlgItem(hwndDlg, IDC_PLAINTEXT), szTmp, sizeof(szTmp));
                   4252:                        }
                   4253:                        else
                   4254:                        {
                   4255:                                n = GetWindowText(GetDlgItem(hwndDlg, IDC_CIPHERTEXT), szTmp, sizeof(szTmp));
                   4256:                        }
                   4257: 
                   4258:                        if (n != pt * 2)
                   4259:                        {
                   4260:                                if (bEncrypt)
                   4261:                                {
1.1.1.13  root     4262:                                        Warning ("TEST_PLAINTEXT_SIZE");
1.1.1.7   root     4263:                                        return 1;
                   4264:                                }
                   4265:                                else
                   4266:                                {
1.1.1.13  root     4267:                                        Warning  ("TEST_CIPHERTEXT_SIZE");
1.1.1.7   root     4268:                                        return 1;
                   4269:                                }
                   4270:                        }
1.1.1.8   root     4271: 
1.1.1.7   root     4272:                        for (n = 0; n < pt; n ++)
                   4273:                        {
                   4274:                                char szTmp2[3], *ptr;
                   4275:                                long x;
                   4276: 
                   4277:                                szTmp2[2] = 0;
                   4278:                                szTmp2[0] = szTmp[n * 2];
                   4279:                                szTmp2[1] = szTmp[n * 2 + 1];
                   4280: 
                   4281:                                x = strtol(szTmp2, &ptr, 16);
                   4282: 
                   4283:                                inputtext[n] = (char) x;
                   4284:                        }
                   4285:                        
1.1.1.13  root     4286:                        // XTS
                   4287:                        if (bXTSTestEnabled)
1.1.1.7   root     4288:                        {
1.1.1.13  root     4289:                                // Secondary key
                   4290: 
                   4291:                                if (GetWindowText(GetDlgItem(hwndDlg, IDC_SECONDARY_KEY), szTmp, sizeof(szTmp)) != 64)
1.1.1.8   root     4292:                                {
1.1.1.13  root     4293:                                        Warning ("TEST_INCORRECT_SECONDARY_KEY_SIZE");
1.1.1.8   root     4294:                                        return 1;
                   4295:                                }
1.1.1.7   root     4296: 
1.1.1.13  root     4297:                                for (n = 0; n < 64; n ++)
1.1.1.7   root     4298:                                {
1.1.1.8   root     4299:                                        char szTmp2[3], *ptr;
                   4300:                                        long x;
                   4301: 
                   4302:                                        szTmp2[2] = 0;
                   4303:                                        szTmp2[0] = szTmp[n * 2];
                   4304:                                        szTmp2[1] = szTmp[n * 2 + 1];
                   4305: 
                   4306:                                        x = strtol(szTmp2, &ptr, 16);
                   4307: 
1.1.1.13  root     4308:                                        secondaryKey[n] = (char) x;
1.1.1.7   root     4309:                                }
                   4310: 
1.1.1.13  root     4311:                                // Data unit number
1.1.1.7   root     4312: 
1.1.1.13  root     4313:                                tlen = GetWindowText(GetDlgItem(hwndDlg, IDC_TEST_DATA_UNIT_NUMBER), szTmp, sizeof(szTmp));
                   4314: 
                   4315:                                if (tlen > 16 || tlen < 1)
1.1.1.7   root     4316:                                {
1.1.1.13  root     4317:                                        Warning ("TEST_INCORRECT_TEST_DATA_UNIT_SIZE");
1.1.1.8   root     4318:                                        return 1;
1.1.1.7   root     4319:                                }
1.1.1.13  root     4320: 
                   4321:                                LeftPadString (szTmp, tlen, 16, '0');
                   4322: 
                   4323:                                for (n = 0; n < 16; n ++)
1.1.1.7   root     4324:                                {
1.1.1.8   root     4325:                                        char szTmp2[3], *ptr;
                   4326:                                        long x;
                   4327: 
                   4328:                                        szTmp2[2] = 0;
                   4329:                                        szTmp2[0] = szTmp[n * 2];
                   4330:                                        szTmp2[1] = szTmp[n * 2 + 1];
                   4331: 
                   4332:                                        x = strtol(szTmp2, &ptr, 16);
                   4333: 
1.1.1.13  root     4334:                                        dataUnitNo[n] = (char) x;
1.1.1.7   root     4335:                                }
1.1.1.9   root     4336: 
1.1.1.13  root     4337:                                // Block number
                   4338: 
                   4339:                                blockNo = (int) SendMessage (GetDlgItem (hwndDlg, IDC_TEST_BLOCK_NUMBER), CB_GETITEMDATA, SendMessage (GetDlgItem (hwndDlg, IDC_TEST_BLOCK_NUMBER), CB_GETCURSEL, 0, 0), 0);
                   4340:                        }       // if (bXTSTestEnabled)
1.1.1.7   root     4341: 
1.1.1.8   root     4342:                        
                   4343:                        /* Perform the actual tests */
                   4344: 
                   4345:                        if (ks != CB_ERR && pt != CB_ERR) 
                   4346:                        {
                   4347:                                char tmp[128];
1.1.1.9   root     4348:                                int tmpRetVal;
1.1.1.8   root     4349: 
                   4350:                                /* Copy the plain/ciphertext */
                   4351:                                memcpy(tmp,inputtext, pt);
                   4352: 
1.1.1.13  root     4353:                                if (bXTSTestEnabled)
1.1.1.7   root     4354:                                {
1.1.1.13  root     4355:                                        UINT64_STRUCT structDataUnitNo;
                   4356: 
                   4357:                                        /* XTS mode */
1.1.1.8   root     4358: 
                   4359:                                        ci = crypto_open ();
                   4360:                                        if (!ci)
                   4361:                                                return 1;
                   4362: 
1.1.1.13  root     4363:                                        ci->mode = XTS;
1.1.1.12  root     4364: 
                   4365:                                        for (ci->ea = EAGetFirst (); ci->ea != 0 ; ci->ea = EAGetNext (ci->ea))
                   4366:                                                if (EAGetCipherCount (ci->ea) == 1 && EAGetFirstCipher (ci->ea) == idTestCipher)
                   4367:                                                        break;
1.1.1.8   root     4368: 
1.1.1.13  root     4369:                                        if ((tmpRetVal = EAInit (ci->ea, key, ci->ks)) != ERR_SUCCESS)
1.1.1.9   root     4370:                                        {
                   4371:                                                handleError (hwndDlg, tmpRetVal);
1.1.1.8   root     4372:                                                return 1;
1.1.1.9   root     4373:                                        }
1.1.1.8   root     4374: 
1.1.1.13  root     4375:                                        memcpy (&ci->k2, secondaryKey, sizeof (secondaryKey));
1.1.1.8   root     4376:                                        if (!EAInitMode (ci))
                   4377:                                                return 1;
                   4378: 
1.1.1.13  root     4379:                                        structDataUnitNo.Value = BE64(((unsigned __int64 *)dataUnitNo)[0]);
1.1.1.12  root     4380: 
1.1.1.13  root     4381:                                        if (bEncrypt)
                   4382:                                                EncryptBufferXTS (tmp, pt, &structDataUnitNo, blockNo, (unsigned char *) (ci->ks), (unsigned char *) ci->ks2, idTestCipher);
                   4383:                                        else
                   4384:                                                DecryptBufferXTS (tmp, pt, &structDataUnitNo, blockNo, (unsigned char *) (ci->ks), (unsigned char *) ci->ks2, idTestCipher);
1.1.1.9   root     4385: 
1.1.1.8   root     4386:                                        crypto_close (ci);
1.1.1.7   root     4387:                                }
1.1.1.8   root     4388:                                else
                   4389:                                {
                   4390:                                        if (idTestCipher == BLOWFISH)
                   4391:                                        {
1.1.1.12  root     4392:                                                /* Deprecated/legacy */
                   4393: 
1.1.1.8   root     4394:                                                /* Convert to little-endian, this is needed here and not in
                   4395:                                                above auto-tests because BF_ecb_encrypt above correctly converts
                   4396:                                                from big to little endian, and EncipherBlock does not! */
                   4397:                                                LongReverse((void*)tmp, pt);
                   4398:                                        }
                   4399: 
                   4400:                                        CipherInit2(idTestCipher, key, ks_tmp, ks);
                   4401: 
                   4402:                                        if (bEncrypt)
                   4403:                                        {
                   4404:                                                EncipherBlock(idTestCipher, tmp, ks_tmp);
                   4405:                                        }
                   4406:                                        else
                   4407:                                        {
                   4408:                                                DecipherBlock(idTestCipher, tmp, ks_tmp);
                   4409:                                        }
1.1.1.7   root     4410: 
1.1.1.8   root     4411:                                        if (idTestCipher == BLOWFISH)
                   4412:                                        {
1.1.1.12  root     4413:                                                /* Deprecated/legacy */
                   4414: 
1.1.1.8   root     4415:                                                /* Convert back to big-endian */
                   4416:                                                LongReverse((void*)tmp, pt);
                   4417:                                        }
                   4418:                                }
1.1.1.7   root     4419:                                *szTmp = 0;
                   4420: 
1.1.1.8   root     4421:                                for (n = 0; n < pt; n ++)
1.1.1.7   root     4422:                                {
                   4423:                                        char szTmp2[3];
                   4424:                                        sprintf(szTmp2, "%02x", (int)((unsigned char)tmp[n]));
                   4425:                                        strcat(szTmp, szTmp2);
                   4426:                                }
                   4427: 
                   4428:                                if (bEncrypt)
                   4429:                                        SetWindowText(GetDlgItem(hwndDlg,IDC_CIPHERTEXT), szTmp);
                   4430:                                else
                   4431:                                        SetWindowText(GetDlgItem(hwndDlg,IDC_PLAINTEXT), szTmp);
                   4432:                        }
1.1.1.8   root     4433: 
1.1.1.7   root     4434:                        return 1;
                   4435:                }
                   4436: 
                   4437:                if (lw == IDCLOSE || lw == IDCANCEL)
                   4438:                {
                   4439:                        idTestCipher = -1;
                   4440:                        EndDialog (hwndDlg, 0);
                   4441:                        return 1;
                   4442:                }
                   4443:                break;
                   4444: 
                   4445:        case WM_CLOSE:
                   4446:                idTestCipher = -1;
                   4447:                EndDialog (hwndDlg, 0);
                   4448:                return 1;
1.1.1.5   root     4449:        }
1.1.1.7   root     4450: 
                   4451:        return 0;
                   4452: }
                   4453: 
                   4454: void 
                   4455: ResetCipherTest(HWND hwndDlg, int idTestCipher)
                   4456: {
                   4457:        int ndx;
                   4458: 
                   4459:        ShowWindow(GetDlgItem(hwndDlg, IDC_TESTS_MESSAGE), SW_HIDE);
                   4460:        ShowWindow(GetDlgItem(hwndDlg, IDC_REDTICK), SW_HIDE);
                   4461: 
1.1.1.11  root     4462:        EnableWindow(GetDlgItem(hwndDlg,IDC_KEY_SIZE), FALSE);
1.1.1.7   root     4463: 
                   4464:        /* Setup the keysize and plaintext sizes for the selected cipher */
                   4465: 
                   4466:        SendMessage (GetDlgItem(hwndDlg, IDC_PLAINTEXT_SIZE), CB_RESETCONTENT, 0,0);
                   4467:        SendMessage (GetDlgItem(hwndDlg, IDC_KEY_SIZE), CB_RESETCONTENT, 0,0);
1.1.1.13  root     4468:        SendMessage (GetDlgItem(hwndDlg, IDC_TEST_BLOCK_NUMBER), CB_RESETCONTENT, 0,0);
1.1.1.7   root     4469: 
                   4470:        ndx = SendMessage (GetDlgItem(hwndDlg, IDC_PLAINTEXT_SIZE), CB_ADDSTRING, 0,(LPARAM) "64");
                   4471:        SendMessage(GetDlgItem(hwndDlg, IDC_PLAINTEXT_SIZE), CB_SETITEMDATA, ndx,(LPARAM) 8);
                   4472:        SendMessage(GetDlgItem(hwndDlg, IDC_PLAINTEXT_SIZE), CB_SETCURSEL, ndx,0);
                   4473: 
1.1.1.13  root     4474:        for (ndx = 0; ndx < BLOCKS_PER_XTS_DATA_UNIT; ndx++)
                   4475:        {
                   4476:                char tmpStr [16];
                   4477: 
                   4478:                sprintf (tmpStr, "%d", ndx);
                   4479: 
                   4480:                ndx = SendMessage (GetDlgItem(hwndDlg, IDC_TEST_BLOCK_NUMBER), CB_ADDSTRING, 0,(LPARAM) tmpStr);
                   4481:                SendMessage(GetDlgItem(hwndDlg, IDC_TEST_BLOCK_NUMBER), CB_SETITEMDATA, ndx,(LPARAM) ndx);
                   4482:        }
                   4483: 
                   4484:        SendMessage(GetDlgItem(hwndDlg, IDC_TEST_BLOCK_NUMBER), CB_SETCURSEL, 0, 0);
                   4485: 
                   4486:        SetWindowText(GetDlgItem(hwndDlg, IDC_SECONDARY_KEY), "0000000000000000000000000000000000000000000000000000000000000000");
                   4487:        SetWindowText(GetDlgItem(hwndDlg, IDC_TEST_DATA_UNIT_NUMBER), "0");
1.1.1.8   root     4488: 
1.1.1.7   root     4489:        if (idTestCipher == BLOWFISH)
                   4490:        {
1.1.1.12  root     4491:                /* Deprecated/legacy */
                   4492: 
1.1.1.7   root     4493:                ndx = SendMessage (GetDlgItem(hwndDlg, IDC_KEY_SIZE), CB_ADDSTRING, 0,(LPARAM) "448");
                   4494:                SendMessage(GetDlgItem(hwndDlg, IDC_KEY_SIZE), CB_SETITEMDATA, ndx,(LPARAM) 56);
                   4495:                ndx = SendMessage (GetDlgItem(hwndDlg, IDC_KEY_SIZE), CB_ADDSTRING, 0,(LPARAM) "256");
                   4496:                SendMessage(GetDlgItem(hwndDlg, IDC_KEY_SIZE), CB_SETITEMDATA, ndx,(LPARAM) 32);
                   4497:                ndx = SendMessage (GetDlgItem(hwndDlg, IDC_KEY_SIZE), CB_ADDSTRING, 0,(LPARAM) "128");
                   4498:                SendMessage(GetDlgItem(hwndDlg, IDC_KEY_SIZE), CB_SETITEMDATA, ndx,(LPARAM) 16);
                   4499:                ndx = SendMessage (GetDlgItem(hwndDlg, IDC_KEY_SIZE), CB_ADDSTRING, 0,(LPARAM) "64");
                   4500:                SendMessage(GetDlgItem(hwndDlg, IDC_KEY_SIZE), CB_SETITEMDATA, ndx,(LPARAM) 8);
                   4501:                SendMessage(GetDlgItem(hwndDlg, IDC_KEY_SIZE), CB_SETCURSEL, 0,0);
                   4502:                SetWindowText(GetDlgItem(hwndDlg, IDC_KEY), "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000");
                   4503:        } 
                   4504: 
                   4505: 
                   4506:        if (idTestCipher == CAST)
                   4507:        {
1.1.1.12  root     4508:                /* Deprecated/legacy */
                   4509: 
1.1.1.7   root     4510:                ndx = SendMessage (GetDlgItem(hwndDlg, IDC_KEY_SIZE), CB_ADDSTRING, 0,(LPARAM) "128");
                   4511:                SendMessage(GetDlgItem(hwndDlg, IDC_KEY_SIZE), CB_SETITEMDATA, ndx,(LPARAM) 16);
                   4512:                SendMessage(GetDlgItem(hwndDlg, IDC_KEY_SIZE), CB_SETCURSEL, ndx,0);
                   4513:                SetWindowText(GetDlgItem(hwndDlg, IDC_KEY), "00000000000000000000000000000000");
                   4514:        }
                   4515: 
                   4516:        if (idTestCipher == TRIPLEDES)
                   4517:        {
1.1.1.12  root     4518:                /* Deprecated/legacy */
                   4519: 
1.1.1.7   root     4520:                ndx = SendMessage (GetDlgItem(hwndDlg, IDC_KEY_SIZE), CB_ADDSTRING, 0,(LPARAM) "168");
                   4521:                SendMessage(GetDlgItem(hwndDlg, IDC_KEY_SIZE), CB_SETITEMDATA, ndx,(LPARAM) 24);
                   4522:                SendMessage(GetDlgItem(hwndDlg, IDC_KEY_SIZE), CB_SETCURSEL, ndx,0);
                   4523:                SetWindowText(GetDlgItem(hwndDlg, IDC_KEY), "000000000000000000000000000000000000000000000000");
                   4524:        }
                   4525: 
                   4526:        if (idTestCipher == DES56)
                   4527:        {
1.1.1.12  root     4528:                /* Deprecated/legacy */
                   4529: 
1.1.1.7   root     4530:                ndx = SendMessage (GetDlgItem(hwndDlg, IDC_KEY_SIZE), CB_ADDSTRING, 0,(LPARAM) "56");
                   4531:                SendMessage(GetDlgItem(hwndDlg, IDC_KEY_SIZE), CB_SETITEMDATA, ndx,(LPARAM) 7);
                   4532:                SendMessage(GetDlgItem(hwndDlg, IDC_KEY_SIZE), CB_SETCURSEL, 0,0);
                   4533:                SetWindowText(GetDlgItem(hwndDlg, IDC_KEY), "0000000000000000");
                   4534:        }
                   4535:        
                   4536:        SetWindowText(GetDlgItem(hwndDlg, IDC_PLAINTEXT), "0000000000000000");
                   4537:        SetWindowText(GetDlgItem(hwndDlg, IDC_CIPHERTEXT), "0000000000000000");
                   4538: 
                   4539:        if (idTestCipher == AES || idTestCipher == SERPENT || idTestCipher == TWOFISH)
                   4540:        {
                   4541:                ndx = SendMessage (GetDlgItem(hwndDlg, IDC_KEY_SIZE), CB_ADDSTRING, 0,(LPARAM) "256");
                   4542:                SendMessage(GetDlgItem(hwndDlg, IDC_KEY_SIZE), CB_SETITEMDATA, ndx,(LPARAM) 32);
                   4543:                SendMessage(GetDlgItem(hwndDlg, IDC_KEY_SIZE), CB_SETCURSEL, ndx,0);
                   4544: 
                   4545:                SendMessage (GetDlgItem(hwndDlg, IDC_PLAINTEXT_SIZE), CB_RESETCONTENT, 0,0);
                   4546:                ndx = SendMessage (GetDlgItem(hwndDlg, IDC_PLAINTEXT_SIZE), CB_ADDSTRING, 0,(LPARAM) "128");
                   4547:                SendMessage(GetDlgItem(hwndDlg, IDC_PLAINTEXT_SIZE), CB_SETITEMDATA, ndx,(LPARAM) 16);
                   4548:                SendMessage(GetDlgItem(hwndDlg, IDC_PLAINTEXT_SIZE), CB_SETCURSEL, ndx,0);
                   4549: 
                   4550:                SetWindowText(GetDlgItem(hwndDlg, IDC_KEY), "0000000000000000000000000000000000000000000000000000000000000000");
                   4551:                SetWindowText(GetDlgItem(hwndDlg, IDC_PLAINTEXT), "00000000000000000000000000000000");
                   4552:                SetWindowText(GetDlgItem(hwndDlg, IDC_CIPHERTEXT), "00000000000000000000000000000000");
                   4553:        }
                   4554: }
                   4555: 
                   4556: #endif // #ifndef SETUP
                   4557: 
                   4558: 
1.1.1.11  root     4559: BOOL CALLBACK MultiChoiceDialogProc (HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
                   4560: {
                   4561:        char **pStr = (char **) lParam;
                   4562:        char **pStrOrig = pStr;
                   4563:        wchar_t **pwStr = (wchar_t **) lParam;
                   4564:        wchar_t **pwStrOrig = pwStr;
                   4565:        int nChoiceIDs [MAX_MULTI_CHOICES+1] = { IDC_MULTI_CHOICE_MSG, IDC_CHOICE1, IDC_CHOICE2, IDC_CHOICE3,
                   4566:                IDC_CHOICE4, IDC_CHOICE5, IDC_CHOICE6, IDC_CHOICE7, IDC_CHOICE8, IDC_CHOICE9, IDC_CHOICE10 };
                   4567:        int nBaseButtonWidth = 0;
                   4568:        int nBaseButtonHeight = 0;
                   4569:        int nActiveChoices = -1;
                   4570:        int nStr = 0;
                   4571:        int vertSubOffset, horizSubOffset, vertMsgHeightOffset;
                   4572:        int vertOffset = 0;
                   4573:        int nLongestButtonCaptionWidth = 6;
                   4574:        int nTextGfxLineHeight = 0;
                   4575:        RECT rec, wrec, wtrec, trec;
                   4576:        BOOL bResolve;
                   4577: 
                   4578:        WORD lw = LOWORD (wParam);
                   4579: 
                   4580:        switch (uMsg)
                   4581:        {
                   4582:        case WM_INITDIALOG:
                   4583:                {
                   4584:                        LocalizeDialog (hwndDlg, NULL);
                   4585: 
                   4586:                        SetWindowPos (hwndDlg, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
                   4587:                        SetWindowPos (hwndDlg, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
                   4588: 
                   4589:                        bResolve = (*pStr == NULL);
                   4590: 
                   4591:                        // Process the strings
                   4592:                        pStr++;
                   4593:                        pwStr++;
                   4594: 
                   4595:                        do 
                   4596:                        {
                   4597:                                if (*pStr != 0)
                   4598:                                {
                   4599:                                        SetWindowTextW (GetDlgItem(hwndDlg, nChoiceIDs[nStr]), bResolve ? GetString(*pStr) : *pwStr);
                   4600: 
                   4601:                                        if (nStr > 0)
                   4602:                                        {
                   4603:                                                nLongestButtonCaptionWidth = max (
                   4604:                                                        GetTextGfxWidth (GetDlgItem(hwndDlg, IDC_CHOICE1),
                   4605:                                                                                        bResolve ? GetString(*pStr) : *pwStr,
                   4606:                                                                                        hUserFont),
                   4607:                                                        nLongestButtonCaptionWidth);
                   4608:                                        }
                   4609: 
                   4610:                                        nActiveChoices++;
                   4611:                                        pStr++;
                   4612:                                        pwStr++;
                   4613:                                }
                   4614:                                else
                   4615:                                {
                   4616:                                        ShowWindow(GetDlgItem(hwndDlg, nChoiceIDs[nStr]), SW_HIDE);
                   4617:                                }
                   4618:                                nStr++;
                   4619: 
                   4620:                        } while (nStr < MAX_MULTI_CHOICES+1);
                   4621: 
                   4622:                        // Get the window coords
                   4623:                        GetWindowRect(hwndDlg, &wrec);
                   4624: 
                   4625:                        // Get the base button size
                   4626:                        GetClientRect(GetDlgItem(hwndDlg, IDC_CHOICE1), &rec);
                   4627:                        nBaseButtonWidth = rec.right + 2;
                   4628:                        nBaseButtonHeight = rec.bottom + 2;
                   4629: 
                   4630:                        // Increase in width based on the gfx length of the widest button caption
1.1.1.12  root     4631:                        horizSubOffset = min (CompensateXDPI (500), max (0, nLongestButtonCaptionWidth + CompensateXDPI (50) - nBaseButtonWidth));
1.1.1.11  root     4632: 
                   4633:                        // Vertical "title bar" offset
                   4634:                        GetClientRect(hwndDlg, &wtrec);
                   4635:                        vertOffset = wrec.bottom - wrec.top - wtrec.bottom - GetSystemMetrics(SM_CYFIXEDFRAME);
                   4636: 
                   4637:                        // Height/width of the message text
                   4638:                        GetClientRect(GetDlgItem(hwndDlg, IDC_MULTI_CHOICE_MSG), &trec);
                   4639: 
                   4640:                        nTextGfxLineHeight = GetTextGfxHeight (GetDlgItem(hwndDlg, IDC_MULTI_CHOICE_MSG),
                   4641:                                                                bResolve ? GetString(*(pStrOrig+1)) : *(pwStrOrig+1),
                   4642:                                                                hUserFont);
                   4643: 
                   4644:                        vertMsgHeightOffset = ((GetTextGfxWidth (GetDlgItem(hwndDlg, IDC_MULTI_CHOICE_MSG),
                   4645:                                                                bResolve ? GetString(*(pStrOrig+1)) : *(pwStrOrig+1),
                   4646:                                                                hUserFont) / (trec.right + horizSubOffset) + 1) * nTextGfxLineHeight) - trec.bottom;
                   4647: 
1.1.1.12  root     4648:                        vertMsgHeightOffset = min (CompensateYDPI (350), vertMsgHeightOffset + nTextGfxLineHeight + (trec.bottom + vertMsgHeightOffset) / 10);
1.1.1.11  root     4649: 
                   4650:                        // Reduction in height according to the number of shown buttons
                   4651:                        vertSubOffset = ((MAX_MULTI_CHOICES - nActiveChoices) * nBaseButtonHeight);
                   4652: 
                   4653:                        if (horizSubOffset > 0 
                   4654:                                || vertMsgHeightOffset > 0 
                   4655:                                || vertOffset > 0)
                   4656:                        {
                   4657:                                // Resize/move each button if necessary
                   4658:                                for (nStr = 1; nStr < MAX_MULTI_CHOICES+1; nStr++)
                   4659:                                {
                   4660:                                        GetWindowRect(GetDlgItem(hwndDlg, nChoiceIDs[nStr]), &rec);
                   4661: 
                   4662:                                        MoveWindow (GetDlgItem(hwndDlg, nChoiceIDs[nStr]),
                   4663:                                                rec.left - wrec.left - GetSystemMetrics(SM_CXFIXEDFRAME),
                   4664:                                                rec.top - wrec.top - vertOffset + vertMsgHeightOffset,
                   4665:                                                nBaseButtonWidth + horizSubOffset,
                   4666:                                                nBaseButtonHeight,
                   4667:                                                TRUE);
                   4668:                                }
                   4669: 
                   4670:                                // Resize/move the remaining GUI elements
                   4671:                                GetWindowRect(GetDlgItem(hwndDlg, IDC_MULTI_CHOICE_MSG), &rec);
                   4672:                                GetClientRect(GetDlgItem(hwndDlg, IDC_MULTI_CHOICE_MSG), &trec);
                   4673:                                MoveWindow (GetDlgItem(hwndDlg, IDC_MULTI_CHOICE_MSG),
                   4674:                                        rec.left - wrec.left - GetSystemMetrics(SM_CXFIXEDFRAME),
                   4675:                                        rec.top - wrec.top - vertOffset,
                   4676:                                        trec.right + 2 + horizSubOffset,
                   4677:                                        trec.bottom + 2 + vertMsgHeightOffset,
                   4678:                                        TRUE);
                   4679: 
                   4680:                                GetWindowRect(GetDlgItem(hwndDlg, IDC_MC_DLG_HR1), &rec);
                   4681:                                GetClientRect(GetDlgItem(hwndDlg, IDC_MC_DLG_HR1), &trec);
                   4682:                                MoveWindow (GetDlgItem(hwndDlg, IDC_MC_DLG_HR1),
                   4683:                                        rec.left - wrec.left - GetSystemMetrics(SM_CXFIXEDFRAME),
                   4684:                                        rec.top - wrec.top - vertOffset,
                   4685:                                        trec.right + 2 + horizSubOffset,
                   4686:                                        trec.bottom + 2,
                   4687:                                        TRUE);
                   4688:                                
                   4689:                                GetWindowRect(GetDlgItem(hwndDlg, IDC_MC_DLG_HR2), &rec);
                   4690:                                GetClientRect(GetDlgItem(hwndDlg, IDC_MC_DLG_HR2), &trec);
                   4691:                                MoveWindow (GetDlgItem(hwndDlg, IDC_MC_DLG_HR2),
                   4692:                                        rec.left - wrec.left - GetSystemMetrics(SM_CXFIXEDFRAME),
                   4693:                                        rec.top - wrec.top - vertOffset + vertMsgHeightOffset,
                   4694:                                        trec.right + 2 + horizSubOffset,
                   4695:                                        trec.bottom + 2,
                   4696:                                        TRUE);
                   4697:                        }
                   4698: 
                   4699:                        // Resize the window according to number of shown buttons and the longest button caption
                   4700:                        MoveWindow (hwndDlg,
                   4701:                                wrec.left - horizSubOffset / 2,
                   4702:                                wrec.top + vertSubOffset / 2 - vertMsgHeightOffset / 2,
                   4703:                                wrec.right - wrec.left + horizSubOffset,
                   4704:                                wrec.bottom - wrec.top - vertSubOffset + 1 + vertMsgHeightOffset,
                   4705:                                TRUE);
                   4706: 
                   4707:                        return 1;
                   4708:                }
                   4709: 
                   4710:        case WM_COMMAND:
                   4711: 
                   4712:                if (lw == IDCLOSE || lw == IDCANCEL)
                   4713:                {
                   4714:                        EndDialog (hwndDlg, 0);
                   4715:                        return 1;
                   4716:                }
                   4717: 
                   4718:                for (nStr = 1; nStr < MAX_MULTI_CHOICES+1; nStr++)
                   4719:                {
                   4720:                        if (lw == nChoiceIDs[nStr])
                   4721:                        {
                   4722:                                EndDialog (hwndDlg, nStr);
                   4723:                                return 1;
                   4724:                        }
                   4725:                }
                   4726:                break;
                   4727: 
                   4728:        case WM_CLOSE:
                   4729:                EndDialog (hwndDlg, 0);
                   4730:                return 1;
                   4731:        }
                   4732: 
                   4733:        return 0;
                   4734: }
                   4735: 
                   4736: 
1.1.1.7   root     4737: BOOL CheckCapsLock (HWND hwnd, BOOL quiet)
                   4738: {
                   4739:        if ((GetKeyState(VK_CAPITAL) & 1) != 0) 
                   4740:        {
                   4741:                if (!quiet)
                   4742:                {
                   4743:                        MessageBoxW (hwnd, GetString ("CAPSLOCK_ON"), lpszTitle, MB_ICONEXCLAMATION);
                   4744:                }
                   4745:                return TRUE;
                   4746:        }
                   4747:        return FALSE;
1.1.1.5   root     4748: }
                   4749: 
                   4750: 
1.1.1.13  root     4751: // Checks whether the file extension is not used for executable files or similarly problematic, which often
                   4752: // causes Windows and antivirus software to interfere with the container 
1.1.1.11  root     4753: BOOL CheckFileExtension (char *fileName)
                   4754: {
1.1.1.13  root     4755:        int i = 0;
1.1.1.12  root     4756:        char *ext = strrchr (fileName, '.');
1.1.1.13  root     4757:        static char *problemFileExt[] = {
                   4758:                // These are protected by the Windows Resource Protection
                   4759:                ".asa", ".asp", ".aspx", ".ax", ".bas", ".bat", ".bin", ".cer", ".chm", ".clb", ".cmd", ".cnt", ".cnv",
                   4760:                ".com", ".cpl", ".cpx", ".crt", ".csh", ".dll", ".drv", ".dtd", ".exe", ".fxp", ".grp", ".h1s", ".hlp",
                   4761:                ".hta", ".ime", ".inf", ".ins", ".isp", ".its", ".js", ".jse", ".ksh", ".lnk", ".mad", ".maf", ".mag",
                   4762:                ".mam", ".man", ".maq", ".mar", ".mas", ".mat", ".mau", ".mav", ".maw", ".mda", ".mdb", ".mde", ".mdt",
                   4763:                ".mdw", ".mdz", ".msc", ".msi", ".msp", ".mst", ".mui", ".nls", ".ocx", ".ops", ".pal", ".pcd", ".pif",
                   4764:                ".prf", ".prg", ".pst", ".reg", ".scf", ".scr", ".sct", ".shb", ".shs", ".sys", ".tlb", ".tsp", ".url",
                   4765:                ".vb", ".vbe", ".vbs", ".vsmacros", ".vss", ".vst", ".vsw", ".ws", ".wsc", ".wsf", ".wsh", ".xsd", ".xsl",
                   4766:                // These additional file extensions are usually watched by antivirus programs
                   4767:                ".386", ".acm", ".ade", ".adp", ".ani", ".app", ".asd", ".asf", ".asx", ".awx", ".ax", ".boo", ".cdf",
                   4768:                ".class", ".dhtm", ".dhtml",".dlo", ".emf", ".eml", ".flt", ".fot", ".hlp", ".htm", ".html", ".ini", 
                   4769:                ".j2k", ".jar", ".jff", ".jif", ".jmh", ".jng", ".jp2", ".jpe", ".jpeg", ".jpg", ".lsp", ".mod", ".nws",
                   4770:                ".obj", ".olb", ".osd", ".ov1", ".ov2", ".ov3", ".ovl", ".ovl", ".ovr", ".pdr", ".pgm", ".php", ".pkg",
                   4771:                ".pl", ".png", ".pot", ".pps", ".ppt", ".rar", ".rpl", ".rtf", ".sbf", ".script", ".sh", ".sha", ".shtm",
                   4772:                ".shtml", ".spl", ".swf", ".tmp", ".ttf", ".vcs", ".vlm", ".vxd", ".vxo", ".wiz", ".wll", ".wmd", ".wmf",
                   4773:                ".wms", ".wmz", ".wpc", ".wsc", ".wsh", ".wwk", ".xhtm", ".xhtml", ".xl", ".xml", ".zip", ".7z",
                   4774:                0};
1.1.1.11  root     4775: 
1.1.1.13  root     4776:        if (!ext)
                   4777:                return FALSE;
                   4778: 
                   4779:        while (problemFileExt[i])
                   4780:        {
                   4781:                if (!_stricmp (ext, problemFileExt[i++]))
                   4782:                        return TRUE;
                   4783:        }
1.1.1.11  root     4784: 
                   4785:        return FALSE;
                   4786: }
                   4787: 
                   4788: 
1.1.1.5   root     4789: int GetFirstAvailableDrive ()
                   4790: {
                   4791:        DWORD dwUsedDrives = GetLogicalDrives();
                   4792:        int i;
                   4793: 
                   4794:        for (i = 3; i < 26; i++)
                   4795:        {
                   4796:                if (!(dwUsedDrives & 1 << i))
                   4797:                        return i;
                   4798:        }
                   4799: 
                   4800:        return -1;
                   4801: }
                   4802: 
                   4803: 
                   4804: int GetLastAvailableDrive ()
                   4805: {
                   4806:        DWORD dwUsedDrives = GetLogicalDrives();
                   4807:        int i;
                   4808: 
                   4809:        for (i = 25; i > 2; i--)
                   4810:        {
                   4811:                if (!(dwUsedDrives & 1 << i))
                   4812:                        return i;
                   4813:        }
                   4814: 
                   4815:        return -1;
                   4816: }
                   4817: 
                   4818: 
                   4819: BOOL IsDriveAvailable (int driveNo)
                   4820: {
                   4821:        return (GetLogicalDrives() & (1 << driveNo)) == 0;
                   4822: }
                   4823: 
                   4824: 
1.1.1.11  root     4825: BOOL IsDeviceMounted (char *deviceName)
                   4826: {
                   4827:        BOOL bResult = FALSE;
                   4828:        DWORD dwResult;
                   4829:        HANDLE dev = INVALID_HANDLE_VALUE;
                   4830: 
                   4831:        if ((dev = CreateFile (deviceName,
1.1.1.12  root     4832:                GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE,
1.1.1.11  root     4833:                NULL,
                   4834:                OPEN_EXISTING,
                   4835:                0,
                   4836:                NULL)) != INVALID_HANDLE_VALUE)
                   4837:        {
                   4838:                bResult = DeviceIoControl (dev, FSCTL_IS_VOLUME_MOUNTED, NULL, 0, NULL, 0, &dwResult, NULL);
                   4839:                CloseHandle (dev);
                   4840:        }
                   4841: 
                   4842:        return bResult;
                   4843: }
                   4844: 
                   4845: 
1.1.1.5   root     4846: int DriverUnmountVolume (HWND hwndDlg, int nDosDriveNo, BOOL forced)
                   4847: {
                   4848:        UNMOUNT_STRUCT unmount;
                   4849:        DWORD dwResult;
                   4850: 
                   4851:        BOOL bResult;
                   4852:        
                   4853:        unmount.nDosDriveNo = nDosDriveNo;
                   4854:        unmount.ignoreOpenFiles = forced;
                   4855: 
1.1.1.13  root     4856:        bResult = DeviceIoControl (hDriver, TC_IOCTL_DISMOUNT_VOLUME, &unmount,
1.1.1.5   root     4857:                sizeof (unmount), &unmount, sizeof (unmount), &dwResult, NULL);
                   4858: 
                   4859:        if (bResult == FALSE)
                   4860:        {
                   4861:                handleWin32Error (hwndDlg);
                   4862:                return 1;
                   4863:        }
                   4864: 
                   4865:        return unmount.nReturnCode;
                   4866: }
                   4867: 
                   4868: 
1.1.1.6   root     4869: void BroadcastDeviceChange (WPARAM message, int nDosDriveNo, DWORD driveMap)
1.1.1.5   root     4870: {
                   4871:        DEV_BROADCAST_VOLUME dbv;
1.1.1.6   root     4872:        DWORD dwResult;
1.1.1.11  root     4873:        LONG eventId = 0;
1.1.1.7   root     4874:        int i;
1.1.1.6   root     4875: 
1.1.1.10  root     4876:        if (message == DBT_DEVICEARRIVAL)
1.1.1.11  root     4877:                eventId = SHCNE_DRIVEADD;
1.1.1.10  root     4878:        else if (message == DBT_DEVICEREMOVECOMPLETE)
1.1.1.11  root     4879:                eventId = SHCNE_DRIVEREMOVED;
1.1.1.5   root     4880: 
1.1.1.7   root     4881:        if (driveMap == 0)
1.1.1.10  root     4882:                driveMap = (1 << nDosDriveNo);
                   4883: 
1.1.1.11  root     4884:        if (eventId != 0)
1.1.1.7   root     4885:        {
1.1.1.11  root     4886:                for (i = 0; i < 26; i++)
1.1.1.7   root     4887:                {
1.1.1.11  root     4888:                        if (driveMap & (1 << i))
1.1.1.7   root     4889:                        {
1.1.1.11  root     4890:                                char root[] = {i + 'A', ':', '\\', 0 };
                   4891:                                SHChangeNotify (eventId, SHCNF_PATH, root, NULL);
1.1.1.10  root     4892: 
1.1.1.11  root     4893:                                if (nCurrentOS == WIN_2000 && RemoteSession)
                   4894:                                {
                   4895:                                        char target[32];
                   4896:                                        wsprintf (target, "%ls%c", TC_MOUNT_PREFIX, i + 'A');
                   4897:                                        root[2] = 0;
                   4898: 
                   4899:                                        if (message == DBT_DEVICEARRIVAL)
                   4900:                                                DefineDosDevice (DDD_RAW_TARGET_PATH, root, target);
                   4901:                                        else if (message == DBT_DEVICEREMOVECOMPLETE)
                   4902:                                                DefineDosDevice (DDD_RAW_TARGET_PATH| DDD_REMOVE_DEFINITION
1.1.1.10  root     4903:                                                | DDD_EXACT_MATCH_ON_REMOVE, root, target);
1.1.1.11  root     4904:                                }
1.1.1.7   root     4905:                        }
                   4906:                }
                   4907:        }
1.1.1.5   root     4908: 
1.1.1.11  root     4909:        dbv.dbcv_size = sizeof (dbv); 
1.1.1.5   root     4910:        dbv.dbcv_devicetype = DBT_DEVTYP_VOLUME; 
                   4911:        dbv.dbcv_reserved = 0;
1.1.1.10  root     4912:        dbv.dbcv_unitmask = driveMap;
1.1.1.5   root     4913:        dbv.dbcv_flags = 0; 
                   4914: 
1.1.1.12  root     4915:        IgnoreWmDeviceChange = TRUE;
1.1.1.10  root     4916:        SendMessageTimeout (HWND_BROADCAST, WM_DEVICECHANGE, message, (LPARAM)(&dbv), 0, 1000, &dwResult);
1.1.1.12  root     4917: 
                   4918:        // Explorer sometimes fails to register a new drive
                   4919:        if (message == DBT_DEVICEARRIVAL)
                   4920:                SendMessageTimeout (HWND_BROADCAST, WM_DEVICECHANGE, message, (LPARAM)(&dbv), 0, 200, &dwResult);
                   4921:        IgnoreWmDeviceChange = FALSE;
1.1.1.5   root     4922: }
                   4923: 
                   4924: 
1.1.1.7   root     4925: // Use only cached passwords if password = NULL
                   4926: //
1.1.1.5   root     4927: // Returns:
                   4928: // -1 = user aborted mount / error
                   4929: // 0  = mount failed
                   4930: // 1  = mount OK
                   4931: // 2  = mount OK in shared mode
                   4932: 
                   4933: int MountVolume (HWND hwndDlg,
                   4934:                                 int driveNo,
                   4935:                                 char *volumePath,
1.1.1.7   root     4936:                                 Password *password,
1.1.1.5   root     4937:                                 BOOL cachePassword,
                   4938:                                 BOOL sharedAccess,
1.1.1.6   root     4939:                                 MountOptions *mountOptions,
1.1.1.7   root     4940:                                 BOOL quiet,
                   4941:                                 BOOL bReportWrongPassword)
1.1.1.5   root     4942: {
1.1.1.7   root     4943:        MOUNT_STRUCT mount;
1.1.1.5   root     4944:        DWORD dwResult;
                   4945:        BOOL bResult, bDevice;
1.1.1.11  root     4946:        char root[MAX_PATH];
1.1.1.5   root     4947: 
                   4948:        if (IsMountedVolume (volumePath))
                   4949:        {
                   4950:                if (!quiet)
1.1.1.11  root     4951:                        Error ("VOL_ALREADY_MOUNTED");
1.1.1.5   root     4952:                return -1;
                   4953:        }
                   4954: 
                   4955:        if (!IsDriveAvailable (driveNo))
1.1.1.10  root     4956:        {
1.1.1.11  root     4957:                Error ("DRIVE_LETTER_UNAVAILABLE");
1.1.1.5   root     4958:                return -1;
1.1.1.10  root     4959:        }
1.1.1.5   root     4960: 
                   4961:        // If using cached passwords, check cache status first
1.1.1.7   root     4962:        if (password == NULL && IsPasswordCacheEmpty ())
1.1.1.5   root     4963:                return 0;
                   4964: 
1.1.1.7   root     4965:        ZeroMemory (&mount, sizeof (mount));
                   4966:        mount.bExclusiveAccess = sharedAccess ? FALSE : TRUE;
1.1.1.5   root     4967: retry:
1.1.1.7   root     4968:        mount.nDosDriveNo = driveNo;
                   4969:        mount.bCache = cachePassword;
                   4970: 
                   4971:        if (password != NULL)
                   4972:                mount.VolumePassword = *password;
                   4973:        else
                   4974:                mount.VolumePassword.Length = 0;
                   4975: 
                   4976:        if (!mountOptions->ReadOnly && mountOptions->ProtectHiddenVolume)
                   4977:        {
                   4978:                mount.ProtectedHidVolPassword = mountOptions->ProtectedHidVolPassword;
                   4979:                mount.bProtectHiddenVolume = TRUE;
                   4980:        }
                   4981:        else
                   4982:                mount.bProtectHiddenVolume = FALSE;
                   4983: 
                   4984:        mount.bMountReadOnly = mountOptions->ReadOnly;
                   4985:        mount.bMountRemovable = mountOptions->Removable;
                   4986:        mount.bPreserveTimestamp = mountOptions->PreserveTimestamp;
                   4987: 
                   4988:        mount.bMountManager = TRUE;
1.1.1.5   root     4989: 
                   4990:        // Windows 2000 mount manager causes problems with remounted volumes
                   4991:        if (CurrentOSMajor == 5 && CurrentOSMinor == 0)
1.1.1.7   root     4992:                mount.bMountManager = FALSE;
1.1.1.5   root     4993: 
1.1.1.7   root     4994:        CreateFullVolumePath ((char *) mount.wszVolume, volumePath, &bDevice);
1.1.1.5   root     4995: 
1.1.1.11  root     4996:        if (!bDevice)
1.1.1.7   root     4997:        {
1.1.1.11  root     4998:                // UNC path
                   4999:                if (volumePath[0] == '\\' && volumePath[1] == '\\')
                   5000:                {
                   5001:                        _snprintf ((char *)mount.wszVolume, MAX_PATH, "UNC%s", volumePath + 1);
1.1.1.7   root     5002:                        mount.bUserContext = TRUE;
1.1.1.11  root     5003:                }
                   5004: 
                   5005:                if (GetVolumePathName (volumePath, root, sizeof (root) - 1))
                   5006:                {
                   5007:                        DWORD bps, flags, d;
                   5008:                        if (GetDiskFreeSpace (root, &d, &bps, &d, &d))
                   5009:                                mount.BytesPerSector = bps;
                   5010: 
                   5011:                        // Read-only host filesystem
                   5012:                        if (!mount.bMountReadOnly && GetVolumeInformation (root, NULL, 0,  NULL, &d, &flags, NULL, 0))
                   5013:                                mount.bMountReadOnly = (flags & FILE_READ_ONLY_VOLUME) != 0;
                   5014: 
                   5015:                        // Network drive
                   5016:                        if (GetDriveType (root) == DRIVE_REMOTE)
                   5017:                                mount.bUserContext = TRUE;
                   5018:                }
1.1.1.7   root     5019:        }
                   5020: 
                   5021:        ToUNICODE ((char *) mount.wszVolume);
1.1.1.5   root     5022: 
1.1.1.13  root     5023:        bResult = DeviceIoControl (hDriver, TC_IOCTL_MOUNT_VOLUME, &mount,
1.1.1.7   root     5024:                sizeof (mount), &mount, sizeof (mount), &dwResult, NULL);
                   5025: 
                   5026:        burn (&mount.VolumePassword, sizeof (mount.VolumePassword));
                   5027:        burn (&mount.ProtectedHidVolPassword, sizeof (mount.ProtectedHidVolPassword));
1.1.1.5   root     5028: 
                   5029:        if (bResult == FALSE)
                   5030:        {
                   5031:                // Volume already open by another process
1.1.1.8   root     5032:                if (GetLastError () == ERROR_SHARING_VIOLATION)
1.1.1.5   root     5033:                {
1.1.1.7   root     5034:                        if (mount.bExclusiveAccess == FALSE)
1.1.1.5   root     5035:                        {
                   5036:                                if (!quiet)
1.1.1.7   root     5037:                                        MessageBoxW (hwndDlg, GetString ("FILE_IN_USE_FAILED"),
1.1.1.5   root     5038:                                                lpszTitle, MB_ICONSTOP);
                   5039: 
                   5040:                                return -1;
                   5041:                        }
                   5042:                        else
                   5043:                        {
                   5044:                                if (quiet)
                   5045:                                {
1.1.1.7   root     5046:                                        mount.bExclusiveAccess = FALSE;
1.1.1.5   root     5047:                                        goto retry;
                   5048:                                }
                   5049: 
                   5050:                                // Ask user 
1.1.1.7   root     5051:                                if (IDYES == MessageBoxW (hwndDlg, GetString ("FILE_IN_USE"),
1.1.1.5   root     5052:                                        lpszTitle, MB_YESNO | MB_DEFBUTTON2 | MB_ICONEXCLAMATION))
                   5053:                                {
1.1.1.7   root     5054:                                        mount.bExclusiveAccess = FALSE;
1.1.1.5   root     5055:                                        goto retry;
                   5056:                                }
                   5057:                        }
                   5058: 
                   5059:                        return -1;
                   5060:                }
                   5061: 
1.1.1.8   root     5062:                // Mount failed in kernel space => retry in user process context
                   5063:                if (!mount.bUserContext)
                   5064:                {
                   5065:                        mount.bUserContext = TRUE;
                   5066:                        goto retry;
                   5067:                }
                   5068: 
1.1.1.5   root     5069:                if (!quiet)
                   5070:                        handleWin32Error (hwndDlg);
                   5071: 
                   5072:                return -1;
                   5073:        }
                   5074: 
1.1.1.7   root     5075:        if (mount.nReturnCode != 0)
1.1.1.5   root     5076:        {
1.1.1.7   root     5077:                if (mount.nReturnCode == ERR_PASSWORD_WRONG)
                   5078:                {
                   5079:                        // Do not report wrong password, if not instructed to 
                   5080:                        if (bReportWrongPassword)
                   5081:                                handleError (hwndDlg, mount.nReturnCode);
                   5082: 
                   5083:                        return 0;
                   5084:                }
1.1.1.5   root     5085: 
1.1.1.7   root     5086:                if (!quiet)
                   5087:                        handleError (hwndDlg, mount.nReturnCode);
1.1.1.5   root     5088: 
                   5089:                return 0;
                   5090:        }
                   5091: 
1.1.1.6   root     5092:        BroadcastDeviceChange (DBT_DEVICEARRIVAL, driveNo, 0);
1.1.1.5   root     5093: 
1.1.1.7   root     5094:        if (mount.bExclusiveAccess == FALSE)
1.1.1.5   root     5095:                return 2;
                   5096: 
                   5097:        return 1;
                   5098: }
                   5099: 
                   5100: 
1.1.1.11  root     5101: BOOL UnmountVolume (HWND hwndDlg, int nDosDriveNo, BOOL forceUnmount)
1.1.1.5   root     5102: {
                   5103:        int result;
                   5104:        BOOL forced = forceUnmount;
1.1.1.6   root     5105:        int dismountMaxRetries = UNMOUNT_MAX_AUTO_RETRIES;
1.1.1.5   root     5106: 
                   5107: retry:
1.1.1.11  root     5108:        BroadcastDeviceChange (DBT_DEVICEREMOVEPENDING, nDosDriveNo, 0);
                   5109: 
1.1.1.6   root     5110:        do
                   5111:        {
                   5112:                result = DriverUnmountVolume (hwndDlg, nDosDriveNo, forced);
                   5113: 
                   5114:                if (result == ERR_FILES_OPEN)
                   5115:                        Sleep (UNMOUNT_AUTO_RETRY_DELAY);
                   5116:                else
                   5117:                        break;
                   5118: 
                   5119:        } while (--dismountMaxRetries > 0);
1.1.1.5   root     5120: 
                   5121:        if (result != 0)
                   5122:        {
1.1.1.7   root     5123:                if (result == ERR_FILES_OPEN && !Silent)
1.1.1.5   root     5124:                {
1.1.1.12  root     5125:                        if (IDYES == AskWarnNoYes ("UNMOUNT_LOCK_FAILED"))
1.1.1.5   root     5126:                        {
                   5127:                                forced = TRUE;
                   5128:                                goto retry;
                   5129:                        }
                   5130: 
                   5131:                        return FALSE;
                   5132:                }
                   5133: 
1.1.1.7   root     5134:                Error ("UNMOUNT_FAILED");
1.1.1.5   root     5135: 
                   5136:                return FALSE;
                   5137:        } 
                   5138:        
1.1.1.6   root     5139:        BroadcastDeviceChange (DBT_DEVICEREMOVECOMPLETE, nDosDriveNo, 0);
1.1.1.5   root     5140: 
                   5141:        return TRUE;
                   5142: }
                   5143: 
                   5144: 
                   5145: BOOL IsPasswordCacheEmpty (void)
                   5146: {
                   5147:        DWORD dw;
1.1.1.13  root     5148:        return !DeviceIoControl (hDriver, TC_IOCTL_GET_PASSWORD_CACHE_STATUS, 0, 0, 0, 0, &dw, 0);
1.1.1.5   root     5149: }
                   5150: 
                   5151: BOOL IsMountedVolume (char *volname)
                   5152: {
                   5153:        MOUNT_LIST_STRUCT mlist;
                   5154:        DWORD dwResult;
                   5155:        int i;
                   5156:        char volume[TC_MAX_PATH*2+16];
                   5157: 
                   5158:        strcpy (volume, volname);
1.1.1.7   root     5159: 
                   5160:        if (strstr (volname, "\\Device\\") != volname)
                   5161:                sprintf(volume, "\\??\\%s", volname);
                   5162:        ToUNICODE (volume);
1.1.1.5   root     5163: 
                   5164:        memset (&mlist, 0, sizeof (mlist));
1.1.1.13  root     5165:        DeviceIoControl (hDriver, TC_IOCTL_GET_MOUNTED_VOLUMES, &mlist,
1.1.1.5   root     5166:                sizeof (mlist), &mlist, sizeof (mlist), &dwResult,
                   5167:                NULL);
                   5168: 
                   5169:        for (i=0 ; i<26; i++)
1.1.1.7   root     5170:                if (0 == wcscmp (mlist.wszVolume[i], (WCHAR *)volume))
1.1.1.5   root     5171:                        return TRUE;
                   5172: 
                   5173:        return FALSE;
                   5174: }
                   5175: 
                   5176: 
1.1.1.12  root     5177: int GetMountedVolumeDriveNo (char *volname)
                   5178: {
                   5179:        MOUNT_LIST_STRUCT mlist;
                   5180:        DWORD dwResult;
                   5181:        int i;
                   5182:        char volume[TC_MAX_PATH*2+16];
                   5183: 
                   5184:        if (volname == NULL)
                   5185:                return -1;
                   5186: 
                   5187:        strcpy (volume, volname);
                   5188: 
                   5189:        if (strstr (volname, "\\Device\\") != volname)
                   5190:                sprintf(volume, "\\??\\%s", volname);
                   5191:        ToUNICODE (volume);
                   5192: 
                   5193:        memset (&mlist, 0, sizeof (mlist));
1.1.1.13  root     5194:        DeviceIoControl (hDriver, TC_IOCTL_GET_MOUNTED_VOLUMES, &mlist,
1.1.1.12  root     5195:                sizeof (mlist), &mlist, sizeof (mlist), &dwResult,
                   5196:                NULL);
                   5197: 
                   5198:        for (i=0 ; i<26; i++)
                   5199:                if (0 == wcscmp (mlist.wszVolume[i], (WCHAR *)volume))
                   5200:                        return i;
                   5201: 
                   5202:        return -1;
                   5203: }
                   5204: 
                   5205: 
1.1.1.5   root     5206: BOOL IsAdmin (void)
                   5207: {
1.1.1.11  root     5208:        return IsUserAnAdmin ();
                   5209: }
1.1.1.5   root     5210: 
                   5211: 
1.1.1.11  root     5212: BOOL IsUacSupported ()
                   5213: {
                   5214:        HKEY hkey;
                   5215:        DWORD value = 1, size = sizeof (DWORD);
1.1.1.5   root     5216: 
1.1.1.11  root     5217:        if (nCurrentOS != WIN_VISTA_OR_LATER)
1.1.1.5   root     5218:                return FALSE;
                   5219: 
1.1.1.11  root     5220:        if (RegOpenKeyEx (HKEY_LOCAL_MACHINE, "Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\System", 0, KEY_READ, &hkey) == ERROR_SUCCESS)
1.1.1.5   root     5221:        {
1.1.1.11  root     5222:                if (RegQueryValueEx (hkey, "EnableLUA", 0, 0, (LPBYTE) &value, &size) != ERROR_SUCCESS)
                   5223:                        value = 1;
1.1.1.5   root     5224: 
1.1.1.11  root     5225:                RegCloseKey (hkey);
1.1.1.5   root     5226:        }
                   5227: 
1.1.1.11  root     5228:        return value != 0;
1.1.1.5   root     5229: }
                   5230: 
                   5231: 
                   5232: BOOL ResolveSymbolicLink (PWSTR symLinkName, PWSTR targetName)
                   5233: {
                   5234:        BOOL bResult;
                   5235:        DWORD dwResult;
                   5236:        RESOLVE_SYMLINK_STRUCT resolve;
                   5237: 
                   5238:        memset (&resolve, 0, sizeof(resolve));
                   5239:        wcscpy ((PWSTR) &resolve.symLinkName, symLinkName);
                   5240: 
1.1.1.13  root     5241:        bResult = DeviceIoControl (hDriver, TC_IOCTL_GET_RESOLVED_SYMLINK, &resolve,
1.1.1.5   root     5242:                sizeof (resolve), &resolve, sizeof (resolve), &dwResult,
                   5243:                NULL);
                   5244: 
                   5245:        wcscpy (targetName, (PWSTR) &resolve.targetName);
                   5246: 
                   5247:        return bResult;
                   5248: }
                   5249: 
                   5250: 
1.1.1.11  root     5251: BOOL GetPartitionInfo (char *deviceName, PPARTITION_INFORMATION rpartInfo)
                   5252: {
                   5253:        BOOL bResult;
                   5254:        DWORD dwResult;
                   5255:        DISK_PARTITION_INFO_STRUCT dpi;
                   5256: 
                   5257:        memset (&dpi, 0, sizeof(dpi));
                   5258:        wsprintfW ((PWSTR) &dpi.deviceName, L"%hs", deviceName);
                   5259: 
1.1.1.13  root     5260:        bResult = DeviceIoControl (hDriver, TC_IOCTL_GET_DRIVE_PARTITION_INFO, &dpi,
1.1.1.11  root     5261:                sizeof (dpi), &dpi, sizeof (dpi), &dwResult, NULL);
                   5262: 
                   5263:        memcpy (rpartInfo, &dpi.partInfo, sizeof (PARTITION_INFORMATION));
                   5264:        return bResult;
                   5265: }
                   5266: 
                   5267: 
                   5268: BOOL GetDriveGeometry (char *deviceName, PDISK_GEOMETRY diskGeometry)
                   5269: {
                   5270:        BOOL bResult;
                   5271:        DWORD dwResult;
                   5272:        DISK_GEOMETRY_STRUCT dg;
                   5273: 
                   5274:        memset (&dg, 0, sizeof(dg));
                   5275:        wsprintfW ((PWSTR) &dg.deviceName, L"%hs", deviceName);
                   5276: 
1.1.1.13  root     5277:        bResult = DeviceIoControl (hDriver, TC_IOCTL_GET_DRIVE_GEOMETRY, &dg,
1.1.1.11  root     5278:                sizeof (dg), &dg, sizeof (dg), &dwResult, NULL);
                   5279: 
                   5280:        memcpy (diskGeometry, &dg.diskGeometry, sizeof (DISK_GEOMETRY));
                   5281:        return bResult;
                   5282: }
                   5283: 
                   5284: 
1.1.1.5   root     5285: // Returns drive letter number assigned to device (-1 if none)
                   5286: int GetDiskDeviceDriveLetter (PWSTR deviceName)
                   5287: {
                   5288:        int i;
                   5289:        WCHAR link[MAX_PATH];
                   5290:        WCHAR target[MAX_PATH];
                   5291:        WCHAR device[MAX_PATH];
                   5292: 
                   5293:        if (!ResolveSymbolicLink (deviceName, device))
                   5294:                wcscpy (device, deviceName);
                   5295: 
                   5296:        for (i = 0; i < 26; i++)
                   5297:        {
                   5298:                WCHAR drive[] = { i + 'A', ':', 0 };
                   5299: 
                   5300:                wcscpy (link, L"\\DosDevices\\");
                   5301:                wcscat (link, drive);
                   5302: 
                   5303:                ResolveSymbolicLink (link, target);
                   5304: 
                   5305:                if (wcscmp (device, target) == 0)
                   5306:                        return i;
                   5307:        }
                   5308: 
                   5309:        return -1;
                   5310: }
                   5311: 
                   5312: 
1.1.1.11  root     5313: HANDLE DismountDrive (char *devName)
1.1.1.5   root     5314: {
                   5315:        DWORD dwResult;
                   5316:        HANDLE hVolume;
1.1.1.11  root     5317:        BOOL bResult = FALSE;
                   5318:        int attempt = 10;
1.1.1.5   root     5319: 
1.1.1.11  root     5320:        hVolume = CreateFile (devName, GENERIC_READ | GENERIC_WRITE,
                   5321:                FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
1.1.1.5   root     5322: 
1.1.1.11  root     5323:        if (hVolume == INVALID_HANDLE_VALUE)
                   5324:                return INVALID_HANDLE_VALUE;
1.1.1.5   root     5325: 
1.1.1.11  root     5326:        while (!(bResult = DeviceIoControl (hVolume, FSCTL_DISMOUNT_VOLUME, NULL, 0, NULL, 0, &dwResult, NULL)) 
                   5327:                && attempt > 0)
                   5328:        {
                   5329:                Sleep (UNMOUNT_AUTO_RETRY_DELAY);
                   5330:                attempt--;
                   5331:        }
1.1.1.5   root     5332: 
1.1.1.11  root     5333:        if (!bResult)
                   5334:                CloseHandle (hVolume);
1.1.1.5   root     5335: 
1.1.1.11  root     5336:        return (bResult ? hVolume : INVALID_HANDLE_VALUE);
1.1.1.6   root     5337: }
                   5338: 
1.1.1.13  root     5339: // Returns TRUE if the file exists (otherwise FALSE).
                   5340: BOOL FileExists (const char *filePathPtr)
                   5341: {
                   5342:        char filePath [TC_MAX_PATH];
                   5343: 
                   5344:        // Strip quotation marks (if any)
                   5345:        if (filePathPtr [0] == '"')
                   5346:        {
                   5347:                strcpy (filePath, filePathPtr + 1);
                   5348:        }
                   5349:        else
                   5350:        {
                   5351:                strcpy (filePath, filePathPtr);
                   5352:        }
                   5353: 
                   5354:        // Strip quotation marks (if any)
                   5355:        if (filePath [strlen (filePath) - 1] == '"')
                   5356:                filePath [strlen (filePath) - 1] = 0;
                   5357: 
                   5358:     return (_access (filePath, 0) != -1);
                   5359: }
                   5360: 
                   5361: // Searches the file from its end for the LAST occurrence of the string str.
                   5362: // The string may contain zeroes, which do NOT terminate the string.
                   5363: // If the string is found, its offset from the start of the file is returned. 
                   5364: // If the string isn't found or if any error occurs, -1 is returned.
                   5365: __int64 FindStringInFile (char *filePath, char* str, int strLen)
                   5366: {
                   5367:        int bufSize = 64 * BYTES_PER_KB;
                   5368:        char *buffer = malloc (bufSize);
                   5369:        HANDLE src = NULL;
                   5370:        DWORD bytesRead;
                   5371:        BOOL readRetVal;
                   5372:        __int64 filePos = GetFileSize64 (filePath);
                   5373:        int bufPos = 0;
                   5374:        LARGE_INTEGER seekOffset, seekOffsetNew;
                   5375:        BOOL bExit = FALSE;
                   5376:        int filePosStep;
                   5377:        __int64 retVal = -1;
                   5378: 
                   5379:        if (filePos <= 0 
                   5380:                || buffer == NULL 
                   5381:                || strLen > bufSize
                   5382:                || strLen < 1)
                   5383:                return -1;
                   5384: 
                   5385:        src = CreateFile (filePath, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
                   5386: 
                   5387:        if (src == INVALID_HANDLE_VALUE)
                   5388:        {
                   5389:                free (buffer);
                   5390:                return -1;
                   5391:        }
                   5392: 
                   5393:        filePosStep = bufSize - strLen + 1;
                   5394: 
                   5395:        do
                   5396:        {
                   5397:                filePos -= filePosStep;
                   5398: 
                   5399:                if (filePos < 0)
                   5400:                {
                   5401:                        filePos = 0;
                   5402:                        bExit = TRUE;
                   5403:                }
                   5404: 
                   5405:                seekOffset.QuadPart = filePos;
                   5406: 
                   5407:                if (SetFilePointerEx (src, seekOffset, &seekOffsetNew, FILE_BEGIN) == 0)
                   5408:                        goto fsif_end;
                   5409: 
                   5410:                if ((readRetVal = ReadFile (src, buffer, bufSize, &bytesRead, NULL)) == 0 
                   5411:                        || bytesRead == 0)
                   5412:                        goto fsif_end;
                   5413: 
                   5414:                bufPos = bytesRead - strLen;
                   5415: 
                   5416:                while (bufPos > 0)
                   5417:                {
                   5418:                        if (memcmp (buffer + bufPos, str, strLen) == 0)
                   5419:                        {
                   5420:                                // String found
                   5421:                                retVal = filePos + bufPos;
                   5422:                                goto fsif_end;
                   5423:                        }
                   5424:                        bufPos--;
                   5425:                }
                   5426: 
                   5427:        } while (!bExit);
                   5428: 
                   5429: fsif_end:
                   5430:        CloseHandle (src);
                   5431:        free (buffer);
                   5432: 
                   5433:        return retVal;
                   5434: }
1.1.1.11  root     5435: 
1.1.1.6   root     5436: // System CopyFile() copies source file attributes (like FILE_ATTRIBUTE_ENCRYPTED)
                   5437: // so we need to use our own copy function
                   5438: BOOL TCCopyFile (char *sourceFileName, char *destinationFile)
                   5439: {
                   5440:        __int8 *buffer;
                   5441:        HANDLE src, dst;
                   5442:        FILETIME fileTime;
                   5443:        DWORD bytesRead, bytesWritten;
                   5444:        BOOL res;
                   5445: 
                   5446:        src = CreateFile (sourceFileName,
                   5447:                GENERIC_READ,
                   5448:                FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
                   5449: 
                   5450:        if (src == INVALID_HANDLE_VALUE)
                   5451:                return FALSE;
                   5452: 
                   5453:        dst = CreateFile (destinationFile,
                   5454:                GENERIC_WRITE,
                   5455:                0, NULL, CREATE_ALWAYS, 0, NULL);
                   5456: 
                   5457:        if (dst == INVALID_HANDLE_VALUE)
                   5458:        {
                   5459:                CloseHandle (src);
                   5460:                return FALSE;
                   5461:        }
                   5462: 
                   5463:        buffer = malloc (64 * 1024);
                   5464:        if (!buffer)
                   5465:        {
                   5466:                CloseHandle (src);
                   5467:                CloseHandle (dst);
                   5468:                return FALSE;
                   5469:        }
                   5470: 
                   5471:        while (res = ReadFile (src, buffer, 64 * 1024, &bytesRead, NULL))
                   5472:        {
                   5473:                if (bytesRead == 0)
                   5474:                {
                   5475:                        res = 1;
                   5476:                        break;
                   5477:                }
                   5478: 
                   5479:                if (!WriteFile (dst, buffer, bytesRead, &bytesWritten, NULL)
                   5480:                        || bytesRead != bytesWritten)
                   5481:                {
                   5482:                        res = 0;
                   5483:                        break;
                   5484:                }
                   5485:        }
                   5486: 
                   5487:        GetFileTime (src, NULL, NULL, &fileTime);
                   5488:        SetFileTime (dst, NULL, NULL, &fileTime);
                   5489: 
                   5490:        CloseHandle (src);
                   5491:        CloseHandle (dst);
                   5492: 
                   5493:        free (buffer);
                   5494:        return res != 0;
                   5495: }
                   5496: 
1.1.1.13  root     5497: // If bAppend is TRUE, the buffer is appended to an existing file. If bAppend is FALSE any existing file 
                   5498: // is replaced and if an error occurs, the incomplete file is deleted.
                   5499: BOOL SaveBufferToFile (char *inputBuffer, char *destinationFile, DWORD inputLength, BOOL bAppend)
                   5500: {
                   5501:        HANDLE dst;
                   5502:        DWORD bytesWritten;
                   5503:        BOOL res = TRUE;
                   5504: 
                   5505:        dst = CreateFile (destinationFile,
                   5506:                GENERIC_WRITE,
                   5507:                FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, bAppend ? OPEN_EXISTING : CREATE_ALWAYS, 0, NULL);
                   5508: 
                   5509:        if (dst == INVALID_HANDLE_VALUE)
                   5510:        {
                   5511:                handleWin32Error (MainDlg);
                   5512:                return FALSE;
                   5513:        }
                   5514: 
                   5515:        if (bAppend)
                   5516:                SetFilePointer (dst, 0, NULL, FILE_END);
                   5517: 
                   5518:        if (!WriteFile (dst, inputBuffer, inputLength, &bytesWritten, NULL)
                   5519:                || inputLength != bytesWritten)
                   5520:        {
                   5521:                res = FALSE;
                   5522:        }
                   5523: 
                   5524:        if (!res)
                   5525:        {
                   5526:                // If CREATE_ALWAYS is used, ERROR_ALREADY_EXISTS is returned after successful overwrite
                   5527:                // of an existing file (it's not an error)
                   5528:                if (! (GetLastError() == ERROR_ALREADY_EXISTS && !bAppend) )    
                   5529:                        handleWin32Error (MainDlg);
                   5530:        }
                   5531: 
                   5532:        CloseHandle (dst);
                   5533:        FlushFileBuffers (dst);
                   5534: 
                   5535:        if (!res && !bAppend)
                   5536:                remove (destinationFile);
                   5537: 
                   5538:        return res;
                   5539: }
                   5540: 
                   5541: // Prints a UTF-16 text (note that this involves a real printer, not a screen).
                   5542: // textByteLen - length of the text in bytes
                   5543: // title - printed as part of the page header and used as the filename for a temporary file 
                   5544: BOOL PrintHardCopyTextUTF16 (wchar_t *text, char *title, int textByteLen)
                   5545: {
                   5546:        char cl [MAX_PATH*3] = {"/p \""};
                   5547:        char path [MAX_PATH * 2] = { 0 };
                   5548:        char filename [MAX_PATH + 1] = { 0 };
                   5549: 
                   5550:        strcpy (filename, title);
                   5551:        //strcat (filename, ".txt");
                   5552: 
                   5553:        GetTempPath (sizeof (path), path);
                   5554: 
                   5555:        if (!FileExists (path))
                   5556:        {
                   5557:                strcpy (path, GetConfigPath (filename));
                   5558: 
                   5559:                if (strlen(path) < 2)
                   5560:                        return FALSE;
                   5561:        }
                   5562:        else
                   5563:        {
                   5564:                strcat (path, filename);
                   5565:        }
                   5566: 
                   5567:        // Write the Unicode signature
                   5568:        if (!SaveBufferToFile ("\xFF\xFE", path, 2, FALSE))
                   5569:        {
                   5570:                remove (path);
                   5571:                return FALSE;
                   5572:        }
                   5573: 
                   5574:        // Write the actual text
                   5575:        if (!SaveBufferToFile ((char *) text, path, textByteLen, TRUE))
                   5576:        {
                   5577:                remove (path);
                   5578:                return FALSE;
                   5579:        }
                   5580: 
                   5581:        strcat (cl, path);
                   5582:        strcat (cl, "\"");
                   5583: 
                   5584:        WaitCursor ();
                   5585:        ShellExecute (NULL, "open", PRINT_TOOL, cl, NULL, SW_HIDE);
                   5586:        Sleep (6000);
                   5587:        NormalCursor();
                   5588: 
                   5589:        remove (path);
                   5590: 
                   5591:        return TRUE;
                   5592: }
                   5593: 
                   5594: 
                   5595: #ifdef TCMOUNT
1.1.1.7   root     5596: int BackupVolumeHeader (HWND hwndDlg, BOOL bRequireConfirmation, char *lpszVolume)
                   5597: {
1.1.1.11  root     5598:        int nDosLinkCreated = 1, nStatus = ERR_OS_ERROR;
1.1.1.7   root     5599:        char szDiskFile[TC_MAX_PATH], szCFDevice[TC_MAX_PATH];
                   5600:        char szFileName[TC_MAX_PATH];
                   5601:        char szDosDevice[TC_MAX_PATH];
                   5602:        char buffer[HEADER_SIZE];
                   5603:        void *dev = INVALID_HANDLE_VALUE;
                   5604:        DWORD dwError;
                   5605:        BOOL bDevice;
                   5606:        unsigned __int64 volSize = 0;
1.1.1.11  root     5607:        wchar_t szTmp[4096];
1.1.1.7   root     5608:        int volumeType;
                   5609:        int fBackup = -1;
                   5610: 
1.1.1.13  root     5611:        switch (IsSystemDevicePath (lpszVolume, hwndDlg, TRUE))
                   5612:        {
                   5613:        case 1:
                   5614:        case 2:
                   5615:                if (AskErrNoYes ("BACKUP_HEADER_NOT_FOR_SYS_DEVICE") == IDYES)
                   5616:                        CreateRescueDisk ();
                   5617: 
                   5618:                return 0;
                   5619: 
                   5620:        case -1:
                   5621:                Error ("ERR_CANNOT_DETERMINE_VOLUME_TYPE");
                   5622:                return 0;
                   5623:        }
1.1.1.7   root     5624: 
                   5625:        if (IsMountedVolume (lpszVolume))
                   5626:        {
                   5627:                Warning ("DISMOUNT_FIRST");
                   5628:                return 0;
                   5629:        }
                   5630: 
1.1.1.11  root     5631:        swprintf (szTmp, GetString ("CONFIRM_VOL_HEADER_BAK"), lpszVolume);
1.1.1.7   root     5632: 
                   5633:        if (bRequireConfirmation 
                   5634:                && (MessageBoxW (hwndDlg, szTmp, lpszTitle, YES_NO|MB_ICONQUESTION|MB_DEFBUTTON1) == IDNO))
                   5635:                return 0;
                   5636: 
                   5637: 
                   5638:        /* Select backup file */
1.1.1.10  root     5639:        if (!BrowseFiles (hwndDlg, "OPEN_TITLE", szFileName, bHistory, TRUE))
1.1.1.7   root     5640:                return 0;
                   5641: 
                   5642:        /* Conceive the backup file */
                   5643:        if ((fBackup = _open(szFileName, _O_CREAT|_O_TRUNC|_O_WRONLY|_O_BINARY, _S_IREAD|_S_IWRITE)) == -1)
1.1.1.11  root     5644:        {
                   5645:                nStatus = ERROR_CANNOT_MAKE;
                   5646:                goto error;
                   5647:        }
1.1.1.7   root     5648: 
                   5649:        /* Read the volume headers and write them to the backup file */
                   5650: 
                   5651:        CreateFullVolumePath (szDiskFile, lpszVolume, &bDevice);
                   5652: 
                   5653:        if (bDevice == FALSE)
                   5654:                strcpy (szCFDevice, szDiskFile);
                   5655:        else
                   5656:        {
                   5657:                nDosLinkCreated = FakeDosNameForDevice (szDiskFile, szDosDevice, szCFDevice, FALSE);
                   5658:                if (nDosLinkCreated != 0)
1.1.1.11  root     5659:                        goto error;
1.1.1.7   root     5660:        }
                   5661: 
                   5662:        dev = CreateFile (szCFDevice, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
                   5663: 
                   5664:        if (bDevice)
                   5665:        {
                   5666:                /* This is necessary to determine the hidden volume header offset */
                   5667: 
                   5668:                if (dev == INVALID_HANDLE_VALUE)
1.1.1.11  root     5669:                        goto error;
1.1.1.7   root     5670:                else
                   5671:                {
                   5672:                        PARTITION_INFORMATION diskInfo;
                   5673:                        DWORD dwResult;
                   5674:                        BOOL bResult;
1.1.1.11  root     5675:                        
                   5676:                        bResult = GetPartitionInfo (lpszVolume, &diskInfo);
1.1.1.7   root     5677: 
                   5678:                        if (bResult)
                   5679:                        {
                   5680:                                volSize = diskInfo.PartitionLength.QuadPart;
                   5681:                        }
                   5682:                        else
                   5683:                        {
                   5684:                                DISK_GEOMETRY driveInfo;
                   5685: 
                   5686:                                bResult = DeviceIoControl (dev, IOCTL_DISK_GET_DRIVE_GEOMETRY, NULL, 0,
                   5687:                                        &driveInfo, sizeof (driveInfo), &dwResult, NULL);
                   5688: 
                   5689:                                if (!bResult)
                   5690:                                        goto error;
                   5691: 
                   5692:                                volSize = driveInfo.Cylinders.QuadPart * driveInfo.BytesPerSector *
                   5693:                                        driveInfo.SectorsPerTrack * driveInfo.TracksPerCylinder;
                   5694:                        }
                   5695: 
                   5696:                        if (volSize == 0)
                   5697:                        {
                   5698:                                nStatus = ERR_VOL_SIZE_WRONG;
                   5699:                                goto error;
                   5700:                        }
                   5701:                }
                   5702:        }
                   5703: 
1.1.1.11  root     5704:        if (dev == INVALID_HANDLE_VALUE)
                   5705:                goto error;
1.1.1.7   root     5706: 
                   5707:        for (volumeType = VOLUME_TYPE_NORMAL; volumeType < NBR_VOLUME_TYPES; volumeType++)
                   5708:        {
                   5709:                /* Read in volume header */
                   5710: 
                   5711:                if (volumeType == VOLUME_TYPE_HIDDEN)
                   5712:                {
                   5713:                        if (!SeekHiddenVolHeader ((HFILE) dev, volSize, bDevice))
                   5714:                        {
                   5715:                                nStatus = ERR_VOL_SEEKING;
                   5716:                                goto error;
                   5717:                        }
                   5718:                }
                   5719: 
1.1.1.9   root     5720:                nStatus = _lread ((HFILE) dev, buffer, sizeof (buffer));
1.1.1.7   root     5721:                if (nStatus != sizeof (buffer))
                   5722:                {
                   5723:                        nStatus = ERR_VOL_SIZE_WRONG;
                   5724:                        goto error;
                   5725:                }
                   5726: 
                   5727:                /* Write the header to the backup file */
                   5728: 
                   5729:                if (_write (fBackup, buffer, sizeof(buffer)) == -1)
                   5730:                        goto error;
                   5731:        }
                   5732: 
                   5733:        /* Backup has been successfully created */
                   5734:        nStatus = 0;
1.1.1.11  root     5735:        Warning("VOL_HEADER_BACKED_UP");
1.1.1.7   root     5736: 
                   5737: error:
                   5738:        dwError = GetLastError ();
                   5739: 
1.1.1.11  root     5740:        if (dev != INVALID_HANDLE_VALUE)
                   5741:                CloseHandle ((HANDLE) dev);
1.1.1.7   root     5742: 
1.1.1.11  root     5743:        if (fBackup != -1)
                   5744:                _close (fBackup);
1.1.1.7   root     5745: 
1.1.1.11  root     5746:        if (nDosLinkCreated == 0)
                   5747:                RemoveFakeDosName (szDiskFile, szDosDevice);
1.1.1.7   root     5748: 
                   5749:        SetLastError (dwError);
1.1.1.11  root     5750:        if (nStatus != 0)
                   5751:                handleError (hwndDlg, nStatus);
1.1.1.7   root     5752: 
                   5753:        return nStatus;
                   5754: }
                   5755: 
1.1.1.11  root     5756: 
1.1.1.7   root     5757: int RestoreVolumeHeader (HWND hwndDlg, char *lpszVolume)
                   5758: {
1.1.1.11  root     5759:        int nDosLinkCreated = -1, nStatus = ERR_OS_ERROR;
1.1.1.7   root     5760:        char szDiskFile[TC_MAX_PATH], szCFDevice[TC_MAX_PATH];
                   5761:        char szFileName[TC_MAX_PATH];
                   5762:        char szDosDevice[TC_MAX_PATH];
                   5763:        char buffer[HEADER_SIZE];
                   5764:        void *dev = INVALID_HANDLE_VALUE;
                   5765:        DWORD dwError;
                   5766:        BOOL bDevice;
                   5767:        unsigned __int64 volSize = 0;
                   5768:        FILETIME ftCreationTime;
                   5769:        FILETIME ftLastWriteTime;
                   5770:        FILETIME ftLastAccessTime;
1.1.1.11  root     5771:        wchar_t szTmp[4096];
1.1.1.7   root     5772:        BOOL bRestoreHiddenVolHeader = FALSE;
                   5773:        BOOL bTimeStampValid = FALSE;
                   5774:        int fBackup = -1;
                   5775: 
1.1.1.13  root     5776:        switch (IsSystemDevicePath (lpszVolume, hwndDlg, TRUE))
                   5777:        {
                   5778:        case 1:
                   5779:        case 2:
                   5780:                if (AskErrNoYes ("RESTORE_HEADER_NOT_FOR_SYS_DEVICE") == IDYES)
                   5781:                        CreateRescueDisk ();
                   5782: 
                   5783:                return 0;
                   5784: 
                   5785:        case -1:
                   5786:                Error ("ERR_CANNOT_DETERMINE_VOLUME_TYPE");
                   5787:                return 0;
                   5788:        }
1.1.1.7   root     5789: 
                   5790:        if (IsMountedVolume (lpszVolume))
                   5791:        {
                   5792:                Warning ("DISMOUNT_FIRST");
                   5793:                return 0;
                   5794:        }
                   5795: 
1.1.1.11  root     5796:        swprintf (szTmp, GetString ("CONFIRM_VOL_HEADER_RESTORE"), lpszVolume);
1.1.1.7   root     5797: 
                   5798:        if (MessageBoxW (hwndDlg, szTmp, lpszTitle, YES_NO|MB_ICONWARNING|MB_DEFBUTTON2) == IDNO)
                   5799:                return 0;
                   5800: 
                   5801: 
                   5802:        /* Select backup file */
1.1.1.10  root     5803:        if (!BrowseFiles (hwndDlg, "OPEN_TITLE", szFileName, bHistory, FALSE))
1.1.1.7   root     5804:                return 0;
                   5805: 
1.1.1.11  root     5806:        /* Ask the user to select the type of volume (normal/hidden) */
                   5807:        {
                   5808:                char *tmpStr[] = {0, "HEADER_RESTORE_TYPE", "RESTORE_NORMAL_VOLUME_HEADER", "RESTORE_HIDDEN_VOLUME_HEADER", "IDCANCEL", 0};
                   5809:                switch (AskMultiChoice (tmpStr))
                   5810:                {
                   5811:                case 1:
                   5812:                        bRestoreHiddenVolHeader = FALSE;
                   5813:                        break;
                   5814:                case 2:
                   5815:                        bRestoreHiddenVolHeader = TRUE;
                   5816:                        break;
                   5817:                default:
                   5818:                        return 0;
                   5819:                }
                   5820:        }
1.1.1.7   root     5821: 
                   5822:        /* Open the backup file */
                   5823:        if ((fBackup = _open(szFileName, _O_BINARY|_O_RDONLY)) == -1)
1.1.1.11  root     5824:        {
                   5825:                nStatus = ERROR_OPEN_FAILED;
                   5826:                goto error;
                   5827:        }
1.1.1.7   root     5828: 
                   5829:        CreateFullVolumePath (szDiskFile, lpszVolume, &bDevice);
                   5830: 
                   5831:        if (bDevice == FALSE)
                   5832:                strcpy (szCFDevice, szDiskFile);
                   5833:        else
                   5834:        {
                   5835:                nDosLinkCreated = FakeDosNameForDevice (szDiskFile, szDosDevice, szCFDevice, FALSE);
                   5836:                if (nDosLinkCreated != 0)
1.1.1.11  root     5837:                        goto error;
1.1.1.7   root     5838:        }
                   5839: 
                   5840:        dev = CreateFile (szCFDevice, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
                   5841: 
                   5842:        if (bDevice)
                   5843:        {
                   5844:                /* This is necessary to determine the hidden volume header offset */
                   5845: 
                   5846:                if (dev == INVALID_HANDLE_VALUE)
1.1.1.11  root     5847:                        goto error;
1.1.1.7   root     5848:                else
                   5849:                {
                   5850:                        PARTITION_INFORMATION diskInfo;
                   5851:                        DWORD dwResult;
                   5852:                        BOOL bResult;
                   5853: 
1.1.1.11  root     5854:                        bResult = GetPartitionInfo (lpszVolume, &diskInfo);
1.1.1.7   root     5855: 
                   5856:                        if (bResult)
                   5857:                        {
                   5858:                                volSize = diskInfo.PartitionLength.QuadPart;
                   5859:                        }
                   5860:                        else
                   5861:                        {
                   5862:                                DISK_GEOMETRY driveInfo;
                   5863: 
                   5864:                                bResult = DeviceIoControl (dev, IOCTL_DISK_GET_DRIVE_GEOMETRY, NULL, 0,
                   5865:                                        &driveInfo, sizeof (driveInfo), &dwResult, NULL);
                   5866: 
                   5867:                                if (!bResult)
                   5868:                                        goto error;
                   5869: 
                   5870:                                volSize = driveInfo.Cylinders.QuadPart * driveInfo.BytesPerSector *
                   5871:                                        driveInfo.SectorsPerTrack * driveInfo.TracksPerCylinder;
                   5872:                        }
                   5873: 
                   5874:                        if (volSize == 0)
                   5875:                        {
                   5876:                                nStatus =  ERR_VOL_SIZE_WRONG;
                   5877:                                goto error;
                   5878:                        }
                   5879:                }
                   5880:        }
                   5881: 
                   5882:        if (dev == INVALID_HANDLE_VALUE) 
1.1.1.11  root     5883:                goto error;
1.1.1.7   root     5884: 
                   5885:        if (!bDevice && bPreserveTimestamp)
                   5886:        {
                   5887:                /* Remember the container modification/creation date and time. */
                   5888: 
                   5889:                if (GetFileTime ((HANDLE) dev, &ftCreationTime, &ftLastAccessTime, &ftLastWriteTime) == 0)
                   5890:                {
                   5891:                        bTimeStampValid = FALSE;
                   5892:                        Warning ("GETFILETIME_FAILED_GENERIC");
                   5893:                }
                   5894:                else
                   5895:                        bTimeStampValid = TRUE;
                   5896:        }
                   5897: 
                   5898:        /* Read the volume header from the backup file */
                   5899: 
                   5900:        if (_lseek(fBackup, bRestoreHiddenVolHeader ? HEADER_SIZE : 0, SEEK_SET) == -1L)
                   5901:        {
                   5902:                nStatus = ERROR_SEEK;
                   5903:                goto error;
                   5904:        }
                   5905: 
                   5906:        if (_read (fBackup, buffer, HEADER_SIZE) == -1)
                   5907:                goto error;
                   5908: 
                   5909: 
                   5910:        /* Restore/write the volume header */
                   5911: 
                   5912:        // Seek
                   5913:        if (bRestoreHiddenVolHeader)
                   5914:        {
                   5915:                if (!SeekHiddenVolHeader ((HFILE) dev, volSize, bDevice))
                   5916:                {
                   5917:                        nStatus = ERR_VOL_SEEKING;
                   5918:                        goto error;
                   5919:                }
                   5920:        }
                   5921:        else
                   5922:        {
                   5923:                nStatus = _llseek ((HFILE) dev, 0, FILE_BEGIN);
                   5924: 
                   5925:                if (nStatus != 0)
                   5926:                {
                   5927:                        nStatus = ERR_VOL_SEEKING;
                   5928:                        goto error;
                   5929:                }
                   5930:        }
                   5931: 
                   5932:        // Write
1.1.1.9   root     5933:        if ((_lwrite ((HFILE) dev, buffer, HEADER_SIZE)) != HEADER_SIZE)
1.1.1.7   root     5934:        {
                   5935:                nStatus = ERR_VOL_WRITING;
                   5936:                goto error;
                   5937:        }
                   5938: 
                   5939:        /* Volume header has been successfully restored */
                   5940: 
                   5941:        nStatus = 0;
                   5942:        Info("VOL_HEADER_RESTORED");
                   5943: 
                   5944: error:
                   5945: 
                   5946:        dwError = GetLastError ();
                   5947: 
                   5948:        if (bTimeStampValid)
                   5949:        {
                   5950:                // Restore the container timestamp (to preserve plausible deniability of possible hidden volume). 
                   5951:                if (SetFileTime (dev, &ftCreationTime, &ftLastAccessTime, &ftLastWriteTime) == 0)
                   5952:                        MessageBoxW (hwndDlg, GetString ("SETFILETIME_FAILED_PW"), L"TrueCrypt", MB_OK | MB_ICONEXCLAMATION);
                   5953:        }
                   5954: 
1.1.1.11  root     5955:        if (dev != INVALID_HANDLE_VALUE)
                   5956:                CloseHandle ((HANDLE) dev);
1.1.1.7   root     5957: 
1.1.1.11  root     5958:        if (fBackup != -1)
                   5959:                _close (fBackup);
1.1.1.7   root     5960: 
1.1.1.11  root     5961:        if (nDosLinkCreated == 0)
                   5962:                RemoveFakeDosName (szDiskFile, szDosDevice);
1.1.1.7   root     5963: 
                   5964:        SetLastError (dwError);
1.1.1.11  root     5965:        if (nStatus != 0)
                   5966:                handleError (hwndDlg, nStatus);
1.1.1.7   root     5967: 
                   5968:        return nStatus;
                   5969: }
1.1.1.13  root     5970: #endif // #ifdef TCMOUNT
1.1.1.6   root     5971: 
1.1.1.11  root     5972: 
1.1.1.6   root     5973: BOOL IsNonInstallMode ()
                   5974: {
1.1.1.11  root     5975:        HKEY hkey;
1.1.1.6   root     5976: 
1.1.1.11  root     5977:        if (RegOpenKeyEx (HKEY_LOCAL_MACHINE, "Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\TrueCrypt", 0, KEY_READ, &hkey) == ERROR_SUCCESS)
1.1.1.7   root     5978:        {
1.1.1.11  root     5979:                RegCloseKey (hkey);
                   5980:                return FALSE;
1.1.1.7   root     5981:        }
                   5982: 
1.1.1.11  root     5983:        return TRUE;
1.1.1.6   root     5984: }
                   5985: 
                   5986: 
1.1.1.7   root     5987: LRESULT SetCheckBox (HWND hwndDlg, int dlgItem, BOOL state)
1.1.1.6   root     5988: {
1.1.1.7   root     5989:        return SendDlgItemMessage (hwndDlg, dlgItem, BM_SETCHECK, state ? BST_CHECKED : BST_UNCHECKED, 0);
                   5990: }
1.1.1.6   root     5991: 
1.1.1.7   root     5992: 
                   5993: BOOL GetCheckBox (HWND hwndDlg, int dlgItem)
                   5994: {
                   5995:        return IsButtonChecked (GetDlgItem (hwndDlg, dlgItem));
                   5996: }
                   5997: 
                   5998: 
1.1.1.13  root     5999: // Adds or removes TrueCrypt.exe to/from the system startup sequence (with appropriate command line arguments)
                   6000: void ManageStartupSeq (void)
                   6001: {
                   6002:        if (!IsNonInstallMode ())
                   6003:        {
                   6004:                char regk [64];
                   6005: 
                   6006:                // Split the string in order to prevent some antivirus packages from falsely reporting  
                   6007:                // TrueCrypt.exe to contain a possible Trojan horse because of this string (heuristic scan).
                   6008:                sprintf (regk, "%s%s", "Software\\Microsoft\\Windows\\Curren", "tVersion\\Run");
                   6009: 
                   6010:                if (bStartOnLogon)
                   6011:                {
                   6012:                        char exe[MAX_PATH * 2] = { '"' };
                   6013: 
                   6014:                        GetModuleFileName (NULL, exe + 1, sizeof (exe) - 1);
                   6015: 
                   6016: #ifdef VOLFORMAT
                   6017:                        {
                   6018:                                char *tmp = NULL;
                   6019: 
                   6020:                                if (tmp = strrchr (exe, '\\'))
                   6021:                                        strcpy (++tmp, "TrueCrypt.exe");
                   6022:                        }
                   6023: #endif
                   6024:                        strcat (exe, "\" /q preferences");
                   6025: 
                   6026:                        if (bMountDevicesOnLogon) strcat (exe, " /a devices");
                   6027:                        if (bMountFavoritesOnLogon) strcat (exe, " /a favorites");
                   6028: 
                   6029:                        WriteRegistryString (regk, "TrueCrypt", exe);
                   6030:                }
                   6031:                else
                   6032:                        DeleteRegistryValue (regk, "TrueCrypt");
                   6033:        }
                   6034: }
                   6035: 
                   6036: 
1.1.1.11  root     6037: // Delete the last used Windows file selector path for TrueCrypt from the registry
1.1.1.7   root     6038: void CleanLastVisitedMRU (void)
                   6039: {
                   6040:        WCHAR exeFilename[MAX_PATH];
                   6041:        WCHAR *strToMatch;
                   6042: 
1.1.1.11  root     6043:        WCHAR strTmp[4096];
                   6044:        char regPath[128];
                   6045:        char key[64];
                   6046:        int id, len;
1.1.1.7   root     6047: 
                   6048:        GetModuleFileNameW (NULL, exeFilename, sizeof (exeFilename));
                   6049:        strToMatch = wcsrchr (exeFilename, '\\') + 1;
                   6050: 
1.1.1.11  root     6051:        sprintf (regPath, "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\ComDlg32\\LastVisited%sMRU", nCurrentOS == WIN_VISTA_OR_LATER ? "Pidl" : "");
                   6052: 
                   6053:        for (id = (nCurrentOS == WIN_VISTA_OR_LATER ? 0 : 'a'); id <= (nCurrentOS == WIN_VISTA_OR_LATER ? 1000 : 'z'); id++)
1.1.1.6   root     6054:        {
1.1.1.7   root     6055:                *strTmp = 0;
1.1.1.11  root     6056:                sprintf (key, (nCurrentOS == WIN_VISTA_OR_LATER ? "%d" : "%c"), id);
                   6057: 
                   6058:                if ((len = ReadRegistryBytes (regPath, key, (char *) strTmp, sizeof (strTmp))) > 0)
1.1.1.6   root     6059:                {
1.1.1.11  root     6060:                        if (_wcsicmp (strTmp, strToMatch) == 0) 
1.1.1.7   root     6061:                        {
1.1.1.11  root     6062:                                char buf[65536], bufout[sizeof (buf)];
                   6063: 
1.1.1.7   root     6064:                                // Overwrite the entry with zeroes while keeping its original size
                   6065:                                memset (strTmp, 0, len);
1.1.1.11  root     6066:                                if (!WriteRegistryBytes (regPath, key, (char *) strTmp, len))
1.1.1.7   root     6067:                                        MessageBoxW (NULL, GetString ("CLEAN_WINMRU_FAILED"), lpszTitle, ICON_HAND);
                   6068: 
1.1.1.11  root     6069:                                DeleteRegistryValue (regPath, key);
                   6070: 
                   6071:                                // Remove ID from MRUList
                   6072:                                if (nCurrentOS == WIN_VISTA_OR_LATER)
                   6073:                                {
                   6074:                                        int *p = (int *)buf;
                   6075:                                        int *pout = (int *)bufout;
                   6076:                                        int l;
                   6077: 
                   6078:                                        l = len = ReadRegistryBytes ("Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\ComDlg32\\LastVisitedPidlMRU", "MRUListEx", buf, sizeof (buf));
                   6079:                                        while (l > 0)
                   6080:                                        {
                   6081:                                                l -= sizeof (int);
                   6082: 
                   6083:                                                if (*p == id)
                   6084:                                                {
                   6085:                                                        p++;
                   6086:                                                        len -= sizeof (int);
                   6087:                                                        continue;
                   6088:                                                }
                   6089:                                                *pout++ = *p++;
                   6090:                                        }
                   6091: 
                   6092:                                        WriteRegistryBytes ("Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\ComDlg32\\LastVisitedPidlMRU", "MRUListEx", bufout, len);
                   6093:                                }
                   6094:                                else
                   6095:                                {
                   6096:                                        char *p = buf;
                   6097:                                        char *pout = bufout;
                   6098: 
                   6099:                                        ReadRegistryString ("Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\ComDlg32\\LastVisitedMRU", "MRUList", "", buf, sizeof (buf));
                   6100:                                        while (*p)
                   6101:                                        {
                   6102:                                                if (*p == id)
                   6103:                                                {
                   6104:                                                        p++;
                   6105:                                                        continue;
                   6106:                                                }
                   6107:                                                *pout++ = *p++;
                   6108:                                        }
                   6109:                                        *pout++ = 0;
                   6110: 
                   6111:                                        WriteRegistryString ("Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\ComDlg32\\LastVisitedMRU", "MRUList", bufout);
                   6112:                                }
                   6113: 
                   6114:                                break;
1.1.1.7   root     6115:                        }
                   6116:                }
                   6117:        }
                   6118: }
1.1.1.6   root     6119: 
                   6120: 
1.1.1.11  root     6121: #ifndef SETUP
                   6122: void ClearHistory (HWND hwndDlgItem)
                   6123: {
                   6124:        ArrowWaitCursor ();
                   6125: 
                   6126:        ClearCombo (hwndDlgItem);
                   6127:        DumpCombo (hwndDlgItem, TRUE);
                   6128: 
                   6129:        CleanLastVisitedMRU ();
                   6130: 
                   6131:        NormalCursor ();
                   6132: }
                   6133: #endif // #ifndef SETUP
                   6134: 
                   6135: 
1.1.1.7   root     6136: LRESULT ListItemAdd (HWND list, int index, char *string)
                   6137: {
                   6138:        LVITEM li;
                   6139:        memset (&li, 0, sizeof(li));
1.1.1.6   root     6140: 
1.1.1.7   root     6141:        li.mask = LVIF_TEXT;
                   6142:        li.pszText = string;
                   6143:        li.iItem = index; 
                   6144:        li.iSubItem = 0;
                   6145:        return ListView_InsertItem (list, &li);
                   6146: }
1.1.1.6   root     6147: 
1.1.1.7   root     6148: 
                   6149: LRESULT ListItemAddW (HWND list, int index, wchar_t *string)
                   6150: {
                   6151:        LVITEMW li;
                   6152:        memset (&li, 0, sizeof(li));
                   6153: 
                   6154:        li.mask = LVIF_TEXT;
                   6155:        li.pszText = string;
                   6156:        li.iItem = index; 
                   6157:        li.iSubItem = 0;
                   6158:        return SendMessageW (list, LVM_INSERTITEMW, 0, (LPARAM)(&li));
                   6159: }
                   6160: 
                   6161: 
                   6162: LRESULT ListSubItemSet (HWND list, int index, int subIndex, char *string)
                   6163: {
                   6164:        LVITEM li;
                   6165:        memset (&li, 0, sizeof(li));
                   6166: 
                   6167:        li.mask = LVIF_TEXT;
                   6168:        li.pszText = string;
                   6169:        li.iItem = index; 
                   6170:        li.iSubItem = subIndex;
                   6171:        return ListView_SetItem (list, &li);
                   6172: }
                   6173: 
                   6174: 
                   6175: LRESULT ListSubItemSetW (HWND list, int index, int subIndex, wchar_t *string)
                   6176: {
                   6177:        LVITEMW li;
                   6178:        memset (&li, 0, sizeof(li));
                   6179: 
                   6180:        li.mask = LVIF_TEXT;
                   6181:        li.pszText = string;
                   6182:        li.iItem = index; 
                   6183:        li.iSubItem = subIndex;
                   6184:        return SendMessageW (list, LVM_SETITEMW, 0, (LPARAM)(&li));
                   6185: }
                   6186: 
                   6187: 
                   6188: BOOL GetMountList (MOUNT_LIST_STRUCT *list)
                   6189: {
                   6190:        DWORD dwResult;
                   6191: 
                   6192:        memset (list, 0, sizeof (*list));
1.1.1.13  root     6193:        return DeviceIoControl (hDriver, TC_IOCTL_GET_MOUNTED_VOLUMES, list,
1.1.1.7   root     6194:                sizeof (*list), list, sizeof (*list), &dwResult,
                   6195:                NULL);
                   6196: }
                   6197: 
                   6198: 
                   6199: int GetDriverRefCount ()
                   6200: {
                   6201:        DWORD dwResult;
                   6202:        BOOL bResult;
                   6203:        int refCount;
                   6204: 
1.1.1.13  root     6205:        bResult = DeviceIoControl (hDriver, TC_IOCTL_GET_DEVICE_REFCOUNT, &refCount, sizeof (refCount), &refCount,
1.1.1.7   root     6206:                sizeof (refCount), &dwResult, NULL);
                   6207: 
                   6208:        if (bResult)
                   6209:                return refCount;
                   6210:        else
                   6211:                return -1;
                   6212: }
                   6213: 
1.1.1.13  root     6214: // Loads a 32-bit integer from the file at the specified file offset. The saved value is assumed to have been
                   6215: // processed by mputLong(). The result is stored in *result. Returns TRUE if successful (otherwise FALSE).
                   6216: BOOL LoadInt32 (char *filePath, unsigned __int32 *result, __int64 fileOffset)
                   6217: {
                   6218:        int bufSize = sizeof(__int32);
                   6219:        unsigned char *buffer = malloc (bufSize);
                   6220:        unsigned char *bufferPtr = buffer;
                   6221:        HANDLE src = NULL;
                   6222:        DWORD bytesRead;
                   6223:        LARGE_INTEGER seekOffset, seekOffsetNew;
                   6224:        BOOL retVal = FALSE;
                   6225: 
                   6226:        if (buffer == NULL)
                   6227:                return -1;
                   6228: 
                   6229:        src = CreateFile (filePath, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
                   6230: 
                   6231:        if (src == INVALID_HANDLE_VALUE)
                   6232:        {
                   6233:                free (buffer);
                   6234:                return FALSE;
                   6235:        }
                   6236: 
                   6237:        seekOffset.QuadPart = fileOffset;
                   6238: 
                   6239:        if (SetFilePointerEx (src, seekOffset, &seekOffsetNew, FILE_BEGIN) == 0)
                   6240:                goto fsif_end;
                   6241: 
                   6242:        if (ReadFile (src, buffer, bufSize, &bytesRead, NULL) == 0 
                   6243:                || bytesRead != bufSize)
                   6244:                goto fsif_end;
                   6245: 
                   6246: 
                   6247:        retVal = TRUE;
                   6248: 
                   6249:        *result = mgetLong(bufferPtr);
                   6250: 
                   6251: fsif_end:
                   6252:        CloseHandle (src);
                   6253:        free (buffer);
                   6254: 
                   6255:        return retVal;
                   6256: }
                   6257: 
                   6258: // Loads a 16-bit integer from the file at the specified file offset. The saved value is assumed to have been
                   6259: // processed by mputWord(). The result is stored in *result. Returns TRUE if successful (otherwise FALSE).
                   6260: BOOL LoadInt16 (char *filePath, int *result, __int64 fileOffset)
                   6261: {
                   6262:        int bufSize = sizeof(__int16);
                   6263:        unsigned char *buffer = malloc (bufSize);
                   6264:        unsigned char *bufferPtr = buffer;
                   6265:        HANDLE src = NULL;
                   6266:        DWORD bytesRead;
                   6267:        LARGE_INTEGER seekOffset, seekOffsetNew;
                   6268:        BOOL retVal = FALSE;
                   6269: 
                   6270:        if (buffer == NULL)
                   6271:                return -1;
                   6272: 
                   6273:        src = CreateFile (filePath, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
                   6274: 
                   6275:        if (src == INVALID_HANDLE_VALUE)
                   6276:        {
                   6277:                free (buffer);
                   6278:                return FALSE;
                   6279:        }
                   6280: 
                   6281:        seekOffset.QuadPart = fileOffset;
                   6282: 
                   6283:        if (SetFilePointerEx (src, seekOffset, &seekOffsetNew, FILE_BEGIN) == 0)
                   6284:                goto fsif_end;
                   6285: 
                   6286:        if (ReadFile (src, buffer, bufSize, &bytesRead, NULL) == 0 
                   6287:                || bytesRead != bufSize)
                   6288:                goto fsif_end;
                   6289: 
1.1.1.7   root     6290: 
1.1.1.13  root     6291:        retVal = TRUE;
                   6292: 
                   6293:        *result = mgetWord(bufferPtr);
                   6294: 
                   6295: fsif_end:
                   6296:        CloseHandle (src);
                   6297:        free (buffer);
                   6298: 
                   6299:        return retVal;
                   6300: }
                   6301: 
                   6302: // Returns NULL if there's any error
1.1.1.7   root     6303: char *LoadFile (char *fileName, DWORD *size)
                   6304: {
                   6305:        char *buf;
1.1.1.13  root     6306:        HANDLE h = CreateFile (fileName, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
1.1.1.7   root     6307:        if (h == INVALID_HANDLE_VALUE)
                   6308:                return NULL;
                   6309: 
                   6310:        *size = GetFileSize (h, NULL);
                   6311:        buf = malloc (*size + 1);
1.1.1.13  root     6312: 
                   6313:        if (buf == NULL)
                   6314:        {
                   6315:                CloseHandle (h);
                   6316:                return NULL;
                   6317:        }
                   6318: 
1.1.1.7   root     6319:        ZeroMemory (buf, *size + 1);
                   6320: 
1.1.1.13  root     6321:        if (!ReadFile (h, buf, *size, size, NULL))
                   6322:        {
                   6323:                free (buf);
                   6324:                buf = NULL;
                   6325:        }
                   6326: 
                   6327:        CloseHandle (h);
                   6328:        return buf;
                   6329: }
                   6330: 
                   6331: 
                   6332: // Returns NULL if there's any error.
                   6333: char *LoadFileBlock (char *fileName, __int64 fileOffset, int count)
                   6334: {
                   6335:        char *buf;
                   6336:        DWORD bytesRead = 0;
                   6337:        LARGE_INTEGER seekOffset, seekOffsetNew;
                   6338: 
                   6339:        HANDLE h = CreateFile (fileName, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
                   6340:        if (h == INVALID_HANDLE_VALUE)
                   6341:                return NULL;
                   6342: 
                   6343:        seekOffset.QuadPart = fileOffset;
                   6344: 
                   6345:        if (SetFilePointerEx (h, seekOffset, &seekOffsetNew, FILE_BEGIN) == 0)
                   6346:        {
                   6347:                CloseHandle (h);
                   6348:                return NULL;
                   6349:        }
                   6350: 
                   6351:        buf = malloc (count);
                   6352: 
                   6353:        if (buf == NULL)
                   6354:        {
                   6355:                CloseHandle (h);
                   6356:                return NULL;
                   6357:        }
                   6358:  
                   6359:        ZeroMemory (buf, count);
                   6360: 
1.1.1.7   root     6361:        if (buf != NULL)
1.1.1.13  root     6362:                ReadFile (h, buf, count, &bytesRead, NULL);
1.1.1.7   root     6363: 
                   6364:        CloseHandle (h);
1.1.1.13  root     6365: 
                   6366:        if (bytesRead != count)
                   6367:        {
                   6368:                free (buf);
                   6369:                return NULL;
                   6370:        }
                   6371: 
1.1.1.7   root     6372:        return buf;
                   6373: }
                   6374: 
                   6375: 
1.1.1.13  root     6376: // Returns -1 if there is an error, or the size of the file.
                   6377: __int64 GetFileSize64 (char *path)
                   6378: {
                   6379:        HANDLE h = CreateFile (path, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
                   6380:        LARGE_INTEGER size;
                   6381: 
                   6382:        if (h == INVALID_HANDLE_VALUE)
                   6383:                return -1;
                   6384: 
                   6385:        if (GetFileSizeEx (h, &size) == 0)
                   6386:                return -1;
                   6387: 
                   6388:        CloseHandle (h);
                   6389: 
                   6390:        return size.QuadPart;
                   6391: }
                   6392: 
                   6393: 
1.1.1.11  root     6394: char *GetModPath (char *path, int maxSize)
1.1.1.10  root     6395: {
                   6396:        GetModuleFileName (NULL, path, maxSize);
                   6397:        strrchr (path, '\\')[1] = 0;
                   6398:        return path;
                   6399: }
                   6400: 
                   6401: 
1.1.1.7   root     6402: char *GetConfigPath (char *fileName)
                   6403: {
1.1.1.10  root     6404:        static char path[MAX_PATH * 2] = { 0 };
1.1.1.11  root     6405: 
                   6406:        if (IsNonInstallMode ())
1.1.1.12  root     6407:        {
                   6408:                GetModPath (path, sizeof (path));
                   6409:                strcat (path, fileName);
                   6410: 
1.1.1.11  root     6411:                return path;
1.1.1.12  root     6412:        }
1.1.1.7   root     6413: 
1.1.1.12  root     6414:        if (SUCCEEDED(SHGetFolderPath (NULL, CSIDL_APPDATA | CSIDL_FLAG_CREATE, NULL, 0, path)))
1.1.1.7   root     6415:        {
                   6416:                strcat (path, "\\TrueCrypt\\");
                   6417:                CreateDirectory (path, NULL);
                   6418:                strcat (path, fileName);
                   6419:        }
                   6420:        else
1.1.1.12  root     6421:                path[0] = 0;
1.1.1.7   root     6422: 
                   6423:        return path;
                   6424: }
                   6425: 
1.1.1.13  root     6426: // Returns 0 if an error occurs or the drive letter (as an upper-case char) of the system partition (e.g. 'C');
                   6427: char GetSystemDriveLetter (void)
                   6428: {
                   6429:        char systemDir [MAX_PATH];
                   6430: 
                   6431:        if (GetSystemDirectory (systemDir, sizeof (systemDir)))
                   6432:                return (toupper (systemDir [0]));
                   6433:        else
                   6434:                return 0;
                   6435: }
1.1.1.7   root     6436: 
                   6437: int Info (char *stringId)
                   6438: {
                   6439:        if (Silent) return 0;
                   6440:        return MessageBoxW (MainDlg, GetString (stringId), lpszTitle, MB_ICONINFORMATION | MB_SETFOREGROUND | MB_TOPMOST);
                   6441: }
                   6442: 
                   6443: 
                   6444: int Warning (char *stringId)
                   6445: {
                   6446:        if (Silent) return 0;
                   6447:        return MessageBoxW (MainDlg, GetString (stringId), lpszTitle, MB_ICONWARNING | MB_SETFOREGROUND | MB_TOPMOST);
                   6448: }
                   6449: 
                   6450: 
                   6451: int Error (char *stringId)
                   6452: {
                   6453:        if (Silent) return 0;
                   6454:        return MessageBoxW (MainDlg, GetString (stringId), lpszTitle, MB_ICONERROR | MB_SETFOREGROUND | MB_TOPMOST);
                   6455: }
                   6456: 
                   6457: 
                   6458: int AskYesNo (char *stringId)
                   6459: {
                   6460:        if (Silent) return 0;
                   6461:        return MessageBoxW (MainDlg, GetString (stringId), lpszTitle, MB_ICONQUESTION | MB_YESNO | MB_DEFBUTTON1 | MB_SETFOREGROUND | MB_TOPMOST);
                   6462: }
                   6463: 
                   6464: 
                   6465: int AskNoYes (char *stringId)
                   6466: {
                   6467:        if (Silent) return 0;
                   6468:        return MessageBoxW (MainDlg, GetString (stringId), lpszTitle, MB_ICONQUESTION | MB_YESNO | MB_DEFBUTTON2 | MB_SETFOREGROUND | MB_TOPMOST);
                   6469: }
                   6470: 
                   6471: 
1.1.1.13  root     6472: int AskOkCancel (char *stringId)
                   6473: {
                   6474:        if (Silent) return 0;
                   6475:        return MessageBoxW (MainDlg, GetString (stringId), lpszTitle, MB_ICONQUESTION | MB_OKCANCEL | MB_DEFBUTTON1 | MB_SETFOREGROUND | MB_TOPMOST);
                   6476: }
                   6477: 
                   6478: 
1.1.1.7   root     6479: int AskWarnYesNo (char *stringId)
                   6480: {
                   6481:        if (Silent) return 0;
                   6482:        return MessageBoxW (MainDlg, GetString (stringId), lpszTitle, MB_ICONWARNING | MB_YESNO | MB_DEFBUTTON1 | MB_SETFOREGROUND | MB_TOPMOST);
                   6483: }
                   6484: 
                   6485: 
                   6486: int AskWarnNoYes (char *stringId)
                   6487: {
                   6488:        if (Silent) return 0;
                   6489:        return MessageBoxW (MainDlg, GetString (stringId), lpszTitle, MB_ICONWARNING | MB_YESNO | MB_DEFBUTTON2 | MB_SETFOREGROUND | MB_TOPMOST);
                   6490: }
                   6491: 
                   6492: 
1.1.1.12  root     6493: int AskWarnNoYesString (wchar_t *string)
                   6494: {
                   6495:        if (Silent) return 0;
                   6496:        return MessageBoxW (MainDlg, string, lpszTitle, MB_ICONWARNING | MB_YESNO | MB_DEFBUTTON2 | MB_SETFOREGROUND | MB_TOPMOST);
                   6497: }
                   6498: 
                   6499: 
1.1.1.7   root     6500: int AskWarnCancelOk (char *stringId)
                   6501: {
                   6502:        if (Silent) return 0;
                   6503:        return MessageBoxW (MainDlg, GetString (stringId), lpszTitle, MB_ICONWARNING | MB_OKCANCEL | MB_DEFBUTTON2 | MB_SETFOREGROUND | MB_TOPMOST);
                   6504: }
                   6505: 
                   6506: 
1.1.1.13  root     6507: int AskErrYesNo (char *stringId)
                   6508: {
                   6509:        if (Silent) return 0;
                   6510:        return MessageBoxW (MainDlg, GetString (stringId), lpszTitle, MB_ICONERROR | MB_YESNO | MB_DEFBUTTON1 | MB_SETFOREGROUND | MB_TOPMOST);
                   6511: }
                   6512: 
                   6513: 
                   6514: int AskErrNoYes (char *stringId)
                   6515: {
                   6516:        if (Silent) return 0;
                   6517:        return MessageBoxW (MainDlg, GetString (stringId), lpszTitle, MB_ICONERROR | MB_YESNO | MB_DEFBUTTON2 | MB_SETFOREGROUND | MB_TOPMOST);
                   6518: }
                   6519: 
                   6520: 
1.1.1.11  root     6521: // The function accepts two input formats:
                   6522: // Input format 1: {0, "MESSAGE_STRING_ID", "BUTTON_1_STRING_ID", ... "LAST_BUTTON_STRING_ID", 0};
                   6523: // Input format 2: {L"", L"Message text", L"Button caption 1", ... L"Last button caption", 0};
                   6524: // The second format is to be used if any of the strings contains format specification (e.g. %s, %d) or
                   6525: // in any other cases where a string needs to be resolved before calling this function.
                   6526: // If the returned value is 0, the user closed the dialog window without making a choice. 
                   6527: // If the user made a choice, the returned value is the ordinal number of the choice (1..MAX_MULTI_CHOICES)
                   6528: int AskMultiChoice (void *strings[])
                   6529: {
                   6530:        return DialogBoxParamW (hInst, 
                   6531:                MAKEINTRESOURCEW (IDD_MULTI_CHOICE_DLG), MainDlg,
                   6532:                (DLGPROC) MultiChoiceDialogProc, (LPARAM) &strings[0]);
                   6533: }
                   6534: 
                   6535: 
1.1.1.7   root     6536: BOOL ConfigWriteBegin ()
                   6537: {
                   6538:        DWORD size;
1.1.1.13  root     6539:        if (ConfigFileHandle != NULL) 
                   6540:                return FALSE;
1.1.1.7   root     6541: 
                   6542:        if (ConfigBuffer == NULL)
                   6543:                ConfigBuffer = LoadFile (GetConfigPath (FILE_CONFIGURATION), &size);
                   6544: 
                   6545:        ConfigFileHandle = fopen (GetConfigPath (FILE_CONFIGURATION), "w");
                   6546:        if (ConfigFileHandle == NULL)
                   6547:        {
                   6548:                free (ConfigBuffer);
                   6549:                ConfigBuffer = NULL;
                   6550:                return FALSE;
                   6551:        }
                   6552:        XmlWriteHeader (ConfigFileHandle);
                   6553:        fputs ("\n\t<configuration>", ConfigFileHandle);
                   6554: 
                   6555:        return TRUE;
                   6556: }
                   6557: 
                   6558: 
                   6559: BOOL ConfigWriteEnd ()
                   6560: {
                   6561:        char *xml = ConfigBuffer;
                   6562:        char key[128], value[2048];
                   6563: 
                   6564:        if (ConfigFileHandle == NULL) return FALSE;
                   6565: 
                   6566:        // Write unmodified values
                   6567:        while (xml && (xml = XmlFindElement (xml, "config")))
                   6568:        {
1.1.1.11  root     6569:                XmlGetAttributeText (xml, "key", key, sizeof (key));
                   6570:                XmlGetNodeText (xml, value, sizeof (value));
1.1.1.7   root     6571: 
                   6572:                fprintf (ConfigFileHandle, "\n\t\t<config key=\"%s\">%s</config>", key, value);
                   6573:                xml++;
                   6574:        }
                   6575: 
                   6576:        fputs ("\n\t</configuration>", ConfigFileHandle);
                   6577:        XmlWriteFooter (ConfigFileHandle);
                   6578: 
                   6579:        fclose (ConfigFileHandle);
                   6580:        ConfigFileHandle = NULL;
                   6581: 
                   6582:        if (ConfigBuffer != NULL)
                   6583:        {
                   6584:                DWORD size;
                   6585:                free (ConfigBuffer);
                   6586:                ConfigBuffer = LoadFile (GetConfigPath (FILE_CONFIGURATION), &size);
                   6587:        }
                   6588: 
                   6589:        return TRUE;
                   6590: }
                   6591: 
                   6592: 
                   6593: BOOL ConfigWriteString (char *configKey, char *configValue)
                   6594: {
                   6595:        char *c;
                   6596:        if (ConfigFileHandle == NULL)
                   6597:                return FALSE;
                   6598: 
                   6599:        // Mark previous config value as updated
                   6600:        if (ConfigBuffer != NULL)
                   6601:        {
                   6602:                c = XmlFindElementByAttributeValue (ConfigBuffer, "config", "key", configKey);
                   6603:                if (c != NULL)
                   6604:                        c[1] = '!';
                   6605:        }
                   6606: 
                   6607:        return 0 != fprintf (
                   6608:                ConfigFileHandle, "\n\t\t<config key=\"%s\">%s</config>",
                   6609:                configKey, configValue);
                   6610: }
                   6611: 
                   6612: 
                   6613: BOOL ConfigWriteInt (char *configKey, int configValue)
                   6614: {
                   6615:        char val[32];
                   6616:        sprintf (val, "%d", configValue);
                   6617:        return ConfigWriteString (configKey, val);
                   6618: }
                   6619: 
                   6620: 
                   6621: static BOOL ConfigRead (char *configKey, char *configValue, int maxValueSize)
                   6622: {
                   6623:        DWORD size;
                   6624:        char *xml;
                   6625: 
                   6626:        if (ConfigBuffer == NULL)
                   6627:                ConfigBuffer = LoadFile (GetConfigPath (FILE_CONFIGURATION), &size);
                   6628: 
                   6629:        xml = ConfigBuffer;
                   6630:        if (xml != NULL)
                   6631:        {
                   6632:                xml = XmlFindElementByAttributeValue (xml, "config", "key", configKey);
                   6633:                if (xml != NULL)
1.1.1.6   root     6634:                {
1.1.1.11  root     6635:                        XmlGetNodeText (xml, configValue, maxValueSize);
1.1.1.7   root     6636:                        return TRUE;
1.1.1.6   root     6637:                }
1.1.1.7   root     6638:        }
                   6639: 
                   6640:        return FALSE;
                   6641: }
                   6642: 
                   6643: 
                   6644: int ConfigReadInt (char *configKey, int defaultValue)
                   6645: {
                   6646:        char s[32];
                   6647: 
                   6648:        if (ConfigRead (configKey, s, sizeof (s)))
                   6649:                return atoi (s);
                   6650:        else
                   6651:                return defaultValue;
                   6652: }
                   6653: 
                   6654: 
                   6655: char *ConfigReadString (char *configKey, char *defaultValue, char *str, int maxLen)
                   6656: {
                   6657:        if (ConfigRead (configKey, str, maxLen))
                   6658:                return str;
                   6659:        else
                   6660:                return defaultValue;
                   6661: }
                   6662: 
                   6663: 
                   6664: void OpenPageHelp (HWND hwndDlg, int nPage)
                   6665: {
                   6666:        int r = (int)ShellExecute (NULL, "open", szHelpFile, NULL, NULL, SW_SHOWNORMAL);
                   6667:        if (nPage);             /* Remove warning */
1.1.1.6   root     6668: 
1.1.1.7   root     6669:        if (r == ERROR_FILE_NOT_FOUND)
                   6670:        {
                   6671:                // Try the secondary help file
                   6672:                r = (int)ShellExecute (NULL, "open", szHelpFile2, NULL, NULL, SW_SHOWNORMAL);
                   6673: 
                   6674:                if (r == ERROR_FILE_NOT_FOUND)
1.1.1.10  root     6675:                {
                   6676:                        OpenOnlineHelp ();
                   6677:                        return;
                   6678:                }
1.1.1.7   root     6679:        }
1.1.1.10  root     6680: 
1.1.1.7   root     6681:        if (r == SE_ERR_NOASSOC)
1.1.1.10  root     6682:        {
                   6683:                if (AskYesNo ("HELP_READER_ERROR") == IDYES)
                   6684:                        OpenOnlineHelp ();
                   6685:        }
                   6686: }
                   6687: 
                   6688: 
                   6689: void OpenOnlineHelp ()
                   6690: {
1.1.1.11  root     6691:        Applink ("help", TRUE, "");
1.1.1.7   root     6692: }
                   6693: 
                   6694: 
                   6695: #ifndef SETUP
                   6696: 
                   6697: void RestoreDefaultKeyFilesParam (void)
                   6698: {
                   6699:        KeyFileRemoveAll (&FirstKeyFile);
                   6700:        if (defaultKeyFilesParam.FirstKeyFile != NULL)
                   6701:        {
                   6702:                FirstKeyFile = KeyFileCloneAll (defaultKeyFilesParam.FirstKeyFile);
                   6703:                KeyFilesEnable = defaultKeyFilesParam.EnableKeyFiles;
                   6704:        }
                   6705:        else
                   6706:                KeyFilesEnable = FALSE;
                   6707: }
                   6708: 
                   6709: 
                   6710: BOOL LoadDefaultKeyFilesParam (void)
                   6711: {
                   6712:        BOOL status = TRUE;
                   6713:        DWORD size;
                   6714:        char *defaultKeyfilesFile = LoadFile (GetConfigPath (FILE_DEFAULT_KEYFILES), &size);
                   6715:        char *xml = defaultKeyfilesFile;
                   6716:        KeyFile *kf;
                   6717: 
                   6718:        if (xml == NULL) 
                   6719:                return FALSE;
                   6720: 
                   6721:        KeyFileRemoveAll (&defaultKeyFilesParam.FirstKeyFile);
                   6722: 
                   6723:        while (xml = XmlFindElement (xml, "keyfile"))
                   6724:        {
                   6725:                kf = malloc (sizeof (KeyFile));
                   6726: 
1.1.1.11  root     6727:                if (XmlGetNodeText (xml, kf->FileName, sizeof (kf->FileName)) != NULL)
1.1.1.7   root     6728:                        defaultKeyFilesParam.FirstKeyFile = KeyFileAdd (defaultKeyFilesParam.FirstKeyFile, kf);
                   6729:                else
                   6730:                        free (kf);
                   6731: 
                   6732:                xml++;
                   6733:        }
                   6734: 
                   6735:        free (defaultKeyfilesFile);
                   6736:        KeyFilesEnable = defaultKeyFilesParam.EnableKeyFiles;
                   6737: 
                   6738:        return status;
                   6739: }
                   6740: 
                   6741: #endif /* #ifndef SETUP */
                   6742: 
                   6743: 
                   6744: void Debug (char *format, ...)
                   6745: {
                   6746:        char buf[1024];
                   6747:        va_list val;
                   6748: 
                   6749:        va_start(val, format);
                   6750:        _vsnprintf (buf, sizeof (buf), format, val);
                   6751:        va_end(val);
                   6752: 
                   6753:        OutputDebugString (buf);
                   6754: }
                   6755: 
                   6756: 
                   6757: void DebugMsgBox (char *format, ...)
                   6758: {
                   6759:        char buf[1024];
                   6760:        va_list val;
                   6761: 
                   6762:        va_start(val, format);
                   6763:        _vsnprintf (buf, sizeof (buf), format, val);
                   6764:        va_end(val);
                   6765: 
                   6766:        MessageBox (MainDlg, buf, "TrueCrypt debug", 0);
                   6767: }
                   6768: 
                   6769: 
                   6770: BOOL Is64BitOs ()
                   6771: {
                   6772:     static BOOL isWow64 = FALSE;
                   6773:        static BOOL valid = FALSE;
                   6774:        typedef BOOL (__stdcall *LPFN_ISWOW64PROCESS ) (HANDLE hProcess,PBOOL Wow64Process);
                   6775:        LPFN_ISWOW64PROCESS fnIsWow64Process;
                   6776: 
                   6777:        if (valid)
                   6778:                return isWow64;
                   6779: 
                   6780:        fnIsWow64Process = (LPFN_ISWOW64PROCESS) GetProcAddress (GetModuleHandle("kernel32"), "IsWow64Process");
                   6781: 
                   6782:     if (fnIsWow64Process != NULL)
                   6783:         if (!fnIsWow64Process (GetCurrentProcess(), &isWow64))
                   6784:                        isWow64 = FALSE;
                   6785: 
                   6786:        valid = TRUE;
                   6787:     return isWow64;
                   6788: }
                   6789: 
                   6790: 
1.1.1.11  root     6791: void Applink (char *dest, BOOL bSendOS, char *extraOutput)
                   6792: {
                   6793:        char url [MAX_URL_LENGTH];
                   6794:        char osname [200];
                   6795: 
                   6796:        if (bSendOS)
                   6797:        {
                   6798:                OSVERSIONINFOEXA os;
                   6799: 
                   6800:                os.dwOSVersionInfoSize = sizeof (OSVERSIONINFOEXA);
                   6801: 
                   6802:                GetVersionExA ((LPOSVERSIONINFOA) &os);
                   6803: 
                   6804:                strcpy (osname, "&os=");
                   6805: 
                   6806:                switch (nCurrentOS)
                   6807:                {
                   6808:                case WIN_2000:
                   6809:                        strcat (osname, "win2000");
                   6810:                        break;
                   6811: 
                   6812:                case WIN_XP:
                   6813:                case WIN_XP64:
                   6814:                        strcat (osname, "winxp");
                   6815:                        break;
                   6816: 
                   6817:                case WIN_SERVER_2003:
                   6818:                        strcat (osname, "win2003");
                   6819:                        break;
                   6820: 
                   6821:                case WIN_VISTA_OR_LATER:
1.1.1.13  root     6822:                        if (CurrentOSMajor == 6 && CurrentOSMinor == 0)
1.1.1.11  root     6823:                        {
1.1.1.13  root     6824:                                if (os.wProductType != VER_NT_SERVER && os.wProductType != VER_NT_DOMAIN_CONTROLLER)
1.1.1.11  root     6825:                                {
1.1.1.13  root     6826:                                        strcat (osname, "winvista");
                   6827: 
                   6828:                                        if (os.wSuiteMask & VER_SUITE_PERSONAL)
                   6829:                                                strcat (osname, "-home");
                   6830:                                        else
1.1.1.11  root     6831:                                        {
1.1.1.13  root     6832:                                                HKEY hkey = 0;
                   6833:                                                char str[300] = {0};
                   6834:                                                DWORD size = sizeof (str);
                   6835: 
                   6836:                                                ZeroMemory (str, sizeof (str));
                   6837:                                                if (RegOpenKeyEx (HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion",
                   6838:                                                        0, KEY_QUERY_VALUE, &hkey) == ERROR_SUCCESS
                   6839:                                                        && (RegQueryValueEx (hkey, "ProductName", 0, 0, (LPBYTE) &str, &size) == ERROR_SUCCESS))
                   6840:                                                {
                   6841:                                                        if (strstr (str, "Enterprise") != 0)
                   6842:                                                                strcat (osname, "-enterprise");
                   6843:                                                        else if (strstr (str, "Business") != 0)
                   6844:                                                                strcat (osname, "-business");
                   6845:                                                        else if (strstr (str, "Ultimate") != 0)
                   6846:                                                                strcat (osname, "-ultimate");
                   6847:                                                }
                   6848:                                                RegCloseKey (hkey);
1.1.1.11  root     6849:                                        }
1.1.1.13  root     6850:                                }
                   6851:                                else
                   6852:                                {
                   6853:                                        strcat (osname, "win2008");
1.1.1.11  root     6854:                                }
                   6855:                        }
1.1.1.12  root     6856:                        else
                   6857:                        {
                   6858:                                sprintf (osname + strlen (osname), "win%d.%d", CurrentOSMajor, CurrentOSMinor);
                   6859:                        }
                   6860: 
                   6861:                        if (os.wProductType == VER_NT_SERVER || os.wProductType == VER_NT_DOMAIN_CONTROLLER)
                   6862:                                strcat (osname, "-server");
                   6863: 
1.1.1.11  root     6864:                        break;
                   6865: 
                   6866:                default:
1.1.1.12  root     6867:                        sprintf (osname + strlen (osname), "win%d.%d", CurrentOSMajor, CurrentOSMinor);
                   6868:                        break;
1.1.1.11  root     6869:                }
                   6870: 
                   6871:                if (Is64BitOs())
                   6872:                        strcat (osname, "-x64");
                   6873: 
                   6874:                if (CurrentOSServicePack > 0)
1.1.1.12  root     6875:                        sprintf (osname + strlen (osname), "-sp%d", CurrentOSServicePack);
1.1.1.11  root     6876:        }
                   6877:        else
                   6878:                osname[0] = 0;
                   6879: 
                   6880:        ArrowWaitCursor ();
                   6881: 
                   6882:        sprintf (url, TC_APPLINK "%s%s&dest=%s", osname, extraOutput, dest);
                   6883:        ShellExecute (NULL, "open", url, NULL, NULL, SW_SHOWNORMAL);
                   6884: 
                   6885:        Sleep (200);
                   6886:        NormalCursor ();
                   6887: }
                   6888: 
                   6889: 
1.1.1.7   root     6890: char *RelativePath2Absolute (char *szFileName)
                   6891: {
                   6892:        if (szFileName[0] != '\\'
                   6893:                && strchr (szFileName, ':') == 0
                   6894:                && strstr (szFileName, "Volume{") != szFileName)
                   6895:        {
                   6896:                char path[MAX_PATH*2];
                   6897:                GetCurrentDirectory (MAX_PATH, path);
                   6898: 
                   6899:                if (path[strlen (path) - 1] != '\\')
                   6900:                        strcat (path, "\\");
                   6901: 
                   6902:                strcat (path, szFileName);
                   6903:                strncpy (szFileName, path, MAX_PATH-1);
                   6904:        }
                   6905: 
                   6906:        return szFileName;
                   6907: }
                   6908: 
                   6909: 
                   6910: void CheckSystemAutoMount ()
                   6911: {
                   6912:        HKEY hkey = 0;
                   6913:        DWORD value = 0, size = sizeof (DWORD);
                   6914: 
                   6915:        if (RegOpenKeyEx (HKEY_LOCAL_MACHINE, "SYSTEM\\ControlSet001\\Services\\MountMgr",
                   6916:                0, KEY_READ, &hkey) != ERROR_SUCCESS)
                   6917:                return;
                   6918: 
                   6919:        if (RegQueryValueEx (hkey, "NoAutoMount", 0, 0, (LPBYTE) &value, &size) == ERROR_SUCCESS
                   6920:                && value != 0)
                   6921:                Warning ("SYS_AUTOMOUNT_DISABLED");
1.1.1.11  root     6922:        else if (nCurrentOS == WIN_VISTA_OR_LATER)
                   6923:                Warning ("SYS_ASSIGN_DRIVE_LETTER");
                   6924: 
1.1.1.7   root     6925:        RegCloseKey (hkey);
                   6926: }
                   6927: 
                   6928: 
1.1.1.8   root     6929: BOOL CALLBACK CloseTCWindowsEnum (HWND hwnd, LPARAM lParam)
1.1.1.7   root     6930: {
                   6931:        if (GetWindowLongPtr (hwnd, GWLP_USERDATA) == (LONG_PTR) 'TRUE')
                   6932:        {
1.1.1.12  root     6933:                char name[1024] = { 0 };
1.1.1.7   root     6934:                GetWindowText (hwnd, name, sizeof (name) - 1);
1.1.1.12  root     6935:                if (hwnd != MainDlg && strstr (name, "TrueCrypt"))
1.1.1.6   root     6936:                {
1.1.1.13  root     6937:                        PostMessage (hwnd, TC_APPMSG_CLOSE_BKG_TASK, 0, 0);
1.1.1.11  root     6938: 
                   6939:                        if (DriverVersion < 0x0430)
                   6940:                                PostMessage (hwnd, WM_ENDSESSION, 0, 0);
                   6941: 
1.1.1.7   root     6942:                        PostMessage (hwnd, WM_CLOSE, 0, 0);
1.1.1.6   root     6943: 
1.1.1.7   root     6944:                        if (lParam != 0)
                   6945:                                *((BOOL *)lParam) = TRUE;
1.1.1.6   root     6946:                }
                   6947:        }
1.1.1.7   root     6948:        return TRUE;
1.1.1.6   root     6949: }
1.1.1.7   root     6950: 
1.1.1.8   root     6951: BOOL CALLBACK FindTCWindowEnum (HWND hwnd, LPARAM lParam)
                   6952: {
                   6953:        if (*(HWND *)lParam == hwnd)
                   6954:                return TRUE;
                   6955: 
                   6956:        if (GetWindowLongPtr (hwnd, GWLP_USERDATA) == (LONG_PTR) 'TRUE')
                   6957:        {
                   6958:                char name[32] = { 0 };
                   6959:                GetWindowText (hwnd, name, sizeof (name) - 1);
1.1.1.9   root     6960:                if (hwnd != MainDlg && strcmp (name, "TrueCrypt") == 0)
1.1.1.8   root     6961:                {
                   6962:                        if (lParam != 0)
                   6963:                                *((HWND *)lParam) = hwnd;
                   6964:                }
                   6965:        }
                   6966:        return TRUE;
                   6967: }
                   6968: 
                   6969: 
1.1.1.7   root     6970: BYTE *MapResource (char *resourceType, int resourceId, PDWORD size)
                   6971: {
                   6972:        HGLOBAL hResL; 
                   6973:     HRSRC hRes;
                   6974: 
                   6975:        hRes = FindResource (NULL, MAKEINTRESOURCE(resourceId), resourceType);
                   6976:        hResL = LoadResource (NULL, hRes);
                   6977: 
                   6978:        if (size != NULL)
                   6979:                *size = SizeofResource (NULL, hRes);
                   6980:   
                   6981:        return (BYTE *) LockResource (hResL);
1.1.1.13  root     6982: }
                   6983: 
                   6984: 
                   6985: void InconsistencyResolved (char *techInfo)
                   6986: {
                   6987:        wchar_t finalMsg[8024];
                   6988: 
                   6989:        wsprintfW (finalMsg, GetString ("INCONSISTENCY_RESOLVED"), techInfo);
                   6990:        MessageBoxW (MainDlg, finalMsg, lpszTitle, MB_ICONWARNING | MB_SETFOREGROUND | MB_TOPMOST);
                   6991: }

unix.superglobalmegacorp.com

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