-
Notifications
You must be signed in to change notification settings - Fork 2
/
entryFuncs.cpp
1138 lines (1026 loc) · 34.8 KB
/
entryFuncs.cpp
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
/*
ax7z entry funcs
*/
#define NOMINMAX
#include <windows.h>
#undef NOMINMAX
#include <commctrl.h>
#include <shlobj.h>
#include <vector>
#include <algorithm>
#include <numeric>
#include <map>
#include <set>
#include "entryFuncs.h"
#include "infcache.h"
#include <sstream>
#include "resource.h"
#include "7z/7zip/Bundles/ax7z/PasswordManager.h"
#include <boost/algorithm/string/split.hpp>
#include <boost/algorithm/string/classification.hpp>
#include "7z/7zip/Bundles/ax7z/SolidCache.h"
#include "version.hpp"
#include <sys/types.h>
#include <sys/stat.h>
#include "release.h"
#include "7z/Windows/FileDir.h"
struct NoCaseLess
{
bool operator()(const std::string &s1, const std::string &s2) const
{
return _stricmp(s1.c_str(), s2.c_str()) < 0;
}
};
//拡張子管理クラス
class ExtManager
{
private:
typedef std::string Ext;
struct Info
{
std::vector<std::string> methods;
bool enable;
};
std::map<Ext, Info, NoCaseLess> m_mTable;
std::set<Ext, NoCaseLess> m_sTableUser;
public:
static const char* const SECTION_NAME;
static const char* const DEFAULT_EXTENSIONS;
ExtManager() {}
typedef std::vector<std::pair<std::string, std::string> > Conf;
void Init(const Conf& conf);
bool IsEnable(LPSTR filename) const;
void SetEnable(const std::string &sExt, bool fEnable)
{
m_mTable[sExt].enable = fEnable;
}
typedef std::map<Ext, Info, NoCaseLess>::value_type EachValueType;
template<typename Functor>
void Each(Functor f) const
{
std::for_each(m_mTable.begin(), m_mTable.end(), f);
}
void SetDefault();
void Save(const std::string &sIniFileName) const;
void Load(const std::string &sIniFileName);
void SetPluginInfo(std::vector<std::string> &info) const;
void AddUserExt(const std::string& sExt);
void RemoveUserExt(const std::string& sExt);
void SetUserExtensions(const std::string& sExts);
std::string GetUserExtensions() const;
};
const char* const ExtManager::SECTION_NAME = "ax7z_s";
const char* const ExtManager::DEFAULT_EXTENSIONS = "bin;img;mdf";
void ExtManager::Init(const Conf& conf)
{
Conf::const_iterator ciEnd = conf.end();
for(Conf::const_iterator ci = conf.begin(); ci != ciEnd; ++ci) {
m_mTable[ci->first].enable = true;
m_mTable[ci->first].methods.push_back(ci->second);
}
}
bool ExtManager::IsEnable(LPSTR filename) const
{
char buf[_MAX_EXT];
_splitpath(filename, NULL, NULL, NULL, buf);
if(buf[0] == 0) buf[1] = 0; // guard
std::string sBuf(buf+1); // skip period
std::map<Ext, Info, NoCaseLess>::const_iterator ci = m_mTable.find(sBuf);
if(ci != m_mTable.end()) {
return ci->second.enable;
}
std::set<Ext, NoCaseLess>::const_iterator ciUser = m_sTableUser.find(sBuf);
if(ciUser != m_sTableUser.end()) {
return true;
}
return false;
}
void ExtManager::SetDefault()
{
std::map<Ext, Info, NoCaseLess>::iterator it, itEnd = m_mTable.end();
for(it = m_mTable.begin(); it != itEnd; ++it) {
it->second.enable = true;
}
}
void ExtManager::Save(const std::string &sIniFileName) const
{
if(m_sTableUser.size()) {
const std::string &sResult = GetUserExtensions();
WritePrivateProfileString(SECTION_NAME, "user_extensions", sResult.c_str(), sIniFileName.c_str());
} else {
WritePrivateProfileString(SECTION_NAME, "user_extensions", NULL, sIniFileName.c_str());
}
std::map<Ext, Info, NoCaseLess>::const_iterator ci, ciEnd = m_mTable.end();
for(ci = m_mTable.begin(); ci != ciEnd; ++ci) {
WritePrivateProfileString(SECTION_NAME, ci->first.c_str(), ci->second.enable ? "1" : "0", sIniFileName.c_str());
}
}
void ExtManager::Load(const std::string &sIniFileName)
{
std::vector<char> vBuf(1024);
DWORD dwSize;
do {
vBuf.resize(vBuf.size() * 2);
dwSize = GetPrivateProfileString(SECTION_NAME, "user_extensions", DEFAULT_EXTENSIONS, &vBuf[0], vBuf.size(), sIniFileName.c_str());
} while(dwSize == vBuf.size() - 1);
std::string sResult(&vBuf[0]);
SetUserExtensions(sResult);
std::map<Ext, Info, NoCaseLess>::iterator mi, miEnd = m_mTable.end();
for(mi = m_mTable.begin(); mi != miEnd; ++mi) {
mi->second.enable = GetPrivateProfileInt(SECTION_NAME, mi->first.c_str(), 1, sIniFileName.c_str()) != 0;
}
}
struct MyConcat2
{
std::string operator()(const std::string &s1, const std::string &s2) const
{
if(s1 == "")
return "*." + s2;
return s1 + ";" + "*." + s2;
}
};
void ExtManager::SetPluginInfo(std::vector<std::string> &vsPluginInfo) const
{
std::map<std::string, std::string> mResmap;
std::map<Ext, Info, NoCaseLess>::const_iterator ci, ciEnd = m_mTable.end();
for(ci = m_mTable.begin(); ci != ciEnd; ++ci) {
if(!ci->second.enable) continue;
std::vector<std::string>::const_iterator ci2, ciEnd2 = ci->second.methods.end();
for(ci2 = ci->second.methods.begin(); ci2 != ciEnd2; ++ci2) {
if(!mResmap[*ci2].empty()) {
mResmap[*ci2] += ";";
}
mResmap[*ci2] += "*.";
mResmap[*ci2] += ci->first;
}
}
vsPluginInfo.clear();
vsPluginInfo.push_back("00AM");
vsPluginInfo.push_back("7z extract library v0.7 for 7-zip 4.57+ " RELEASE_STR " (C) Makito Miyano / enhanced by Yak!");
std::map<std::string, std::string>::const_iterator ci2, ciEnd2 = mResmap.end();
for(ci2 = mResmap.begin(); ci2 != ciEnd2; ++ci2) {
vsPluginInfo.push_back(ci2->second);
vsPluginInfo.push_back(ci2->first + " files");
}
if(m_sTableUser.size()) {
vsPluginInfo.push_back(std::accumulate(m_sTableUser.begin(), m_sTableUser.end(), std::string(), MyConcat2()));
vsPluginInfo.push_back("User-defined");
}
}
void ExtManager::AddUserExt(const std::string& sExt)
{
m_sTableUser.insert(sExt);
}
void ExtManager::RemoveUserExt(const std::string& sExt)
{
m_sTableUser.erase(sExt);
}
struct MyConcat
{
std::string operator()(const std::string &s1, const std::string &s2) const
{
if(s1 == "") return s2;
return s1 + ";" + s2;
}
};
std::string ExtManager::GetUserExtensions() const
{
return std::accumulate(m_sTableUser.begin(), m_sTableUser.end(), std::string(), MyConcat());
}
void ExtManager::SetUserExtensions(const std::string& sExts)
{
boost::algorithm::split(m_sTableUser, sExts, boost::algorithm::is_any_of(";"), boost::algorithm::token_compress_on);
m_sTableUser.erase("");
}
//グローバル変数
static InfoCache infocache; //アーカイブ情報キャッシュクラス
static InfoCacheW infocacheW; //アーカイブ情報キャッシュクラス
static std::string g_sIniFileName; // ini ファイル名
static ExtManager g_extManager;
static bool g_fSharedConf;
HINSTANCE g_hInstance;
int g_nSolidEnable7z = 1;
int g_nSolidEnableRar = 1;
#ifndef _UNICODE
bool g_IsNT = false;
static inline bool IsItWindowsNT()
{
OSVERSIONINFO versionInfo;
versionInfo.dwOSVersionInfoSize = sizeof(versionInfo);
if (!::GetVersionEx(&versionInfo))
return false;
return (versionInfo.dwPlatformId == VER_PLATFORM_WIN32_NT);
}
#endif
void SetParamDefault()
{
g_extManager.SetDefault();
g_extManager.SetUserExtensions(ExtManager::DEFAULT_EXTENSIONS);
g_nSolidEnable7z = 1;
g_nSolidEnableRar = 1;
SolidCache& sc = SolidCache::GetInstance();
sc.SetMaxLookAhead(-1);
sc.SetMaxMemory(100);
sc.SetMaxDisk(1024);
sc.SetPurgeMemory(10);
sc.SetPurgeDisk(10);
// char buf[2048];
// GetTempPath(sizeof(buf), buf);
// sc.SetCacheFolder(buf);
g_fSharedConf = false;
}
std::string GetIniFileName()
{
return g_sIniFileName;
}
void LoadFromIni()
{
SetParamDefault();
std::string sIniFileName = GetIniFileName();
g_extManager.Load(sIniFileName);
SolidCache& sc = SolidCache::GetInstance();
g_nSolidEnable7z = GetPrivateProfileInt(ExtManager::SECTION_NAME, "solid7z", g_nSolidEnable7z, sIniFileName.c_str());
g_nSolidEnableRar = GetPrivateProfileInt(ExtManager::SECTION_NAME, "solidrar", g_nSolidEnableRar, sIniFileName.c_str());
sc.SetMaxLookAhead(GetPrivateProfileInt(ExtManager::SECTION_NAME, "lookahead", sc.GetMaxLookAhead(), sIniFileName.c_str()));
sc.SetMaxMemory(GetPrivateProfileInt(ExtManager::SECTION_NAME, "memory", sc.GetMaxMemory(), sIniFileName.c_str()));
sc.SetMaxDisk(GetPrivateProfileInt(ExtManager::SECTION_NAME, "disk", sc.GetMaxDisk(), sIniFileName.c_str()));
sc.SetPurgeMemory(GetPrivateProfileInt(ExtManager::SECTION_NAME, "purge_memory", sc.GetPurgeMemory(), sIniFileName.c_str()));
sc.SetPurgeDisk(GetPrivateProfileInt(ExtManager::SECTION_NAME, "purge_disk", sc.GetPurgeDisk(), sIniFileName.c_str()));
char buf[2048], buf2[2048];
GetTempPath(sizeof(buf2), buf2);
GetPrivateProfileString(ExtManager::SECTION_NAME, "folder", buf2, buf, sizeof(buf), sIniFileName.c_str());
sc.SetCacheFolder(buf);
std::vector<char> vBuf(1024);
DWORD dwSize;
do {
vBuf.resize(vBuf.size() * 2);
dwSize = GetPrivateProfileString(ExtManager::SECTION_NAME, "topmost", "false", &vBuf[0], vBuf.size(), sIniFileName.c_str());
} while(dwSize == vBuf.size() - 1);
std::string sResult(&vBuf[0]);
PasswordManager::Get().SetTopMost(sResult == "true");
int nShared = GetPrivateProfileInt(ExtManager::SECTION_NAME, "shared", -1, sIniFileName.c_str());
if(nShared != -1) g_fSharedConf = nShared;
}
bool SaveToIni()
{
std::string sIniFileName = GetIniFileName();
g_extManager.Save(sIniFileName);
char buf[2048];
WritePrivateProfileString(ExtManager::SECTION_NAME, "solid7z", g_nSolidEnable7z ? "1" : "0", sIniFileName.c_str());
WritePrivateProfileString(ExtManager::SECTION_NAME, "solidrar", g_nSolidEnableRar ? "1" : "0", sIniFileName.c_str());
SolidCache& sc = SolidCache::GetInstance();
wsprintf(buf, "%d", sc.GetMaxLookAhead());
WritePrivateProfileString(ExtManager::SECTION_NAME, "lookahead", buf, sIniFileName.c_str());
wsprintf(buf, "%d", sc.GetMaxMemory());
WritePrivateProfileString(ExtManager::SECTION_NAME, "memory", buf, sIniFileName.c_str());
wsprintf(buf, "%d", sc.GetMaxDisk());
WritePrivateProfileString(ExtManager::SECTION_NAME, "disk", buf, sIniFileName.c_str());
wsprintf(buf, "%d", sc.GetPurgeMemory());
WritePrivateProfileString(ExtManager::SECTION_NAME, "purge_memory", buf, sIniFileName.c_str());
wsprintf(buf, "%d", sc.GetPurgeDisk());
WritePrivateProfileString(ExtManager::SECTION_NAME, "purge_disk", buf, sIniFileName.c_str());
WritePrivateProfileString(ExtManager::SECTION_NAME, "folder", sc.GetCacheFolder().c_str(), sIniFileName.c_str());
WritePrivateProfileString(ExtManager::SECTION_NAME, "topmost", PasswordManager::Get().GetTopMost() ? "true" : "false", sIniFileName.c_str());
int nResult = 1;
nResult &= WritePrivateProfileString(ExtManager::SECTION_NAME, "topmost", PasswordManager::Get().GetTopMost() ? "true" : "false", sIniFileName.c_str());
if(g_fSharedConf)
nResult &= WritePrivateProfileString(ExtManager::SECTION_NAME, "shared", "1", sIniFileName.c_str());
return nResult;
}
std::string GetSharedIniFileName(HANDLE hModule)
{
std::vector<char> vModulePath(1024);
size_t nLen = GetModuleFileName((HMODULE)hModule, &vModulePath[0], (DWORD)vModulePath.size());
vModulePath.resize(nLen + 1);
std::string sResult(&vModulePath[0]);
sResult += ".ini";
return sResult;
}
void MakePersonalIniFolder()
{
char p[MAX_PATH];
SHGetFolderPath(0, CSIDL_APPDATA, 0, SHGFP_TYPE_CURRENT, p);
std::string s(p);
s += "\\ax7z";
CreateDirectory(s.c_str(), 0);
}
std::string GetPersonalIniFileName(HANDLE hModule)
{
char p[MAX_PATH];
SHGetFolderPath(0, CSIDL_APPDATA, 0, SHGFP_TYPE_CURRENT, p);
std::string s(p);
s += "\\ax7z";
std::vector<char> vModulePath(1024);
size_t nLen = GetModuleFileName((HMODULE)hModule, &vModulePath[0], (DWORD)vModulePath.size());
vModulePath.resize(nLen + 1);
std::vector<char>::reverse_iterator it = find(vModulePath.rbegin(), vModulePath.rend(), '\\');
s += "\\";
s += std::string(&*it.base());
s += ".ini";
return s;
}
void SetIniFileName(HANDLE hModule)
{
std::string sPersonal(GetPersonalIniFileName(hModule));
struct __stat64 st;
if(!_stat64(sPersonal.c_str(), &st)) { // If personal ini file found, always used
g_sIniFileName = sPersonal;
g_fSharedConf = false;
return;
}
std::string sShared(GetSharedIniFileName(hModule));
if(!_stat64(sShared.c_str(), &st)) { // Found
int nShared = GetPrivateProfileInt(ExtManager::SECTION_NAME, "shared", -1, sShared.c_str());
if(nShared == 1) {
g_sIniFileName = sShared;
g_fSharedConf = true;
return;
}
char buf[1024];
wsprintf(buf,
"The %s.spi.ini file is found where the %s.spi file exists.\n"
"To support multiple users, the default place of the %s.spi.ini file is changed to your AppData folder.\n"
"\n"
"Do you want to move the %s.spi.ini file to your AppData folder?\n"
"If you can not understand what it is, please select NO.",
ExtManager::SECTION_NAME, ExtManager::SECTION_NAME, ExtManager::SECTION_NAME, ExtManager::SECTION_NAME);
int nRet = MessageBox(NULL, buf,
ExtManager::SECTION_NAME, MB_TASKMODAL | MB_ICONWARNING | MB_YESNO);
if(nRet == IDNO) {
WritePrivateProfileString(ExtManager::SECTION_NAME, "shared", "1", sShared.c_str());
g_sIniFileName = sShared;
g_fSharedConf = true;
return;
} else {
MakePersonalIniFolder();
if(!CopyFile(sShared.c_str(), sPersonal.c_str(), TRUE)) {
MessageBox(NULL, "Copy of the ini file is failed. Use the old ini file.", ExtManager::SECTION_NAME, MB_TASKMODAL | MB_ICONWARNING | MB_OK);
g_sIniFileName = sShared;
g_fSharedConf = true;
return;
}
if(!DeleteFile(sShared.c_str())) {
MessageBox(NULL, "Delete of the old ini file is failed. Leave as it is.", ExtManager::SECTION_NAME, MB_TASKMODAL | MB_ICONWARNING | MB_OK);
}
}
}
g_sIniFileName = sPersonal;
g_fSharedConf = false;
}
void handle_exception(const std::string &s)
{
try {
throw;
} catch (std::exception &e) {
OutputDebugPrintf("%s: exception %s thrown", s.c_str(), e.what());
} catch (...) {
OutputDebugPrintf("%s: unknown exception thrown", s.c_str());
}
}
/* エントリポイント */
BOOL APIENTRY SpiEntryPoint(HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
{
try {
bool bInitPath = false;
INITCOMMONCONTROLSEX ice = { sizeof(INITCOMMONCONTROLSEX), ICC_PROGRESS_CLASS };
switch (ul_reason_for_call) {
case DLL_PROCESS_ATTACH:
#ifndef _UNICODE
g_IsNT = IsItWindowsNT();
#endif
InitCommonControlsEx(&ice);
{
extern void GetFormats(ExtManager::Conf &res);
ExtManager::Conf v;
GetFormats(v);
g_extManager.Init(v);
}
SetIniFileName(hModule);
LoadFromIni();
bInitPath = true;
break;
case DLL_THREAD_ATTACH:
SetIniFileName(hModule);
LoadFromIni();
break;
case DLL_THREAD_DETACH:
break;
case DLL_PROCESS_DETACH:
break;
}
} catch (...) {
handle_exception(__FUNCTION__);
return FALSE;
}
return TRUE;
}
//---------------------------------------------------------------------------
/* エントリポイント */
BOOL APIENTRY DllMain(HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
{
int a = sizeof(fileInfoW);
switch (ul_reason_for_call) {
case DLL_PROCESS_DETACH:
infocache.Clear();
infocacheW.Clear();
break;
}
g_hInstance = (HINSTANCE)hModule;
return SpiEntryPoint(hModule, ul_reason_for_call, lpReserved);
}
/***************************************************************************
* SPI関数
***************************************************************************/
//---------------------------------------------------------------------------
int __stdcall GetPluginInfo(int infono, LPSTR buf, int buflen)
{
try {
std::vector<std::string> vsPluginInfo;
g_extManager.SetPluginInfo(vsPluginInfo);
if (infono < 0 || infono >= (int)vsPluginInfo.size()) {
return 0;
}
lstrcpyn(buf, vsPluginInfo[infono].c_str(), buflen);
return lstrlen(buf);
} catch (...) {
handle_exception(__FUNCTION__);
return 0;
}
}
static bool CheckFileExtension(const char* pFileName, const char* pExtension)
{
int nExtensionLen = strlen(pExtension);
int nFileNameLen = strlen(pFileName);
// ピリオドを入れてファイル名本体が存在するか?
if (nFileNameLen <= nExtensionLen + 1) {
return false;
}
return (strnicmp(pFileName + nFileNameLen - nExtensionLen, pExtension, nExtensionLen) == 0);
}
static bool CheckFileExtensionW(const wchar_t* pFileName, const wchar_t* pExtension)
{
int nExtensionLen = wcslen(pExtension);
int nFileNameLen = wcslen(pFileName);
// ピリオドを入れてファイル名本体が存在するか?
if (nFileNameLen <= nExtensionLen + 1) {
return false;
}
return (wcsnicmp(pFileName + nFileNameLen - nExtensionLen, pExtension, nExtensionLen) == 0);
}
//---------------------------------------------------------------------------
int __stdcall IsSupported(LPSTR filename, DWORD dw)
{
try {
// 現時点では名前のみで判断
return g_extManager.IsEnable(filename);
} catch(...) {
handle_exception(__FUNCTION__);
return 0;
}
}
int __stdcall IsSupportedW(LPWSTR filename, DWORD dw)
{
// Unicode 未サポート
return 0;
}
//---------------------------------------------------------------------------
//アーカイブ情報をキャッシュする
int GetArchiveInfoCache(char *filename, long len, HLOCAL *phinfo, fileInfo *pinfo)
{
int ret = infocache.Dupli(filename, phinfo, pinfo);
if (ret != SPI_NO_FUNCTION) return ret;
//キャッシュに無い
HLOCAL hinfo;
ret = GetArchiveInfoEx(filename, len, &hinfo);
if (ret != SPI_ALL_RIGHT) return ret;
//キャッシュ
infocache.Add(filename, &hinfo);
if (phinfo != NULL) {
UINT size = LocalSize(hinfo);
/* 出力用のメモリの割り当て */
*phinfo = LocalAlloc(LMEM_FIXED, size);
if (*phinfo == NULL) {
return SPI_NO_MEMORY;
}
memcpy(*phinfo, (void*)hinfo, size);
} else {
fileInfo *ptmp = (fileInfo *)hinfo;
if (pinfo->filename[0] != '\0') {
for (;;) {
if (ptmp->method[0] == '\0') return SPI_NO_FUNCTION;
// complete path relative to archive root
char path[sizeof(ptmp->path)+sizeof(ptmp->filename)];
strcpy(path, ptmp->path);
size_t len = strlen(path);
if(len && path[len-1] != '/' && path[len-1] != '\\') // need delimiter
strcat(path, "\\");
strcat(path, ptmp->filename);
if (lstrcmpi(path, pinfo->filename) == 0) break;
ptmp++;
}
} else {
for (;;) {
if (ptmp->method[0] == '\0') return SPI_NO_FUNCTION;
if (ptmp->position == pinfo->position) break;
ptmp++;
}
}
*pinfo = *ptmp;
}
return SPI_ALL_RIGHT;
}
int GetArchiveInfoCacheW(wchar_t *filename, long len, HLOCAL *phinfo, fileInfoW *pinfo)
{
int ret = infocacheW.Dupli(filename, phinfo, pinfo);
if (ret != SPI_NO_FUNCTION) return ret;
//キャッシュに無い
HLOCAL hinfo;
ret = GetArchiveInfoWEx(filename, len, &hinfo);
if (ret != SPI_ALL_RIGHT) return ret;
//キャッシュ
infocacheW.Add(filename, &hinfo);
if (phinfo != NULL) {
UINT size = LocalSize(hinfo);
/* 出力用のメモリの割り当て */
*phinfo = LocalAlloc(LMEM_FIXED, size);
if (*phinfo == NULL) {
return SPI_NO_MEMORY;
}
memcpy(*phinfo, (void*)hinfo, size);
} else {
fileInfoW *ptmp = (fileInfoW *)hinfo;
if (pinfo->filename[0] != L'\0') {
for (;;) {
if (ptmp->method[0] == '\0') return SPI_NO_FUNCTION;
// complete path relative to archive root
wchar_t path[sizeof(ptmp->path)+sizeof(ptmp->filename)];
wcscpy(path, ptmp->path);
size_t len = wcslen(path);
if(len && path[len-1] != L'/' && path[len-1] != L'\\') // need delimiter
wcscat(path, L"\\");
wcscat(path, ptmp->filename);
if (wcsicmp(path, pinfo->filename) == 0) break;
ptmp++;
}
} else {
for (;;) {
if (ptmp->method[0] == '\0') return SPI_NO_FUNCTION;
if (ptmp->position == pinfo->position) break;
ptmp++;
}
}
*pinfo = *ptmp;
}
return SPI_ALL_RIGHT;
}
//---------------------------------------------------------------------------
int __stdcall GetArchiveInfo(LPSTR buf, long len, unsigned int flag, HLOCAL *lphInf)
{
try {
//メモリ入力には対応しない
if ((flag & 7) != 0) return SPI_NO_FUNCTION;
*lphInf = NULL;
return GetArchiveInfoCache(buf, len, lphInf, NULL);
} catch(...) {
handle_exception(__FUNCTION__);
return SPI_OTHER_ERROR;
}
}
int __stdcall GetArchiveInfoW(LPWSTR buf, long len, unsigned int flag, HLOCAL *lphInf)
{
try {
//メモリ入力には対応しない
if ((flag & 7) != 0) return SPI_NO_FUNCTION;
*lphInf = NULL;
return GetArchiveInfoCacheW(buf, len, lphInf, NULL);
} catch(...) {
handle_exception(__FUNCTION__);
return SPI_OTHER_ERROR;
}
}
//---------------------------------------------------------------------------
int __stdcall GetFileInfo
(LPSTR buf, long len, LPSTR filename, unsigned int flag, struct fileInfo *lpInfo)
{
try {
//メモリ入力には対応しない
if ((flag & 7) != 0) return SPI_NO_FUNCTION;
lstrcpy(lpInfo->filename, filename);
return GetArchiveInfoCache(buf, len, NULL, lpInfo);
} catch(...) {
handle_exception(__FUNCTION__);
return SPI_OTHER_ERROR;
}
}
int __stdcall GetFileInfoW
(LPWSTR buf, long len, LPWSTR filename, unsigned int flag, struct fileInfoW *lpInfo)
{
try {
//メモリ入力には対応しない
if ((flag & 7) != 0) return SPI_NO_FUNCTION;
wcscpy(lpInfo->filename, filename);
return GetArchiveInfoCacheW(buf, len, NULL, lpInfo);
} catch(...) {
handle_exception(__FUNCTION__);
return SPI_OTHER_ERROR;
}
}
//---------------------------------------------------------------------------
int __stdcall GetFile(LPSTR src, long len,
LPSTR dest, unsigned int flag,
SPI_PROGRESS lpPrgressCallback, long lData)
{
try {
//メモリ入力には対応しない
if ((flag & 7) != 0) return SPI_NO_FUNCTION;
fileInfo info;
info.filename[0] = '\0';
info.position = len;
int ret = GetArchiveInfoCache(src, 0, NULL, &info);
if (ret != SPI_ALL_RIGHT) {
return ret;
}
int nRet;
if ((flag & 0x700) == 0) {
//ファイルへの出力の場合
std::string s = dest;
s += "\\";
s += info.filename;
nRet = GetFileEx(src, NULL, s.c_str(), &info, lpPrgressCallback, lData);
} else {
// メモリへの出力の場合
nRet = GetFileEx(src, (HLOCAL *)dest, NULL, &info, lpPrgressCallback, lData);
}
return nRet;
} catch(...) {
handle_exception(__FUNCTION__);
return SPI_OTHER_ERROR;
}
}
int __stdcall GetFileW(LPWSTR src, long len,
LPWSTR dest, unsigned int flag,
SPI_PROGRESS lpPrgressCallback, long lData)
{
try {
//メモリ入力には対応しない
if ((flag & 7) != 0) return SPI_NO_FUNCTION;
fileInfoW info;
info.filename[0] = L'\0';
info.position = len;
int ret = GetArchiveInfoCacheW(src, 0, NULL, &info);
if (ret != SPI_ALL_RIGHT) {
return ret;
}
int nRet;
if ((flag & 0x700) == 0) {
//ファイルへの出力の場合
std::wstring s = dest;
s += L"\\";
s += info.filename;
nRet = GetFileWEx(src, NULL, s.c_str(), &info, lpPrgressCallback, lData);
} else {
// メモリへの出力の場合
nRet = GetFileWEx(src, (HLOCAL *)dest, NULL, &info, lpPrgressCallback, lData);
}
return nRet;
} catch(...) {
handle_exception(__FUNCTION__);
return SPI_OTHER_ERROR;
}
}
static std::string Get7zPath(HKEY hkParent)
{
HKEY hkey;
if(RegOpenKey(hkParent, "Software\\7-zip", &hkey) == ERROR_SUCCESS) {
std::vector<BYTE> buf(4096);
DWORD dwSize = buf.size();
if(RegQueryValueEx(hkey, "Path", 0, 0, &buf[0], &dwSize) == ERROR_SUCCESS) {
buf.resize(dwSize ? dwSize - 1 : 0);
return std::string(buf.begin(), buf.end());
}
}
return std::string();
}
static HINSTANCE LoadLibrary7z()
{
HINSTANCE h = LoadLibrary("7z.dll");
if(h) return h;
std::string s = Get7zPath(HKEY_CURRENT_USER) + "7z.dll";
h = LoadLibrary(s.c_str());
if(h) return h;
s = Get7zPath(HKEY_LOCAL_MACHINE) + "7z.dll";
h = LoadLibrary(s.c_str());
return h;
}
static std::string Find7zPath()
{
HINSTANCE h = LoadLibrary7z();
if(!h) return std::string();
std::string s(4096, '\0');
DWORD dwLen = GetModuleFileName(h, &s[0], s.size());
s.resize(dwLen);
FreeLibrary(h);
return s;
}
//---------------------------------------------------------------------------
LRESULT CALLBACK AboutDlgProc(HWND hDlgWnd, UINT msg, WPARAM wp, LPARAM lp)
{
try {
switch (msg) {
case WM_INITDIALOG:
{
OutputDebugPrintf("AboutDlgProc: 7z DLL path %s", Find7zPath().c_str());
yak::util::windows::VersionResource vr(Find7zPath().c_str());
std::string s = vr.GetValue(yak::util::windows::VersionResource::FILE_VERSION);
OutputDebugPrintf("AboutDlgProc: 7z DLL version %s", s.c_str());
SendDlgItemMessage(hDlgWnd, IDC_7ZVERSION, WM_SETTEXT, 0, (LPARAM)s.c_str());
return FALSE;
}
case WM_COMMAND:
switch (LOWORD(wp)) {
case IDOK:
EndDialog(hDlgWnd, IDOK);
break;
case IDCANCEL:
EndDialog(hDlgWnd, IDCANCEL);
break;
default:
return FALSE;
}
default:
return FALSE;
}
return TRUE;
} catch(...) {
handle_exception(__FUNCTION__);
return SPI_OTHER_ERROR;
}
}
//---------------------------------------------------------------------------
static void UpdateSolidDialogItem(HWND hDlgWnd)
{
SendDlgItemMessage(hDlgWnd, IDC_SOLID_7Z_CHECK, BM_SETCHECK, (WPARAM)g_nSolidEnable7z, 0L);
SendDlgItemMessage(hDlgWnd, IDC_SOLID_RAR_CHECK, BM_SETCHECK, (WPARAM)g_nSolidEnableRar, 0L);
SolidCache& sc = SolidCache::GetInstance();
char buf[2048];
wsprintf(buf, "%d", sc.GetMaxLookAhead());
SendDlgItemMessage(hDlgWnd, IDC_MAX_LOOKAHEAD_EDIT, WM_SETTEXT, 0, (LPARAM)buf);
wsprintf(buf, "%d", sc.GetMaxMemory());
SendDlgItemMessage(hDlgWnd, IDC_MAX_MEMORY_EDIT, WM_SETTEXT, 0, (LPARAM)buf);
wsprintf(buf, "%d", sc.GetMaxDisk());
SendDlgItemMessage(hDlgWnd, IDC_MAX_DISK_EDIT, WM_SETTEXT, 0, (LPARAM)buf);
wsprintf(buf, "%d", sc.GetPurgeMemory());
SendDlgItemMessage(hDlgWnd, IDC_PURGE_MEMORY_EDIT, WM_SETTEXT, 0, (LPARAM)buf);
wsprintf(buf, "%d", sc.GetPurgeDisk());
SendDlgItemMessage(hDlgWnd, IDC_PURGE_DISK_EDIT, WM_SETTEXT, 0, (LPARAM)buf);
SendDlgItemMessage(hDlgWnd, IDC_CACHE_FOLDER_EDIT, WM_SETTEXT, 0, (LPARAM)sc.GetCacheFolder().c_str());
}
static bool IsChecked2(HWND hDlgWnd, int nID, int &nVar)
{
int nOldVar = nVar;
if (IsDlgButtonChecked(hDlgWnd, nID) == BST_CHECKED) {
nVar = 1;
} else {
nVar = 0;
}
return (nVar != nOldVar);
}
static bool GetIntValue(HWND hDlgWnd, int nID, int (SolidCache::*mfGetter)() const, int (SolidCache::*mfSetter)(int))
{
char buf[2048];
SendDlgItemMessage(hDlgWnd, nID, WM_GETTEXT, sizeof(buf), (LPARAM)buf);
int nTempVar = atoi(buf);
if(nTempVar != (SolidCache::GetInstance().*mfGetter)()) {
(SolidCache::GetInstance().*mfSetter)(nTempVar);
return true;
}
return false;
}
static bool UpdateSolidValue(HWND hDlgWnd)
{
bool fChanged = false;
fChanged |= IsChecked2(hDlgWnd, IDC_SOLID_7Z_CHECK, g_nSolidEnable7z);
fChanged |= IsChecked2(hDlgWnd, IDC_SOLID_RAR_CHECK, g_nSolidEnableRar);
fChanged |= GetIntValue(hDlgWnd, IDC_MAX_LOOKAHEAD_EDIT, &SolidCache::GetMaxLookAhead, &SolidCache::SetMaxLookAhead);
fChanged |= GetIntValue(hDlgWnd, IDC_MAX_MEMORY_EDIT, &SolidCache::GetMaxMemory, &SolidCache::SetMaxMemory);
fChanged |= GetIntValue(hDlgWnd, IDC_MAX_DISK_EDIT, &SolidCache::GetMaxDisk, &SolidCache::SetMaxDisk);
fChanged |= GetIntValue(hDlgWnd, IDC_PURGE_MEMORY_EDIT, &SolidCache::GetPurgeMemory, &SolidCache::SetPurgeMemory);
fChanged |= GetIntValue(hDlgWnd, IDC_PURGE_DISK_EDIT, &SolidCache::GetPurgeDisk, &SolidCache::SetPurgeDisk);
char buf[2048];
SendDlgItemMessage(hDlgWnd, IDC_CACHE_FOLDER_EDIT, WM_GETTEXT, sizeof(buf), (LPARAM)buf);
if(lstrcmp(buf, SolidCache::GetInstance().GetCacheFolder().c_str())) {
fChanged = true;
SolidCache::GetInstance().SetCacheFolder(buf);
}
return fChanged;
}
static bool IsExistentFolder(HWND hwnd, const std::string &sPath)
{
DWORD dwAttr = GetFileAttributes(sPath.c_str());
if(dwAttr == -1) {
std::string sText = "Directory: " + sPath + " is not found, create?";
if(MessageBox(hwnd, sText.c_str(), ExtManager::SECTION_NAME, MB_YESNO) == IDYES) {
if(NWindows::NFile::NDirectory::CreateComplexDirectory(sPath.c_str())) {
return true;
} else {
std::string sText = "Directory: " + sPath + " can't be created.";
MessageBox(hwnd, sText.c_str(), ExtManager::SECTION_NAME, MB_ICONERROR | MB_OK);
}
}
} else {
if(dwAttr & FILE_ATTRIBUTE_DIRECTORY) {
return true;
} else {
std::string sText = "Path: " + sPath + " already exists and is not a directory.";
MessageBox(hwnd, sText.c_str(), ExtManager::SECTION_NAME, MB_ICONERROR | MB_OK);
}
}
return false;
}
LRESULT CALLBACK SolidConfigDlgProc(HWND hDlgWnd, UINT msg, WPARAM wp, LPARAM lp)
{
switch (msg) {
case WM_INITDIALOG:
UpdateSolidDialogItem(hDlgWnd);
EnableWindow(GetDlgItem(hDlgWnd, IDC_MAX_LOOKAHEAD_EDIT), FALSE);
return FALSE;
case WM_COMMAND:
switch (LOWORD(wp)) {
case IDOK:
{
char buf[2048];
SendDlgItemMessage(hDlgWnd, IDC_CACHE_FOLDER_EDIT, WM_GETTEXT, sizeof(buf), (LPARAM)buf);
if (IsExistentFolder(hDlgWnd, buf)) {
if (UpdateSolidValue(hDlgWnd)) {
SaveToIni();
}
EndDialog(hDlgWnd, IDOK);
}
break;
}
case IDCANCEL:
EndDialog(hDlgWnd, IDCANCEL);
break;
case IDC_BROWSE_BUTTON:
{
char buf[MAX_PATH];
HRESULT hr = CoInitialize(NULL);
BROWSEINFO bwi = { hDlgWnd, NULL, buf, "Specify cache folder", BIF_RETURNONLYFSDIRS };
LPITEMIDLIST piid = SHBrowseForFolder(&bwi);
if(piid) {
SHGetPathFromIDList(piid, buf);
SendDlgItemMessage(hDlgWnd, IDC_CACHE_FOLDER_EDIT, WM_SETTEXT, 0, (LPARAM)buf);
CoTaskMemFree(piid);
}
if(SUCCEEDED(hr)) CoUninitialize();
}
break;
case IDC_CLEAR_BUTTON:
if(MessageBox(hDlgWnd, "Do you want to clear cache contents?", "ax7z_s.spi confirmation", MB_YESNO) == IDYES)
SolidCache::GetInstance().Clear();
break;
default:
return FALSE;
}
}
return FALSE;
}
//---------------------------------------------------------------------------
struct MyConcat3
{
std::string operator()(const std::string &s1, const std::string &s2) const
{
if(s1 == "") return s2;
return s1 + ", " + s2;
}
};
struct ListUpdater
{
ListUpdater(HWND hwnd, std::vector<std::string> &v) : hwnd(hwnd), v(v) {}
HWND hwnd;
std::vector<std::string> &v;
void operator()(const ExtManager::EachValueType& value)
{