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