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
|
// LoadCodecs.cpp
#include "StdAfx.h"
#include "LoadCodecs.h"
#include "../../../Common/MyCom.h"
#ifdef NEW_FOLDER_INTERFACE
#include "../../../Common/StringToInt.h"
#endif
#include "../../../Windows/PropVariant.h"
#include "../../ICoder.h"
#include "../../Common/RegisterArc.h"
#ifdef EXTERNAL_CODECS
#include "../../../Windows/FileFind.h"
#include "../../../Windows/DLL.h"
#ifdef NEW_FOLDER_INTERFACE
#include "../../../Windows/ResourceString.h"
static const UINT kIconTypesResId = 100;
#endif
#ifdef _WIN32
#include "Windows/Registry.h"
#endif
using namespace NWindows;
using namespace NFile;
extern HINSTANCE g_hInstance;
static CSysString GetLibraryFolderPrefix()
{
TCHAR fullPath[MAX_PATH + 1];
::GetModuleFileName(g_hInstance, fullPath, MAX_PATH);
CSysString path = fullPath;
int pos = path.ReverseFind(TEXT(CHAR_PATH_SEPARATOR));
return path.Left(pos + 1);
}
#define kCodecsFolderName TEXT("Codecs")
#define kFormatsFolderName TEXT("Formats")
static TCHAR *kMainDll = TEXT("7z.dll");
#ifdef _WIN32
static LPCTSTR kRegistryPath = TEXT("Software\\7-zip");
static LPCTSTR kProgramPathValue = TEXT("Path");
static bool ReadPathFromRegistry(HKEY baseKey, CSysString &path)
{
NRegistry::CKey key;
if(key.Open(baseKey, kRegistryPath, KEY_READ) == ERROR_SUCCESS)
if (key.QueryValue(kProgramPathValue, path) == ERROR_SUCCESS)
{
NName::NormalizeDirPathPrefix(path);
return true;
}
return false;
}
CSysString GetBaseFolderPrefixFromRegistry()
{
CSysString moduleFolderPrefix = GetLibraryFolderPrefix();
NFind::CFileInfo fileInfo;
if (NFind::FindFile(moduleFolderPrefix + kMainDll, fileInfo))
if (!fileInfo.IsDirectory())
return moduleFolderPrefix;
if (NFind::FindFile(moduleFolderPrefix + kCodecsFolderName, fileInfo))
if (fileInfo.IsDirectory())
return moduleFolderPrefix;
if (NFind::FindFile(moduleFolderPrefix + kFormatsFolderName, fileInfo))
if (fileInfo.IsDirectory())
return moduleFolderPrefix;
CSysString path;
#ifdef _WIN32
if (ReadPathFromRegistry(HKEY_CURRENT_USER, path))
return path;
if (ReadPathFromRegistry(HKEY_LOCAL_MACHINE, path))
return path;
#endif
return moduleFolderPrefix;
}
#endif
typedef UInt32 (WINAPI *GetNumberOfMethodsFunc)(UInt32 *numMethods);
typedef UInt32 (WINAPI *GetNumberOfFormatsFunc)(UInt32 *numFormats);
typedef UInt32 (WINAPI *GetHandlerPropertyFunc)(PROPID propID, PROPVARIANT *value);
typedef UInt32 (WINAPI *GetHandlerPropertyFunc2)(UInt32 index, PROPID propID, PROPVARIANT *value);
typedef UINT32 (WINAPI *CreateObjectFunc)(const GUID *clsID, const GUID *iid, void **outObject);
static HRESULT GetCoderClass(GetMethodPropertyFunc getMethodProperty, UInt32 index,
PROPID propId, CLSID &clsId, bool &isAssigned)
{
NWindows::NCOM::CPropVariant prop;
isAssigned = false;
RINOK(getMethodProperty(index, propId, &prop));
if (prop.vt == VT_BSTR)
{
isAssigned = true;
clsId = *(const GUID *)prop.bstrVal;
}
else if (prop.vt != VT_EMPTY)
return E_FAIL;
return S_OK;
}
HRESULT CCodecs::LoadCodecs()
{
CCodecLib &lib = Libs.Back();
lib.GetMethodProperty = (GetMethodPropertyFunc)lib.Lib.GetProcAddress("GetMethodProperty");
if (lib.GetMethodProperty == NULL)
return S_OK;
UInt32 numMethods = 1;
GetNumberOfMethodsFunc getNumberOfMethodsFunc = (GetNumberOfMethodsFunc)lib.Lib.GetProcAddress("GetNumberOfMethods");
if (getNumberOfMethodsFunc != NULL)
{
RINOK(getNumberOfMethodsFunc(&numMethods));
}
for(UInt32 i = 0; i < numMethods; i++)
{
CDllCodecInfo info;
info.LibIndex = Libs.Size() - 1;
info.CodecIndex = i;
RINOK(GetCoderClass(lib.GetMethodProperty, i, NMethodPropID::kEncoder, info.Encoder, info.EncoderIsAssigned));
RINOK(GetCoderClass(lib.GetMethodProperty, i, NMethodPropID::kDecoder, info.Decoder, info.DecoderIsAssigned));
Codecs.Add(info);
}
return S_OK;
}
static HRESULT ReadProp(
GetHandlerPropertyFunc getProp,
GetHandlerPropertyFunc2 getProp2,
UInt32 index, PROPID propID, NCOM::CPropVariant &prop)
{
if (getProp2)
return getProp2(index, propID, &prop);;
return getProp(propID, &prop);
}
static HRESULT ReadBoolProp(
GetHandlerPropertyFunc getProp,
GetHandlerPropertyFunc2 getProp2,
UInt32 index, PROPID propID, bool &res)
{
NCOM::CPropVariant prop;
RINOK(ReadProp(getProp, getProp2, index, propID, prop));
if (prop.vt == VT_BOOL)
res = VARIANT_BOOLToBool(prop.boolVal);
else if (prop.vt != VT_EMPTY)
return E_FAIL;
return S_OK;
}
static HRESULT ReadStringProp(
GetHandlerPropertyFunc getProp,
GetHandlerPropertyFunc2 getProp2,
UInt32 index, PROPID propID, UString &res)
{
NCOM::CPropVariant prop;
RINOK(ReadProp(getProp, getProp2, index, propID, prop));
if (prop.vt == VT_BSTR)
res = prop.bstrVal;
else if (prop.vt != VT_EMPTY)
return E_FAIL;
return S_OK;
}
#endif
static const unsigned int kNumArcsMax = 32;
static unsigned int g_NumArcs = 0;
static const CArcInfo *g_Arcs[kNumArcsMax];
void RegisterArc(const CArcInfo *arcInfo)
{
if (g_NumArcs < kNumArcsMax)
g_Arcs[g_NumArcs++] = arcInfo;
}
static void SplitString(const UString &srcString, UStringVector &destStrings)
{
destStrings.Clear();
UString s;
int len = srcString.Length();
if (len == 0)
return;
for (int i = 0; i < len; i++)
{
wchar_t c = srcString[i];
if (c == L' ')
{
if (!s.IsEmpty())
{
destStrings.Add(s);
s.Empty();
}
}
else
s += c;
}
if (!s.IsEmpty())
destStrings.Add(s);
}
void CArcInfoEx::AddExts(const wchar_t* ext, const wchar_t* addExt)
{
UStringVector exts, addExts;
SplitString(ext, exts);
if (addExt != 0)
SplitString(addExt, addExts);
for (int i = 0; i < exts.Size(); i++)
{
CArcExtInfo extInfo;
extInfo.Ext = exts[i];
if (i < addExts.Size())
{
extInfo.AddExt = addExts[i];
if (extInfo.AddExt == L"*")
extInfo.AddExt.Empty();
}
Exts.Add(extInfo);
}
}
#ifdef EXTERNAL_CODECS
HRESULT CCodecs::LoadFormats()
{
const NDLL::CLibrary &lib = Libs.Back().Lib;
GetHandlerPropertyFunc getProp = 0;
GetHandlerPropertyFunc2 getProp2 = (GetHandlerPropertyFunc2)
lib.GetProcAddress("GetHandlerProperty2");
if (getProp2 == NULL)
{
getProp = (GetHandlerPropertyFunc)
lib.GetProcAddress("GetHandlerProperty");
if (getProp == NULL)
return S_OK;
}
UInt32 numFormats = 1;
GetNumberOfFormatsFunc getNumberOfFormats = (GetNumberOfFormatsFunc)
lib.GetProcAddress("GetNumberOfFormats");
if (getNumberOfFormats != NULL)
{
RINOK(getNumberOfFormats(&numFormats));
}
if (getProp2 == NULL)
numFormats = 1;
for(UInt32 i = 0; i < numFormats; i++)
{
CArcInfoEx item;
item.LibIndex = Libs.Size() - 1;
item.FormatIndex = i;
RINOK(ReadStringProp(getProp, getProp2, i, NArchive::kName, item.Name));
NCOM::CPropVariant prop;
if (ReadProp(getProp, getProp2, i, NArchive::kClassID, prop) != S_OK)
continue;
if (prop.vt != VT_BSTR)
continue;
item.ClassID = *(const GUID *)prop.bstrVal;
prop.Clear();
UString ext, addExt;
RINOK(ReadStringProp(getProp, getProp2, i, NArchive::kExtension, ext));
RINOK(ReadStringProp(getProp, getProp2, i, NArchive::kAddExtension, addExt));
item.AddExts(ext, addExt);
ReadBoolProp(getProp, getProp2, i, NArchive::kUpdate, item.UpdateEnabled);
if (item.UpdateEnabled)
ReadBoolProp(getProp, getProp2, i, NArchive::kKeepName, item.KeepName);
if (ReadProp(getProp, getProp2, i, NArchive::kStartSignature, prop) == S_OK)
if (prop.vt == VT_BSTR)
{
UINT len = ::SysStringByteLen(prop.bstrVal);
item.StartSignature.SetCapacity(len);
memmove(item.StartSignature, prop.bstrVal, len);
}
Formats.Add(item);
}
return S_OK;
}
#ifdef NEW_FOLDER_INTERFACE
void CCodecLib::LoadIcons()
{
UString iconTypes = MyLoadStringW((HMODULE)Lib, kIconTypesResId);
UStringVector pairs;
SplitString(iconTypes, pairs);
for (int i = 0; i < pairs.Size(); i++)
{
const UString &s = pairs[i];
int pos = s.Find(L':');
if (pos < 0)
continue;
CIconPair iconPair;
const wchar_t *end;
UString num = s.Mid(pos + 1);
iconPair.IconIndex = (UInt32)ConvertStringToUInt64(num, &end);
if (*end != L'\0')
continue;
iconPair.Ext = s.Left(pos);
IconPairs.Add(iconPair);
}
}
int CCodecLib::FindIconIndex(const UString &ext) const
{
for (int i = 0; i < IconPairs.Size(); i++)
{
const CIconPair &pair = IconPairs[i];
if (ext.CompareNoCase(pair.Ext) == 0)
return pair.IconIndex;
}
return -1;
}
#endif
HRESULT CCodecs::LoadDll(const CSysString &dllPath)
{
{
NDLL::CLibrary library;
if (!library.LoadEx(dllPath, LOAD_LIBRARY_AS_DATAFILE))
return S_OK;
}
Libs.Add(CCodecLib());
CCodecLib &lib = Libs.Back();
#ifdef NEW_FOLDER_INTERFACE
lib.Path = dllPath;
#endif
bool used = false;
HRESULT res = S_OK;
if (lib.Lib.Load(dllPath))
{
#ifdef NEW_FOLDER_INTERFACE
lib.LoadIcons();
#endif
lib.CreateObject = (CreateObjectFunc)lib.Lib.GetProcAddress("CreateObject");
if (lib.CreateObject != 0)
{
int startSize = Codecs.Size();
res = LoadCodecs();
used = (Codecs.Size() != startSize);
if (res == S_OK)
{
startSize = Formats.Size();
res = LoadFormats();
used = used || (Formats.Size() != startSize);
}
}
}
if (!used)
Libs.DeleteBack();
return res;
}
HRESULT CCodecs::LoadDllsFromFolder(const CSysString &folderPrefix)
{
NFile::NFind::CEnumerator enumerator(folderPrefix + CSysString(TEXT("*")));
NFile::NFind::CFileInfo fileInfo;
while (enumerator.Next(fileInfo))
{
if (fileInfo.IsDirectory())
continue;
RINOK(LoadDll(folderPrefix + fileInfo.Name));
}
return S_OK;
}
#endif
#ifndef _SFX
static inline void SetBuffer(CByteBuffer &bb, const Byte *data, int size)
{
bb.SetCapacity(size);
memmove((Byte *)bb, data, size);
}
#endif
HRESULT CCodecs::Load()
{
Formats.Clear();
#ifdef EXTERNAL_CODECS
Codecs.Clear();
#endif
for (UInt32 i = 0; i < g_NumArcs; i++)
{
const CArcInfo &arc = *g_Arcs[i];
CArcInfoEx item;
item.Name = arc.Name;
item.CreateInArchive = arc.CreateInArchive;
item.CreateOutArchive = arc.CreateOutArchive;
item.AddExts(arc.Ext, arc.AddExt);
item.UpdateEnabled = (arc.CreateOutArchive != 0);
item.KeepName = arc.KeepName;
#ifndef _SFX
SetBuffer(item.StartSignature, arc.Signature, arc.SignatureSize);
#endif
Formats.Add(item);
}
#ifdef EXTERNAL_CODECS
const CSysString baseFolder = GetBaseFolderPrefixFromRegistry();
RINOK(LoadDll(baseFolder + kMainDll));
RINOK(LoadDllsFromFolder(baseFolder + kCodecsFolderName TEXT(STRING_PATH_SEPARATOR)));
RINOK(LoadDllsFromFolder(baseFolder + kFormatsFolderName TEXT(STRING_PATH_SEPARATOR)));
#endif
return S_OK;
}
int CCodecs::FindFormatForArchiveName(const UString &archivePath) const
{
int slashPos1 = archivePath.ReverseFind(L'\\');
int slashPos2 = archivePath.ReverseFind(L'.');
int dotPos = archivePath.ReverseFind(L'.');
if (dotPos < 0 || dotPos < slashPos1 || dotPos < slashPos2)
return -1;
UString ext = archivePath.Mid(dotPos + 1);
for (int i = 0; i < Formats.Size(); i++)
{
const CArcInfoEx &arc = Formats[i];
if (!arc.UpdateEnabled)
continue;
// if (arc.FindExtension(ext) >= 0)
UString mainExt = arc.GetMainExt();
if (!mainExt.IsEmpty() && ext.CompareNoCase(mainExt) == 0)
return i;
}
return -1;
}
int CCodecs::FindFormatForArchiveType(const UString &arcType) const
{
for (int i = 0; i < Formats.Size(); i++)
{
const CArcInfoEx &arc = Formats[i];
if (!arc.UpdateEnabled)
continue;
if (arc.Name.CompareNoCase(arcType) == 0)
return i;
}
return -1;
}
#ifdef EXTERNAL_CODECS
#ifdef EXPORT_CODECS
extern unsigned int g_NumCodecs;
STDAPI CreateCoder2(bool encode, UInt32 index, const GUID *iid, void **outObject);
STDAPI GetMethodProperty(UInt32 codecIndex, PROPID propID, PROPVARIANT *value);
// STDAPI GetNumberOfMethods(UINT32 *numCodecs);
#endif
STDMETHODIMP CCodecs::GetNumberOfMethods(UINT32 *numMethods)
{
*numMethods =
#ifdef EXPORT_CODECS
g_NumCodecs +
#endif
Codecs.Size();
return S_OK;
}
STDMETHODIMP CCodecs::GetProperty(UINT32 index, PROPID propID, PROPVARIANT *value)
{
#ifdef EXPORT_CODECS
if (index < g_NumCodecs)
return GetMethodProperty(index, propID, value);
#endif
const CDllCodecInfo &ci = Codecs[index
#ifdef EXPORT_CODECS
- g_NumCodecs
#endif
];
if (propID == NMethodPropID::kDecoderIsAssigned)
{
NWindows::NCOM::CPropVariant propVariant;
propVariant = ci.DecoderIsAssigned;
propVariant.Detach(value);
return S_OK;
}
if (propID == NMethodPropID::kEncoderIsAssigned)
{
NWindows::NCOM::CPropVariant propVariant;
propVariant = ci.EncoderIsAssigned;
propVariant.Detach(value);
return S_OK;
}
return Libs[ci.LibIndex].GetMethodProperty(ci.CodecIndex, propID, value);
}
STDMETHODIMP CCodecs::CreateDecoder(UINT32 index, const GUID *iid, void **coder)
{
#ifdef EXPORT_CODECS
if (index < g_NumCodecs)
return CreateCoder2(false, index, iid, coder);
#endif
const CDllCodecInfo &ci = Codecs[index
#ifdef EXPORT_CODECS
- g_NumCodecs
#endif
];
if (ci.DecoderIsAssigned)
return Libs[ci.LibIndex].CreateObject(&ci.Decoder, iid, (void **)coder);
return S_OK;
}
STDMETHODIMP CCodecs::CreateEncoder(UINT32 index, const GUID *iid, void **coder)
{
#ifdef EXPORT_CODECS
if (index < g_NumCodecs)
return CreateCoder2(true, index, iid, coder);
#endif
const CDllCodecInfo &ci = Codecs[index
#ifdef EXPORT_CODECS
- g_NumCodecs
#endif
];
if (ci.EncoderIsAssigned)
return Libs[ci.LibIndex].CreateObject(&ci.Encoder, iid, (void **)coder);
return S_OK;
}
HRESULT CCodecs::CreateCoder(const UString &name, bool encode, CMyComPtr<ICompressCoder> &coder) const
{
for (int i = 0; i < Codecs.Size(); i++)
{
const CDllCodecInfo &codec = Codecs[i];
if (encode && !codec.EncoderIsAssigned || !encode && !codec.DecoderIsAssigned)
continue;
const CCodecLib &lib = Libs[codec.LibIndex];
UString res;
NWindows::NCOM::CPropVariant prop;
RINOK(lib.GetMethodProperty(codec.CodecIndex, NMethodPropID::kName, &prop));
if (prop.vt == VT_BSTR)
res = prop.bstrVal;
else if (prop.vt != VT_EMPTY)
continue;
if (name.CompareNoCase(res) == 0)
return lib.CreateObject(encode ? &codec.Encoder : &codec.Decoder, &IID_ICompressCoder, (void **)&coder);
}
return CLASS_E_CLASSNOTAVAILABLE;
}
int CCodecs::GetCodecLibIndex(UInt32 index)
{
#ifdef EXPORT_CODECS
if (index < g_NumCodecs)
return -1;
#endif
#ifdef EXTERNAL_CODECS
const CDllCodecInfo &ci = Codecs[index
#ifdef EXPORT_CODECS
- g_NumCodecs
#endif
];
return ci.LibIndex;
#else
return -1;
#endif
}
bool CCodecs::GetCodecEncoderIsAssigned(UInt32 index)
{
#ifdef EXPORT_CODECS
if (index < g_NumCodecs)
{
NWindows::NCOM::CPropVariant prop;
if (GetProperty(index, NMethodPropID::kEncoder, &prop) == S_OK)
if (prop.vt != VT_EMPTY)
return true;
return false;
}
#endif
#ifdef EXTERNAL_CODECS
const CDllCodecInfo &ci = Codecs[index
#ifdef EXPORT_CODECS
- g_NumCodecs
#endif
];
return ci.EncoderIsAssigned;
#else
return false;
#endif
}
HRESULT CCodecs::GetCodecId(UInt32 index, UInt64 &id)
{
UString s;
NWindows::NCOM::CPropVariant prop;
RINOK(GetProperty(index, NMethodPropID::kID, &prop));
if (prop.vt != VT_UI8)
return E_INVALIDARG;
id = prop.uhVal.QuadPart;
return S_OK;
}
UString CCodecs::GetCodecName(UInt32 index)
{
UString s;
NWindows::NCOM::CPropVariant prop;
if (GetProperty(index, NMethodPropID::kName, &prop) == S_OK)
if (prop.vt == VT_BSTR)
s = prop.bstrVal;
return s;
}
#endif
|