-
Notifications
You must be signed in to change notification settings - Fork 466
/
zadig_stdlg.c
1286 lines (1186 loc) · 37.9 KB
/
zadig_stdlg.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Zadig: Automated Driver Installer for USB devices (GUI version)
* Standard Dialog Routines (Browse for folder, About, etc)
* Copyright (c) 2010-2017 Pete Batard <pete@akeo.ie>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/* Memory leaks detection - define _CRTDBG_MAP_ALLOC as preprocessor macro */
#ifdef _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>
#endif
#include <windows.h>
#include <windowsx.h>
#include <stdlib.h>
#include <stdio.h>
#include <io.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include <shlobj.h>
#include <shellapi.h>
#include <process.h>
#include <commdlg.h>
#include <richedit.h>
#include <sddl.h>
#include "libwdi.h"
#include "zadig_resource.h"
#include "zadig.h"
#include "zadig_license.h"
#include "zadig_registry.h"
#include "msapi_utf8.h"
#include "stdfn.h"
/*
* Globals
*/
static HICON hMessageIcon = (HICON)INVALID_HANDLE_VALUE;
static char* szMessageText = NULL;
static char* szMessageTitle = NULL;
extern HFONT bold_font;
extern float fScale;
static HWND hBrowseEdit;
static WNDPROC pOrgBrowseWndproc;
static const SETTEXTEX friggin_microsoft_unicode_amateurs = {ST_DEFAULT, CP_UTF8};
static BOOL notification_is_question;
static const notification_info* notification_more_info;
static BOOL reg_commcheck = FALSE;
static WNDPROC original_wndproc = NULL;
/*
* https://blogs.msdn.microsoft.com/oldnewthing/20040802-00/?p=38283/
*/
void SetDialogFocus(HWND hDlg, HWND hCtrl)
{
SendMessage(hDlg, WM_NEXTDLGCTL, (WPARAM)hCtrl, TRUE);
}
/*
* Converts a name + ext UTF-8 pair to a valid MS filename.
* Returned string is allocated and needs to be freed manually
*/
char* to_valid_filename(char* name, char* ext)
{
size_t i, j, k;
BOOL found;
char* ret;
wchar_t unauthorized[] = L"\x0001\x0002\x0003\x0004\x0005\x0006\x0007\x0008\x000a"
L"\x000b\x000c\x000d\x000e\x000f\x0010\x0011\x0012\x0013\x0014\x0015\x0016\x0017"
L"\x0018\x0019\x001a\x001b\x001c\x001d\x001e\x001f\x007f\"*/:<>?\\|,";
wchar_t to_underscore[] = L" \t";
wchar_t *wname, *wext, *wret;
if ((name == NULL) || (ext == NULL)) {
return NULL;
}
if (strlen(name) > WDI_MAX_STRLEN) return NULL;
// Convert to UTF-16
wname = utf8_to_wchar(name);
wext = utf8_to_wchar(ext);
if ((wname == NULL) || (wext == NULL)) {
safe_free(wname); safe_free(wext); return NULL;
}
// The returned UTF-8 string will never be larger than the sum of its parts
wret = (wchar_t*)calloc(2*(wcslen(wname) + wcslen(wext) + 2), 1);
if (wret == NULL) {
safe_free(wname); safe_free(wext); return NULL;
}
wcscpy(wret, wname);
safe_free(wname);
wcscat(wret, wext);
safe_free(wext);
for (i=0, k=0; i<wcslen(wret); i++) {
found = FALSE;
for (j=0; j<wcslen(unauthorized); j++) {
if (wret[i] == unauthorized[j]) {
found = TRUE; break;
}
}
if (found) continue;
found = FALSE;
for (j=0; j<wcslen(to_underscore); j++) {
if (wret[i] == to_underscore[j]) {
wret[k++] = '_';
found = TRUE; break;
}
}
if (found) continue;
wret[k++] = wret[i];
}
wret[k] = 0;
ret = wchar_to_utf8(wret);
safe_free(wret);
return ret;
}
/*
* Convert a windows error to human readable string
* uses retval as errorcode, or, if 0, use GetLastError()
*/
const char *WindowsErrorString(void)
{
static char err_string[256] = { 0 };
DWORD size;
DWORD errcode, format_error;
errcode = GetLastError();
static_sprintf(err_string, "[0x%08lX] ", errcode);
size = FormatMessageU(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, HRESULT_CODE(errcode),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), &err_string[strlen(err_string)],
sizeof(err_string) - (DWORD)strlen(err_string), NULL);
if (size == 0) {
format_error = GetLastError();
if ((format_error) && (format_error != 0x13D)) // 0x13D, decode error, is returned for unknown codes
static_sprintf(err_string, "Windows error code 0x%08lX (FormatMessage error code 0x%08lX)",
errcode, format_error);
else
static_sprintf(err_string, "Unknown error 0x%08lX", errcode);
}
SetLastError(errcode); // Make sure we don't change the errorcode on exit
return err_string;
}
/*
* Retrieve the SID of the current user. The returned PSID must be freed by the caller using LocalFree()
*/
static PSID GetSid(void) {
TOKEN_USER* tu = NULL;
DWORD len;
HANDLE token;
PSID ret = NULL;
char* psid_string = NULL;
if (!OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &token)) {
dprintf("OpenProcessToken failed: %s", WindowsErrorString());
return NULL;
}
if (!GetTokenInformation(token, TokenUser, tu, 0, &len)) {
if (GetLastError() != ERROR_INSUFFICIENT_BUFFER) {
dprintf("GetTokenInformation (pre) failed: %s", WindowsErrorString());
return NULL;
}
tu = (TOKEN_USER*)calloc(1, len);
}
if (tu == NULL) {
return NULL;
}
if (GetTokenInformation(token, TokenUser, tu, len, &len)) {
/*
* now of course, the interesting thing is that if you return tu->User.Sid
* but free tu, the PSID pointer becomes invalid after a while.
* The workaround? Convert to string then back to PSID
*/
if (!ConvertSidToStringSidA(tu->User.Sid, &psid_string)) {
dprintf("unable to convert SID to string: %s", WindowsErrorString());
ret = NULL;
} else {
if (!ConvertStringSidToSidA(psid_string, &ret)) {
dprintf("unable to convert string back to SID: %s", WindowsErrorString());
ret = NULL;
}
// MUST use LocalFree()
LocalFree(psid_string);
}
} else {
ret = NULL;
dprintf("GetTokenInformation (real) failed: %s", WindowsErrorString());
}
free(tu);
return ret;
}
/*
* We need a sub-callback to read the content of the edit box on exit and update
* our path, else if what the user typed does match the selection, it is discarded.
* Talk about a convoluted way of producing an intuitive folder selection dialog
*/
INT CALLBACK BrowseDlgCallback(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
switch(message) {
case WM_DESTROY:
GetWindowTextU(hBrowseEdit, szFolderPath, sizeof(szFolderPath));
break;
}
return (INT)CallWindowProc(pOrgBrowseWndproc, hDlg, message, wParam, lParam);
}
/*
* Main BrowseInfo callback to set the initial directory and populate the edit control
*/
INT CALLBACK BrowseInfoCallback(HWND hDlg, UINT message, LPARAM lParam, LPARAM pData)
{
char dir[MAX_PATH];
wchar_t* wpath;
LPITEMIDLIST pidl;
switch(message) {
case BFFM_INITIALIZED:
pOrgBrowseWndproc = (WNDPROC)SetWindowLongPtr(hDlg, GWLP_WNDPROC, (LONG_PTR)BrowseDlgCallback);
// Windows hides the full path in the edit box by default, which is bull.
// Get a handle to the edit control to fix that
hBrowseEdit = FindWindowExA(hDlg, NULL, "Edit", NULL);
SetWindowTextU(hBrowseEdit, szFolderPath);
SetDialogFocus(hDlg, hBrowseEdit);
// On Windows 7, MinGW only properly selects the specified folder when using a pidl
wpath = utf8_to_wchar(szFolderPath);
pidl = SHSimpleIDListFromPath(wpath);
safe_free(wpath);
// NB: see http://connect.microsoft.com/VisualStudio/feedback/details/518103/bffm-setselection-does-not-work-with-shbrowseforfolder-on-windows-7
// for details as to why we send BFFM_SETSELECTION twice.
SendMessageW(hDlg, BFFM_SETSELECTION, (WPARAM)FALSE, (LPARAM)pidl);
Sleep(100);
PostMessageW(hDlg, BFFM_SETSELECTION, (WPARAM)FALSE, (LPARAM)pidl);
break;
case BFFM_SELCHANGED:
// Update the status
if (SHGetPathFromIDListU((LPITEMIDLIST)lParam, dir)) {
SendMessageLU(hDlg, BFFM_SETSTATUSTEXT, 0, dir);
SetWindowTextU(hBrowseEdit, dir);
}
break;
}
return 0;
}
/*
* Browse for a folder and update the folder edit box
*/
void BrowseForFolder(void) {
BROWSEINFOW bi;
LPITEMIDLIST pidl;
WCHAR *wpath;
size_t i;
HRESULT hr;
IShellItem *psi = NULL;
IShellItem *si_path = NULL; // Automatically freed
IFileOpenDialog *pfod = NULL;
WCHAR *fname;
char* tmp_path = NULL;
dialog_showing++;
hr = CoCreateInstance(&CLSID_FileOpenDialog, NULL, CLSCTX_INPROC,
&IID_IFileOpenDialog, (LPVOID)&pfod);
if (FAILED(hr)) {
dprintf("CoCreateInstance for FileOpenDialog failed: error %X", hr);
pfod = NULL; // Just in case
goto fallback;
}
hr = pfod->lpVtbl->SetOptions(pfod, FOS_PICKFOLDERS);
if (FAILED(hr)) {
dprintf("Failed to set folder option for FileOpenDialog: error %X", hr);
goto fallback;
}
// Set the initial folder (if the path is invalid, will simply use last)
wpath = utf8_to_wchar(szFolderPath);
// The new IFileOpenDialog makes us split the path
fname = NULL;
if ((wpath != NULL) && (wcslen(wpath) >= 1)) {
for (i = wcslen(wpath) - 1; i != 0; i--) {
if (wpath[i] == L'\\') {
wpath[i] = 0;
fname = &wpath[i + 1];
break;
}
}
}
hr = SHCreateItemFromParsingName(wpath, NULL, &IID_IShellItem, (LPVOID)&si_path);
if (SUCCEEDED(hr)) {
if (wpath != NULL) {
pfod->lpVtbl->SetFolder(pfod, si_path);
}
if (fname != NULL) {
pfod->lpVtbl->SetFileName(pfod, fname);
}
}
safe_free(wpath);
hr = pfod->lpVtbl->Show(pfod, hMainDialog);
if (SUCCEEDED(hr)) {
hr = pfod->lpVtbl->GetResult(pfod, &psi);
if (SUCCEEDED(hr)) {
psi->lpVtbl->GetDisplayName(psi, SIGDN_FILESYSPATH, &wpath);
tmp_path = wchar_to_utf8(wpath);
CoTaskMemFree(wpath);
if (tmp_path == NULL) {
dprintf("Could not convert path");
} else {
static_strcpy(szFolderPath, tmp_path);
safe_free(tmp_path);
}
} else {
dprintf("Failed to set folder option for FileOpenDialog: error %X", hr);
}
} else if ((hr & 0xFFFF) != ERROR_CANCELLED) {
// If it's not a user cancel, assume the dialog didn't show and fallback
dprintf("Could not show FileOpenDialog: error %X", hr);
goto fallback;
}
pfod->lpVtbl->Release(pfod);
dialog_showing--;
return;
fallback:
if (pfod != NULL) {
pfod->lpVtbl->Release(pfod);
}
memset(&bi, 0, sizeof(BROWSEINFOW));
bi.hwndOwner = hMainDialog;
bi.lpszTitle = L"Please select folder";
bi.lpfn = BrowseInfoCallback;
// BIF_NONEWFOLDERBUTTON = 0x00000200 is unknown on MinGW
bi.ulFlags = BIF_RETURNFSANCESTORS | BIF_RETURNONLYFSDIRS |
BIF_DONTGOBELOWDOMAIN | BIF_EDITBOX | 0x00000200;
pidl = SHBrowseForFolderW(&bi);
if (pidl != NULL) {
CoTaskMemFree(pidl);
}
dialog_showing--;
}
/*
* read or write I/O to a file
* buffer is allocated by the procedure. path is UTF-8
*/
BOOL FileIo(BOOL save, char* path, char** buffer, DWORD* size)
{
SECURITY_ATTRIBUTES s_attr, *ps = NULL;
SECURITY_DESCRIPTOR s_desc;
PSID sid = NULL;
HANDLE handle;
BOOL r;
BOOL ret = FALSE;
// Change the owner from admin to regular user
sid = GetSid();
if ( (sid != NULL)
&& InitializeSecurityDescriptor(&s_desc, SECURITY_DESCRIPTOR_REVISION)
&& SetSecurityDescriptorOwner(&s_desc, sid, FALSE) ) {
s_attr.nLength = sizeof(SECURITY_ATTRIBUTES);
s_attr.bInheritHandle = FALSE;
s_attr.lpSecurityDescriptor = &s_desc;
ps = &s_attr;
} else {
dprintf("could not set security descriptor: %s", WindowsErrorString());
}
if (!save) {
*buffer = NULL;
}
handle = CreateFileU(path, save?GENERIC_WRITE:GENERIC_READ, FILE_SHARE_READ,
ps, save?CREATE_ALWAYS:OPEN_EXISTING, 0, NULL);
if (handle == INVALID_HANDLE_VALUE) {
dprintf("Could not %s file '%s'", save?"create":"open", path);
goto out;
}
if (save) {
r = WriteFile(handle, *buffer, *size, size, NULL);
} else {
*size = GetFileSize(handle, NULL);
*buffer = (char*)malloc(*size);
if (*buffer == NULL) {
dprintf("Could not allocate buffer for reading file");
goto out;
}
r = ReadFile(handle, *buffer, *size, size, NULL);
}
if (!r) {
dprintf("I/O Error: %s", WindowsErrorString());
goto out;
}
dsprintf("%s '%s'", save?"Saved file as":"Opened file", path);
ret = TRUE;
out:
CloseHandle(handle);
if (!ret) {
// Only leave a buffer allocated if successful
*size = 0;
if (!save) {
safe_free(*buffer);
}
}
return ret;
}
/*
* Return the UTF8 path of a file selected through a load or save dialog
* All string parameters are UTF-8
* IMPORTANT NOTE: Remember that you need to call CoInitializeEx() for
* *EACH* thread you invoke FileDialog from, as GetDisplayName() will
* return error 0x8001010E otherwise.
*/
char* FileDialog(BOOL save, char* path, const ext_t* ext, DWORD options)
{
DWORD tmp;
OPENFILENAMEA ofn;
char selected_name[MAX_PATH];
char *ext_string = NULL, *all_files = NULL;
size_t i, j, ext_strlen;
BOOL r;
char* filepath = NULL;
HRESULT hr = FALSE;
IFileDialog *pfd = NULL;
IShellItem *psiResult;
COMDLG_FILTERSPEC* filter_spec = NULL;
wchar_t *wpath = NULL, *wfilename = NULL;
IShellItem *si_path = NULL; // Automatically freed
if ((ext == NULL) || (ext->count == 0) || (ext->extension == NULL) || (ext->description == NULL))
return NULL;
dialog_showing++;
filter_spec = (COMDLG_FILTERSPEC*)calloc(ext->count + 1, sizeof(COMDLG_FILTERSPEC));
if (filter_spec != NULL) {
// Setup the file extension filter table
for (i = 0; i < ext->count; i++) {
filter_spec[i].pszSpec = utf8_to_wchar(ext->extension[i]);
filter_spec[i].pszName = utf8_to_wchar(ext->description[i]);
}
filter_spec[i].pszSpec = L"*.*";
filter_spec[i].pszName = L"All files";
hr = CoCreateInstance(save ? &CLSID_FileSaveDialog : &CLSID_FileOpenDialog, NULL, CLSCTX_INPROC,
&IID_IFileDialog, (LPVOID)&pfd);
if (FAILED(hr)) {
SetLastError(hr);
dprintf("CoCreateInstance for FileOpenDialog failed: %s\n", WindowsErrorString());
pfd = NULL; // Just in case
goto fallback;
}
// Set the file extension filters
pfd->lpVtbl->SetFileTypes(pfd, (UINT)ext->count + 1, filter_spec);
// Set the default directory
wpath = utf8_to_wchar(path);
hr = SHCreateItemFromParsingName(wpath, NULL, &IID_IShellItem, (LPVOID)&si_path);
if (SUCCEEDED(hr)) {
pfd->lpVtbl->SetFolder(pfd, si_path);
}
safe_free(wpath);
// Set the default filename
wfilename = utf8_to_wchar((ext->filename == NULL) ? "" : ext->filename);
if (wfilename != NULL) {
pfd->lpVtbl->SetFileName(pfd, wfilename);
}
// Display the dialog
hr = pfd->lpVtbl->Show(pfd, hMainDialog);
// Cleanup
safe_free(wfilename);
for (i = 0; i < ext->count; i++) {
safe_free(filter_spec[i].pszSpec);
safe_free(filter_spec[i].pszName);
}
safe_free(filter_spec);
if (SUCCEEDED(hr)) {
// Obtain the result of the user's interaction with the dialog.
hr = pfd->lpVtbl->GetResult(pfd, &psiResult);
if (SUCCEEDED(hr)) {
hr = psiResult->lpVtbl->GetDisplayName(psiResult, SIGDN_FILESYSPATH, &wpath);
if (SUCCEEDED(hr)) {
filepath = wchar_to_utf8(wpath);
CoTaskMemFree(wpath);
} else {
SetLastError(hr);
dprintf("Unable to access file path: %s\n", WindowsErrorString());
}
psiResult->lpVtbl->Release(psiResult);
}
} else if ((hr & 0xFFFF) != ERROR_CANCELLED) {
// If it's not a user cancel, assume the dialog didn't show and fallback
SetLastError(hr);
dprintf("Could not show FileOpenDialog: %s\n", WindowsErrorString());
goto fallback;
}
pfd->lpVtbl->Release(pfd);
dialog_showing--;
return filepath;
}
fallback:
safe_free(filter_spec);
if (pfd != NULL) {
pfd->lpVtbl->Release(pfd);
}
memset(&ofn, 0, sizeof(ofn));
ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = hMainDialog;
// Selected File name
static_sprintf(selected_name, "%s", (ext->filename == NULL) ? "" : ext->filename);
ofn.lpstrFile = selected_name;
ofn.nMaxFile = MAX_PATH;
// Set the file extension filters
all_files = "All files";
ext_strlen = 0;
for (i = 0; i<ext->count; i++) {
ext_strlen += safe_strlen(ext->description[i]) + 2 * safe_strlen(ext->extension[i]) + sizeof(" ()\r\r");
}
ext_strlen += safe_strlen(all_files) + sizeof(" (*.*)\r*.*\r");
ext_string = (char*)malloc(ext_strlen + 1);
if (ext_string == NULL)
return NULL;
ext_string[0] = 0;
for (i = 0, j = 0; i<ext->count; i++) {
j += _snprintf(&ext_string[j], ext_strlen - j, "%s (%s)\r%s\r", ext->description[i], ext->extension[i], ext->extension[i]);
}
j = _snprintf(&ext_string[j], ext_strlen - j, "%s (*.*)\r*.*\r", all_files);
// Microsoft could really have picked a better delimiter!
for (i = 0; i<ext_strlen; i++) {
// Since the VS Code Analysis tool is dumb...
#if defined(_MSC_VER)
#pragma warning(suppress: 6385)
#endif
if (ext_string[i] == '\r') {
#if defined(_MSC_VER)
#pragma warning(suppress: 6386)
#endif
ext_string[i] = 0;
}
}
ofn.lpstrFilter = ext_string;
ofn.nFilterIndex = 1;
ofn.lpstrInitialDir = path;
ofn.Flags = OFN_OVERWRITEPROMPT | options;
// Show Dialog
if (save) {
r = GetSaveFileNameU(&ofn);
} else {
r = GetOpenFileNameU(&ofn);
}
if (r) {
filepath = safe_strdup(selected_name);
} else {
tmp = CommDlgExtendedError();
if (tmp != 0) {
dprintf("Could not select file for %s. Error %X\n", save ? "save" : "open", tmp);
}
}
safe_free(ext_string);
dialog_showing--;
return filepath;
}
/*
* Create the application status bar
*/
void create_status_bar(void)
{
RECT rect;
int edge[2];
// Create the status bar.
hStatus = CreateWindowEx(0, STATUSCLASSNAME, NULL, WS_CHILD | WS_VISIBLE,
0, 0, 0, 0, hMainDialog, (HMENU)IDC_STATUS, main_instance, NULL);
// Create 2 status areas
GetClientRect(hMainDialog, &rect);
edge[0] = rect.right - (int)(100.0f*fScale);
edge[1] = rect.right;
SendMessage(hStatus, SB_SETPARTS, (WPARAM) 2, (LPARAM)&edge);
}
/*
* Center a dialog with regards to the main application Window or the desktop
* See http://msdn.microsoft.com/en-us/library/windows/desktop/ms644996.aspx#init_box
*/
void center_dialog(HWND hDlg)
{
HWND hParent;
RECT rc, rcDlg, rcParent;
if ((hParent = GetParent(hDlg)) == NULL) {
hParent = GetDesktopWindow();
}
GetWindowRect(hParent, &rcParent);
GetWindowRect(hDlg, &rcDlg);
CopyRect(&rc, &rcParent);
// Offset the parent and dialog box rectangles so that right and bottom
// values represent the width and height, and then offset the parent again
// to discard space taken up by the dialog box.
OffsetRect(&rcDlg, -rcDlg.left, -rcDlg.top);
OffsetRect(&rc, -rc.left, -rc.top);
OffsetRect(&rc, -rcDlg.right, -rcDlg.bottom);
SetWindowPos(hDlg, HWND_TOP, rcParent.left + (rc.right / 2), rcParent.top + (rc.bottom / 2) - 25, 0, 0, SWP_NOSIZE);
}
/*
* License callback
*/
INT_PTR CALLBACK license_callback(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message) {
case WM_INITDIALOG:
center_dialog(hDlg);
SetDlgItemTextA(hDlg, IDC_LICENSE_TEXT, gplv3);
break;
case WM_COMMAND:
switch (LOWORD(wParam)) {
case IDOK:
case IDCANCEL:
EndDialog(hDlg, LOWORD(wParam));
return (INT_PTR)TRUE;
}
}
return (INT_PTR)FALSE;
}
/*
* About dialog callback
*/
INT_PTR CALLBACK about_callback(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
int i;
const int edit_id[2] = {IDC_ABOUT_BLURB, IDC_ABOUT_COPYRIGHTS};
char about_blurb[2048];
const char* edit_text[2] = {about_blurb, additional_copyrights};
HWND hEdit[2];
TEXTRANGEW tr;
ENLINK* enl;
wchar_t wUrl[256];
switch (message) {
case WM_INITDIALOG:
dialog_showing++;
set_title_bar_icon(hDlg);
center_dialog(hDlg);
if (reg_commcheck)
ShowWindow(GetDlgItem(hDlg, IDC_ABOUT_UPDATES), SW_SHOW);
safe_sprintf(about_blurb, sizeof(about_blurb), about_blurb_format,
application_version[0], application_version[1], application_version[2]);
for (i=0; i<ARRAYSIZE(hEdit); i++) {
hEdit[i] = GetDlgItem(hDlg, edit_id[i]);
SendMessage(hEdit[i], EM_AUTOURLDETECT, 1, 0);
/* Can't use SetDlgItemText, because it only works with RichEdit20A... and VS insists
* on reverting to RichEdit20W as soon as you edit the dialog. You can try all the W
* methods you want, it JUST WON'T WORK unless you use EM_SETTEXTEX. Also see:
* http://blog.kowalczyk.info/article/eny/Setting-unicode-rtf-text-in-rich-edit-control.html */
SendMessageA(hEdit[i], EM_SETTEXTEX, (WPARAM)&friggin_microsoft_unicode_amateurs, (LPARAM)edit_text[i]);
SendMessage(hEdit[i], EM_SETSEL, -1, -1);
SendMessage(hEdit[i], EM_SETEVENTMASK, 0, ENM_LINK);
SendMessage(hEdit[i], EM_SETBKGNDCOLOR, 0, (LPARAM)GetSysColor(COLOR_BTNFACE));
}
break;
case WM_NOTIFY:
switch (((LPNMHDR)lParam)->code) {
case EN_LINK:
enl = (ENLINK*) lParam;
if (enl->msg == WM_LBUTTONUP) {
tr.lpstrText = wUrl;
tr.chrg.cpMin = enl->chrg.cpMin;
tr.chrg.cpMax = enl->chrg.cpMax;
SendMessageW(enl->nmhdr.hwndFrom, EM_GETTEXTRANGE, 0, (LPARAM)&tr);
wUrl[ARRAYSIZE(wUrl)-1] = 0;
ShellExecuteW(hDlg, L"open", wUrl, NULL, NULL, SW_SHOWNORMAL);
}
break;
}
break;
case WM_COMMAND:
switch (LOWORD(wParam)) {
case IDOK:
case IDCANCEL:
EndDialog(hDlg, LOWORD(wParam));
dialog_showing--;
return (INT_PTR)TRUE;
case IDC_ABOUT_LICENSE:
DialogBoxW(main_instance, MAKEINTRESOURCEW(IDD_LICENSE), hDlg, license_callback);
break;
case IDC_ABOUT_UPDATES:
DialogBoxW(main_instance, MAKEINTRESOURCEW(IDD_UPDATE_POLICY), hDlg, UpdateCallback);
break;
}
break;
}
return (INT_PTR)FALSE;
}
/*
* We use our own MessageBox for notifications to have greater control (center, no close button, etc)
*/
INT_PTR CALLBACK notification_callback(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
LRESULT loc;
int i;
// Prevent resizing
static LRESULT disabled[9] = { HTLEFT, HTRIGHT, HTTOP, HTBOTTOM, HTSIZE,
HTTOPLEFT, HTTOPRIGHT, HTBOTTOMLEFT, HTBOTTOMRIGHT };
static HBRUSH white_brush, separator_brush;
switch (message) {
case WM_INITDIALOG:
white_brush = CreateSolidBrush(WHITE);
separator_brush = CreateSolidBrush(SEPARATOR_GREY);
set_title_bar_icon(hDlg);
center_dialog(hDlg);
// Change the default icon
if (Static_SetIcon(GetDlgItem(hDlg, IDC_NOTIFICATION_ICON), hMessageIcon) == 0) {
dprintf("Could not set dialog icon\n");
}
// Set the dialog title
if (szMessageTitle != NULL) {
SetWindowTextU(hDlg, szMessageTitle);
}
// Enable/disable the buttons and set text
if (!notification_is_question) {
SetWindowTextU(GetDlgItem(hDlg, IDNO), "Close");
} else {
ShowWindow(GetDlgItem(hDlg, IDYES), SW_SHOW);
}
if ((notification_more_info != NULL) && (notification_more_info->callback != NULL)) {
ShowWindow(GetDlgItem(hDlg, IDC_MORE_INFO), SW_SHOW);
}
// Set the control text
if (szMessageText != NULL) {
SetWindowTextU(GetDlgItem(hDlg, IDC_NOTIFICATION_TEXT), szMessageText);
}
return (INT_PTR)TRUE;
case WM_CTLCOLORSTATIC:
// Change the background colour for static text and icon
SetBkMode((HDC)wParam, TRANSPARENT);
if ((HWND)lParam == GetDlgItem(hDlg, IDC_NOTIFICATION_LINE)) {
return (INT_PTR)separator_brush;
}
return (INT_PTR)white_brush;
case WM_NCHITTEST:
// Check coordinates to prevent resize actions
loc = DefWindowProc(hDlg, message, wParam, lParam);
for(i = 0; i < 9; i++) {
if (loc == disabled[i]) {
return (INT_PTR)TRUE;
}
}
return (INT_PTR)FALSE;
case WM_COMMAND:
switch (LOWORD(wParam)) {
case IDOK:
case IDCANCEL:
case IDYES:
case IDNO:
EndDialog(hDlg, LOWORD(wParam));
return (INT_PTR)TRUE;
case IDC_MORE_INFO:
if (notification_more_info != NULL)
DialogBoxW(main_instance, MAKEINTRESOURCEW(notification_more_info->id),
hDlg, notification_more_info->callback);
break;
}
break;
}
return (INT_PTR)FALSE;
}
/*
* Display a custom notification
*/
BOOL notification(int type, const notification_info* more_info, char* title, char* format, ...)
{
BOOL ret;
va_list args;
dialog_showing++;
szMessageText = (char*)malloc(MAX_PATH);
if (szMessageText == NULL) return FALSE;
szMessageTitle = title;
va_start(args, format);
safe_vsnprintf(szMessageText, MAX_PATH-1, format, args);
va_end(args);
szMessageText[MAX_PATH-1] = 0;
notification_more_info = more_info;
notification_is_question = FALSE;
switch(type) {
case MSG_WARNING:
hMessageIcon = LoadIcon(NULL, IDI_WARNING);
break;
case MSG_ERROR:
hMessageIcon = LoadIcon(NULL, IDI_ERROR);
break;
case MSG_QUESTION:
hMessageIcon = LoadIcon(NULL, IDI_QUESTION);
notification_is_question = TRUE;
break;
case MSG_INFO:
default:
hMessageIcon = LoadIcon(NULL, IDI_INFORMATION);
break;
}
ret = (DialogBox(main_instance, MAKEINTRESOURCE(IDD_NOTIFICATION), hMainDialog, notification_callback) == IDYES);
safe_free(szMessageText);
dialog_showing--;
return ret;
}
struct {
HWND hTip;
WNDPROC original_proc;
LPWSTR wstring;
} ttlist[MAX_TOOLTIPS] = { {0} };
INT_PTR CALLBACK tooltip_callback(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
LPNMTTDISPINFOW lpnmtdi;
int i = MAX_TOOLTIPS;
// Make sure we have an original proc
for (i=0; i<MAX_TOOLTIPS; i++) {
if (ttlist[i].hTip == hDlg) break;
}
if (i == MAX_TOOLTIPS) {
return (INT_PTR)FALSE;
}
switch (message)
{
case WM_NOTIFY:
switch (((LPNMHDR)lParam)->code) {
case TTN_GETDISPINFOW:
lpnmtdi = (LPNMTTDISPINFOW)lParam;
lpnmtdi->lpszText = ttlist[i].wstring;
SendMessage(hDlg, TTM_SETMAXTIPWIDTH, 0, 300);
return (INT_PTR)TRUE;
}
break;
}
return CallWindowProc(ttlist[i].original_proc, hDlg, message, wParam, lParam);
}
/*
* Create a tooltip for the control passed as first parameter
* duration sets the duration in ms. Use -1 for default
* message is an UTF-8 string
*/
HWND create_tooltip(HWND hControl, char* message, int duration)
{
TOOLINFOW toolInfo = {0};
int i;
if ( (hControl == NULL) || (message == NULL) ) {
return (HWND)NULL;
}
// Find an empty slot
for (i=0; i<MAX_TOOLTIPS; i++) {
if (ttlist[i].hTip == NULL) break;
}
if (i == MAX_TOOLTIPS) {
return (HWND)NULL; // No more space
}
// Create the tooltip window
ttlist[i].hTip = CreateWindowExW(0, TOOLTIPS_CLASSW, NULL, WS_POPUP | TTS_NOPREFIX | TTS_ALWAYSTIP,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, hMainDialog, NULL,
main_instance, NULL);
if (ttlist[i].hTip == NULL) {
return (HWND)NULL;
}
// Subclass the tooltip to handle multiline
ttlist[i].original_proc = (WNDPROC)SetWindowLongPtr(ttlist[i].hTip, GWLP_WNDPROC, (LONG_PTR)tooltip_callback);
// Set the string to display (can be multiline)
ttlist[i].wstring = utf8_to_wchar(message);
// Set tooltip duration (ms)
PostMessage(ttlist[i].hTip, TTM_SETDELAYTIME, (WPARAM)TTDT_AUTOPOP, (LPARAM)duration);
// Associate the tooltip to the control
toolInfo.cbSize = sizeof(toolInfo);
toolInfo.hwnd = ttlist[i].hTip; // Set to the tooltip itself to ease up subclassing
toolInfo.uFlags = TTF_IDISHWND | TTF_SUBCLASS;
toolInfo.uId = (UINT_PTR)hControl;
toolInfo.lpszText = LPSTR_TEXTCALLBACKW;
SendMessageW(ttlist[i].hTip, TTM_ADDTOOLW, 0, (LPARAM)&toolInfo);
return ttlist[i].hTip;
}
void destroy_tooltip(HWND hWnd)
{
int i;
if (hWnd == NULL) return;
for (i=0; i<MAX_TOOLTIPS; i++) {
if (ttlist[i].hTip == hWnd) break;
}
if (i == MAX_TOOLTIPS) return;
DestroyWindow(hWnd);
safe_free(ttlist[i].wstring);
ttlist[i].original_proc = NULL;
ttlist[i].hTip = NULL;
}
void destroy_all_tooltips(void)
{
int i;
for (i=0; i<MAX_TOOLTIPS; i++) {
if (ttlist[i].hTip == NULL) continue;
DestroyWindow(ttlist[i].hTip);
safe_free(ttlist[i].wstring);
}
}
/*
* Update policy and settings dialog callback
*/
INT_PTR CALLBACK UpdateCallback(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
HWND hPolicy;
static HWND hFrequency, hBeta;
int32_t freq;
char update_policy_text[4096];
switch (message) {
case WM_INITDIALOG:
set_title_bar_icon(hDlg);
center_dialog(hDlg);
hFrequency = GetDlgItem(hDlg, IDC_UPDATE_FREQUENCY);
hBeta = GetDlgItem(hDlg, IDC_INCLUDE_BETAS);
IGNORE_RETVAL(ComboBox_SetItemData(hFrequency, ComboBox_AddStringU(hFrequency, "Disabled"), -1));
IGNORE_RETVAL(ComboBox_SetItemData(hFrequency, ComboBox_AddStringU(hFrequency, "Daily (Default)"), 86400));
IGNORE_RETVAL(ComboBox_SetItemData(hFrequency, ComboBox_AddStringU(hFrequency, "Weekly"), 604800));
IGNORE_RETVAL(ComboBox_SetItemData(hFrequency, ComboBox_AddStringU(hFrequency, "Monthly"), 2629800));
freq = ReadRegistryKey32(REGKEY_HKCU, REGKEY_UPDATE_INTERVAL);
EnableWindow(GetDlgItem(hDlg, IDC_CHECK_NOW), (freq != 0));
EnableWindow(hBeta, (freq >= 0));
switch(freq) {
case -1:
IGNORE_RETVAL(ComboBox_SetCurSel(hFrequency, 0));
break;
case 0:
case 86400:
IGNORE_RETVAL(ComboBox_SetCurSel(hFrequency, 1));
break;
case 604800:
IGNORE_RETVAL(ComboBox_SetCurSel(hFrequency, 2));
break;
case 2629800:
IGNORE_RETVAL(ComboBox_SetCurSel(hFrequency, 3));
break;