other-licenses/7zstub/src/7zip/UI/Common/ArchiverInfo.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/other-licenses/7zstub/src/7zip/UI/Common/ArchiverInfo.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,359 @@
     1.4 +// ArchiverInfo.cpp
     1.5 +
     1.6 +#include "StdAfx.h"
     1.7 +
     1.8 +#include "ArchiverInfo.h"
     1.9 +
    1.10 +#ifndef EXCLUDE_COM
    1.11 +
    1.12 +#include "Common/StringConvert.h"
    1.13 +#include "Windows/FileFind.h"
    1.14 +#include "Windows/FileName.h"
    1.15 +#include "Windows/DLL.h"
    1.16 +#ifdef _WIN32
    1.17 +#include "Windows/Registry.h"
    1.18 +#endif
    1.19 +#include "Windows/PropVariant.h"
    1.20 +#include "../../Archive/IArchive.h"
    1.21 +
    1.22 +using namespace NWindows;
    1.23 +using namespace NFile;
    1.24 +
    1.25 +#endif
    1.26 +
    1.27 +extern HINSTANCE g_hInstance;
    1.28 +
    1.29 +#ifndef EXCLUDE_COM
    1.30 +
    1.31 +static void SplitString(const UString &srcString, UStringVector &destStrings)
    1.32 +{
    1.33 +  destStrings.Clear();
    1.34 +  UString string;
    1.35 +  int len = srcString.Length();
    1.36 +  if (len == 0)
    1.37 +    return;
    1.38 +  for (int i = 0; i < len; i++)
    1.39 +  {
    1.40 +    wchar_t c = srcString[i];
    1.41 +    if (c == L' ')
    1.42 +    {
    1.43 +      if (!string.IsEmpty())
    1.44 +      {
    1.45 +        destStrings.Add(string);
    1.46 +        string.Empty();
    1.47 +      }
    1.48 +    }
    1.49 +    else
    1.50 +      string += c;
    1.51 +  }
    1.52 +  if (!string.IsEmpty())
    1.53 +    destStrings.Add(string);
    1.54 +}
    1.55 +
    1.56 +typedef UInt32 (WINAPI * GetHandlerPropertyFunc)(
    1.57 +    PROPID propID, PROPVARIANT *value);
    1.58 +
    1.59 +static UString GetModuleFolderPrefix()
    1.60 +{
    1.61 +  UString path;
    1.62 +  NDLL::MyGetModuleFileName(g_hInstance, path);
    1.63 +  int pos = path.ReverseFind(WCHAR_PATH_SEPARATOR);
    1.64 +  return path.Left(pos + 1);
    1.65 +}
    1.66 +
    1.67 +static wchar_t *kFormatFolderName = L"Formats";
    1.68 +
    1.69 +#ifdef _WIN32
    1.70 +static LPCTSTR kRegistryPath = TEXT("Software\\7-zip");
    1.71 +static LPCWSTR kProgramPathValue = L"Path";
    1.72 +static bool ReadPathFromRegistry(HKEY baseKey, UString &path)
    1.73 +{
    1.74 +  NRegistry::CKey key;
    1.75 +  if(key.Open(baseKey, kRegistryPath, KEY_READ) == ERROR_SUCCESS)
    1.76 +    if (key.QueryValue(kProgramPathValue, path) == ERROR_SUCCESS)
    1.77 +    {
    1.78 +      NName::NormalizeDirPathPrefix(path);
    1.79 +      return true;
    1.80 +    }
    1.81 +  return false;
    1.82 +}
    1.83 +#endif
    1.84 +
    1.85 +static UString GetBaseFolderPrefixFromRegistry()
    1.86 +{
    1.87 +  UString moduleFolderPrefix = GetModuleFolderPrefix();
    1.88 +  NFind::CFileInfoW fileInfo;
    1.89 +  if (NFind::FindFile(moduleFolderPrefix + kFormatFolderName, fileInfo))
    1.90 +    if (fileInfo.IsDirectory())
    1.91 +      return moduleFolderPrefix;
    1.92 +  UString path;
    1.93 +  #ifdef _WIN32
    1.94 +  if(ReadPathFromRegistry(HKEY_CURRENT_USER, path))
    1.95 +    return path;
    1.96 +  if(ReadPathFromRegistry(HKEY_LOCAL_MACHINE, path))
    1.97 +    return path;
    1.98 +  #endif
    1.99 +  return moduleFolderPrefix;
   1.100 +}
   1.101 +
   1.102 +typedef UInt32 (WINAPI *CreateObjectPointer)(
   1.103 +    const GUID *clsID, 
   1.104 +    const GUID *interfaceID, 
   1.105 +    void **outObject);
   1.106 +
   1.107 +#endif
   1.108 +
   1.109 +#ifndef _SFX
   1.110 +static void SetBuffer(CByteBuffer &bb, const Byte *data, int size)
   1.111 +{
   1.112 +  bb.SetCapacity(size);
   1.113 +  memmove((Byte *)bb, data, size);
   1.114 +}
   1.115 +#endif
   1.116 +
   1.117 +void ReadArchiverInfoList(CObjectVector<CArchiverInfo> &archivers)
   1.118 +{
   1.119 +  archivers.Clear();
   1.120 +  
   1.121 +  #ifdef EXCLUDE_COM
   1.122 +  
   1.123 +  #ifdef FORMAT_7Z
   1.124 +  {
   1.125 +    CArchiverInfo item;
   1.126 +    item.UpdateEnabled = true;
   1.127 +    item.Name = L"7z";
   1.128 +    item.Extensions.Add(CArchiverExtInfo(L"7z"));
   1.129 +    #ifndef _SFX
   1.130 +    const unsigned char kSig[] = {'7' , 'z', 0xBC, 0xAF, 0x27, 0x1C};
   1.131 +    SetBuffer(item.StartSignature, kSig, 6);
   1.132 +    #endif
   1.133 +    archivers.Add(item);
   1.134 +  }
   1.135 +  #endif
   1.136 +
   1.137 +  #ifdef FORMAT_BZIP2
   1.138 +  {
   1.139 +    CArchiverInfo item;
   1.140 +    item.UpdateEnabled = true;
   1.141 +    item.KeepName = true;
   1.142 +    item.Name = L"BZip2";
   1.143 +    item.Extensions.Add(CArchiverExtInfo(L"bz2"));
   1.144 +    item.Extensions.Add(CArchiverExtInfo(L"tbz2", L".tar"));
   1.145 +    #ifndef _SFX
   1.146 +    const unsigned char sig[] = {'B' , 'Z', 'h' };
   1.147 +    SetBuffer(item.StartSignature, sig, 3);
   1.148 +    #endif
   1.149 +    archivers.Add(item);
   1.150 +  }
   1.151 +  #endif
   1.152 +
   1.153 +  #ifdef FORMAT_GZIP
   1.154 +  {
   1.155 +    CArchiverInfo item;
   1.156 +    item.UpdateEnabled = true;
   1.157 +    item.Name = L"GZip";
   1.158 +    item.Extensions.Add(CArchiverExtInfo(L"gz"));
   1.159 +    item.Extensions.Add(CArchiverExtInfo(L"tgz", L".tar"));
   1.160 +    #ifndef _SFX
   1.161 +    const unsigned char sig[] = { 0x1F, 0x8B };
   1.162 +    SetBuffer(item.StartSignature, sig, 2);
   1.163 +    #endif
   1.164 +    archivers.Add(item);
   1.165 +  }
   1.166 +  #endif
   1.167 +
   1.168 +  #ifdef FORMAT_SPLIT
   1.169 +  {
   1.170 +    CArchiverInfo item;
   1.171 +    item.UpdateEnabled = false;
   1.172 +    item.KeepName = true;
   1.173 +    item.Name = L"Split";
   1.174 +    item.Extensions.Add(CArchiverExtInfo(L"001"));
   1.175 +    archivers.Add(item);
   1.176 +  }
   1.177 +  #endif
   1.178 +
   1.179 +  #ifdef FORMAT_TAR
   1.180 +  {
   1.181 +    CArchiverInfo item;
   1.182 +    item.UpdateEnabled = true;
   1.183 +    item.Name = L"Tar";
   1.184 +    item.Extensions.Add(CArchiverExtInfo(L"tar"));
   1.185 +    archivers.Add(item);
   1.186 +  }
   1.187 +  #endif
   1.188 +
   1.189 +  #ifdef FORMAT_ZIP
   1.190 +  {
   1.191 +    CArchiverInfo item;
   1.192 +    item.UpdateEnabled = true;
   1.193 +    item.Name = L"Zip";
   1.194 +    item.Extensions.Add(CArchiverExtInfo(L"zip"));
   1.195 +    #ifndef _SFX
   1.196 +    const unsigned char sig[] = { 0x50, 0x4B, 0x03, 0x04 };
   1.197 +    SetBuffer(item.StartSignature, sig, 4);
   1.198 +    #endif
   1.199 +    archivers.Add(item);
   1.200 +  }
   1.201 +  #endif
   1.202 +
   1.203 +  #ifdef FORMAT_CPIO
   1.204 +  {
   1.205 +    CArchiverInfo item;
   1.206 +    item.Name = L"Cpio";
   1.207 +    item.Extensions.Add(CArchiverExtInfo(L"cpio"));
   1.208 +    archivers.Add(item);
   1.209 +  }
   1.210 +  #endif
   1.211 +
   1.212 +  #ifdef FORMAT_RPM
   1.213 +  {
   1.214 +    CArchiverInfo item;
   1.215 +    item.Name = L"Rpm";
   1.216 +    item.Extensions.Add(CArchiverExtInfo(L"rpm", L".cpio.gz"));
   1.217 +    archivers.Add(item);
   1.218 +  }
   1.219 +  #endif
   1.220 +
   1.221 +  #ifdef FORMAT_ARJ
   1.222 +  {
   1.223 +    CArchiverInfo item;
   1.224 +    item.Name = L"Arj";
   1.225 +    item.Extensions.Add(CArchiverExtInfo(L"arj"));
   1.226 +    #ifndef _SFX
   1.227 +    const unsigned char sig[] = { 0x60, 0xEA };
   1.228 +    SetBuffer(item.StartSignature, sig, 2);
   1.229 +    #endif
   1.230 +    archivers.Add(item);
   1.231 +  }
   1.232 +  #endif
   1.233 +
   1.234 +  #ifdef FORMAT_Z
   1.235 +  {
   1.236 +    CArchiverInfo item;
   1.237 +    item.Name = L"Z";
   1.238 +    item.Extensions.Add(CArchiverExtInfo(L"Z"));
   1.239 +    #ifndef _SFX
   1.240 +    const unsigned char sig[] = { 0x1F, 0x9D };
   1.241 +    SetBuffer(item.StartSignature, sig, 2);
   1.242 +    #endif
   1.243 +    archivers.Add(item);
   1.244 +  }
   1.245 +  #endif
   1.246 +  
   1.247 +  #else
   1.248 +
   1.249 +  UString folderPath = GetBaseFolderPrefixFromRegistry() + 
   1.250 +      (UString)kFormatFolderName + (UString)WSTRING_PATH_SEPARATOR;
   1.251 +  NFind::CEnumeratorW enumerator(folderPath + L"*");
   1.252 +  NFind::CFileInfoW fileInfo;
   1.253 +  while (enumerator.Next(fileInfo))
   1.254 +  {
   1.255 +    if (fileInfo.IsDirectory())
   1.256 +      continue;
   1.257 +    UString filePath = folderPath + fileInfo.Name;
   1.258 +    {
   1.259 +      NDLL::CLibrary library;
   1.260 +      if (!library.LoadEx(filePath, LOAD_LIBRARY_AS_DATAFILE))
   1.261 +        continue;
   1.262 +    }
   1.263 +
   1.264 +    NDLL::CLibrary library;
   1.265 +    if (!library.Load(filePath))
   1.266 +      continue;
   1.267 +    GetHandlerPropertyFunc getHandlerProperty = (GetHandlerPropertyFunc)
   1.268 +        library.GetProcAddress("GetHandlerProperty");
   1.269 +    if (getHandlerProperty == NULL)
   1.270 +      continue;
   1.271 +
   1.272 +    CArchiverInfo item;
   1.273 +    item.FilePath = filePath;
   1.274 +    
   1.275 +    NWindows::NCOM::CPropVariant prop;
   1.276 +    if (getHandlerProperty(NArchive::kName, &prop) != S_OK)
   1.277 +      continue;
   1.278 +    if (prop.vt != VT_BSTR)
   1.279 +      continue;
   1.280 +    item.Name = prop.bstrVal;
   1.281 +    prop.Clear();
   1.282 +
   1.283 +    if (getHandlerProperty(NArchive::kClassID, &prop) != S_OK)
   1.284 +      continue;
   1.285 +    if (prop.vt != VT_BSTR)
   1.286 +      continue;
   1.287 +    item.ClassID = *(const GUID *)prop.bstrVal;
   1.288 +    prop.Clear();
   1.289 +
   1.290 +    if (getHandlerProperty(NArchive::kExtension, &prop) != S_OK)
   1.291 +      continue;
   1.292 +    if (prop.vt != VT_BSTR)
   1.293 +      continue;
   1.294 +
   1.295 +    UString ext  = prop.bstrVal;
   1.296 +    UString addExt;
   1.297 +
   1.298 +    prop.Clear();
   1.299 +
   1.300 +    if (getHandlerProperty(NArchive::kAddExtension, &prop) != S_OK)
   1.301 +      continue;
   1.302 +    if (prop.vt == VT_BSTR)
   1.303 +    {
   1.304 +      addExt = prop.bstrVal;
   1.305 +    }
   1.306 +    else if (prop.vt != VT_EMPTY)
   1.307 +      continue;
   1.308 +    prop.Clear();
   1.309 +
   1.310 +    UStringVector exts, addExts;
   1.311 +    SplitString(ext, exts);
   1.312 +    SplitString(addExt, addExts);
   1.313 +
   1.314 +    prop.Clear();
   1.315 +    for (int i = 0; i < exts.Size(); i++)
   1.316 +    {
   1.317 +      CArchiverExtInfo extInfo;
   1.318 +      extInfo.Ext = exts[i];
   1.319 +      if (addExts.Size() > 0)
   1.320 +        extInfo.AddExt = addExts[i];
   1.321 +      if (extInfo.AddExt == L"*")
   1.322 +        extInfo.AddExt.Empty();
   1.323 +      item.Extensions.Add(extInfo);
   1.324 +    }
   1.325 +
   1.326 +    if (getHandlerProperty(NArchive::kUpdate, &prop) == S_OK)
   1.327 +      if (prop.vt == VT_BOOL)
   1.328 +        item.UpdateEnabled = VARIANT_BOOLToBool(prop.boolVal);
   1.329 +    prop.Clear();
   1.330 +
   1.331 +    if (item.UpdateEnabled)
   1.332 +    {
   1.333 +      if (getHandlerProperty(NArchive::kKeepName, &prop) == S_OK)
   1.334 +        if (prop.vt == VT_BOOL)
   1.335 +          item.KeepName = VARIANT_BOOLToBool(prop.boolVal);
   1.336 +      prop.Clear();
   1.337 +    }
   1.338 +
   1.339 +    if (getHandlerProperty(NArchive::kStartSignature, &prop) == S_OK)
   1.340 +    {
   1.341 +      if (prop.vt == VT_BSTR)
   1.342 +      {
   1.343 +        UINT len = ::SysStringByteLen(prop.bstrVal);
   1.344 +        item.StartSignature.SetCapacity(len);
   1.345 +        memmove(item.StartSignature, prop.bstrVal, len);
   1.346 +      }
   1.347 +    }
   1.348 +    prop.Clear();
   1.349 +
   1.350 +    if (getHandlerProperty(NArchive::kAssociate, &prop) == S_OK)
   1.351 +      if (prop.vt == VT_BOOL)
   1.352 +        item.Associate = VARIANT_BOOLToBool(prop.boolVal);
   1.353 +    prop.Clear();
   1.354 +
   1.355 +
   1.356 +    archivers.Add(item);
   1.357 +  }
   1.358 +
   1.359 +  #endif
   1.360 +}
   1.361 +
   1.362 +

mercurial