Tue, 06 Jan 2015 21:39:09 +0100
Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.
michael@0 | 1 | // 7zMethods.cpp |
michael@0 | 2 | |
michael@0 | 3 | #include "StdAfx.h" |
michael@0 | 4 | |
michael@0 | 5 | #include "7zMethods.h" |
michael@0 | 6 | |
michael@0 | 7 | #include "../../../Windows/FileFind.h" |
michael@0 | 8 | #include "../../../Windows/DLL.h" |
michael@0 | 9 | #include "../../../Windows/PropVariant.h" |
michael@0 | 10 | #include "../../../Windows/Synchronization.h" |
michael@0 | 11 | |
michael@0 | 12 | #include "../../ICoder.h" |
michael@0 | 13 | #include "../Common/CodecsPath.h" |
michael@0 | 14 | |
michael@0 | 15 | using namespace NWindows; |
michael@0 | 16 | |
michael@0 | 17 | namespace NArchive { |
michael@0 | 18 | namespace N7z { |
michael@0 | 19 | |
michael@0 | 20 | static CObjectVector<CMethodInfo2> g_Methods; |
michael@0 | 21 | static bool g_Loaded = false; |
michael@0 | 22 | |
michael@0 | 23 | typedef UInt32 (WINAPI *GetNumberOfMethodsFunc)(UInt32 *numMethods); |
michael@0 | 24 | |
michael@0 | 25 | typedef UInt32 (WINAPI *GetMethodPropertyFunc)( |
michael@0 | 26 | UInt32 index, PROPID propID, PROPVARIANT *value); |
michael@0 | 27 | |
michael@0 | 28 | static void Load(const CSysString &folderPrefix) |
michael@0 | 29 | { |
michael@0 | 30 | NFile::NFind::CEnumerator enumerator(folderPrefix + CSysString(TEXT("*"))); |
michael@0 | 31 | NFile::NFind::CFileInfo fileInfo; |
michael@0 | 32 | while (enumerator.Next(fileInfo)) |
michael@0 | 33 | { |
michael@0 | 34 | if (fileInfo.IsDirectory()) |
michael@0 | 35 | continue; |
michael@0 | 36 | CSysString filePath = folderPrefix + fileInfo.Name; |
michael@0 | 37 | { |
michael@0 | 38 | NDLL::CLibrary library; |
michael@0 | 39 | if (!library.LoadEx(filePath, LOAD_LIBRARY_AS_DATAFILE)) |
michael@0 | 40 | continue; |
michael@0 | 41 | } |
michael@0 | 42 | NDLL::CLibrary library; |
michael@0 | 43 | if (!library.Load(filePath)) |
michael@0 | 44 | continue; |
michael@0 | 45 | GetMethodPropertyFunc getMethodProperty = (GetMethodPropertyFunc) |
michael@0 | 46 | library.GetProcAddress("GetMethodProperty"); |
michael@0 | 47 | if (getMethodProperty == NULL) |
michael@0 | 48 | continue; |
michael@0 | 49 | |
michael@0 | 50 | UInt32 numMethods = 1; |
michael@0 | 51 | GetNumberOfMethodsFunc getNumberOfMethodsFunc = (GetNumberOfMethodsFunc) |
michael@0 | 52 | library.GetProcAddress("GetNumberOfMethods"); |
michael@0 | 53 | if (getNumberOfMethodsFunc != NULL) |
michael@0 | 54 | if (getNumberOfMethodsFunc(&numMethods) != S_OK) |
michael@0 | 55 | continue; |
michael@0 | 56 | |
michael@0 | 57 | for(UInt32 i = 0; i < numMethods; i++) |
michael@0 | 58 | { |
michael@0 | 59 | CMethodInfo2 info; |
michael@0 | 60 | info.FilePath = filePath; |
michael@0 | 61 | |
michael@0 | 62 | NWindows::NCOM::CPropVariant propVariant; |
michael@0 | 63 | if (getMethodProperty(i, NMethodPropID::kID, &propVariant) != S_OK) |
michael@0 | 64 | continue; |
michael@0 | 65 | if (propVariant.vt != VT_BSTR) |
michael@0 | 66 | continue; |
michael@0 | 67 | info.MethodID.IDSize = SysStringByteLen(propVariant.bstrVal); |
michael@0 | 68 | memmove(info.MethodID.ID, propVariant.bstrVal, info.MethodID.IDSize); |
michael@0 | 69 | propVariant.Clear(); |
michael@0 | 70 | |
michael@0 | 71 | if (getMethodProperty(i, NMethodPropID::kName, &propVariant) != S_OK) |
michael@0 | 72 | continue; |
michael@0 | 73 | if (propVariant.vt == VT_EMPTY) |
michael@0 | 74 | { |
michael@0 | 75 | } |
michael@0 | 76 | else if (propVariant.vt == VT_BSTR) |
michael@0 | 77 | info.Name = propVariant.bstrVal; |
michael@0 | 78 | else |
michael@0 | 79 | continue; |
michael@0 | 80 | propVariant.Clear(); |
michael@0 | 81 | |
michael@0 | 82 | if (getMethodProperty (i, NMethodPropID::kEncoder, &propVariant) != S_OK) |
michael@0 | 83 | continue; |
michael@0 | 84 | if (propVariant.vt == VT_EMPTY) |
michael@0 | 85 | info.EncoderIsAssigned = false; |
michael@0 | 86 | else if (propVariant.vt == VT_BSTR) |
michael@0 | 87 | { |
michael@0 | 88 | info.EncoderIsAssigned = true; |
michael@0 | 89 | info.Encoder = *(const GUID *)propVariant.bstrVal; |
michael@0 | 90 | } |
michael@0 | 91 | else |
michael@0 | 92 | continue; |
michael@0 | 93 | propVariant.Clear(); |
michael@0 | 94 | |
michael@0 | 95 | if (getMethodProperty (i, NMethodPropID::kDecoder, &propVariant) != S_OK) |
michael@0 | 96 | continue; |
michael@0 | 97 | if (propVariant.vt == VT_EMPTY) |
michael@0 | 98 | info.DecoderIsAssigned = false; |
michael@0 | 99 | else if (propVariant.vt == VT_BSTR) |
michael@0 | 100 | { |
michael@0 | 101 | info.DecoderIsAssigned = true; |
michael@0 | 102 | info.Decoder = *(const GUID *)propVariant.bstrVal; |
michael@0 | 103 | } |
michael@0 | 104 | else |
michael@0 | 105 | continue; |
michael@0 | 106 | propVariant.Clear(); |
michael@0 | 107 | |
michael@0 | 108 | if (getMethodProperty (i, NMethodPropID::kInStreams, &propVariant) != S_OK) |
michael@0 | 109 | continue; |
michael@0 | 110 | if (propVariant.vt == VT_EMPTY) |
michael@0 | 111 | info.NumInStreams = 1; |
michael@0 | 112 | else if (propVariant.vt == VT_UI4) |
michael@0 | 113 | info.NumInStreams = propVariant.ulVal; |
michael@0 | 114 | else |
michael@0 | 115 | continue; |
michael@0 | 116 | propVariant.Clear(); |
michael@0 | 117 | |
michael@0 | 118 | if (getMethodProperty (i, NMethodPropID::kOutStreams, &propVariant) != S_OK) |
michael@0 | 119 | continue; |
michael@0 | 120 | if (propVariant.vt == VT_EMPTY) |
michael@0 | 121 | info.NumOutStreams = 1; |
michael@0 | 122 | else if (propVariant.vt == VT_UI4) |
michael@0 | 123 | info.NumOutStreams = propVariant.ulVal; |
michael@0 | 124 | else |
michael@0 | 125 | continue; |
michael@0 | 126 | propVariant.Clear(); |
michael@0 | 127 | |
michael@0 | 128 | g_Methods.Add(info); |
michael@0 | 129 | } |
michael@0 | 130 | } |
michael@0 | 131 | } |
michael@0 | 132 | |
michael@0 | 133 | static NSynchronization::CCriticalSection g_CriticalSection; |
michael@0 | 134 | |
michael@0 | 135 | void LoadMethodMap() |
michael@0 | 136 | { |
michael@0 | 137 | NSynchronization::CCriticalSectionLock lock(g_CriticalSection); |
michael@0 | 138 | if (g_Loaded) |
michael@0 | 139 | return; |
michael@0 | 140 | g_Loaded = true; |
michael@0 | 141 | Load(GetCodecsFolderPrefix()); |
michael@0 | 142 | } |
michael@0 | 143 | |
michael@0 | 144 | bool GetMethodInfo(const CMethodID &methodID, CMethodInfo &methodInfo) |
michael@0 | 145 | { |
michael@0 | 146 | for(int i = 0; i < g_Methods.Size(); i++) |
michael@0 | 147 | { |
michael@0 | 148 | const CMethodInfo2 &method = g_Methods[i]; |
michael@0 | 149 | if (method.MethodID == methodID) |
michael@0 | 150 | { |
michael@0 | 151 | methodInfo = (CMethodInfo)method; |
michael@0 | 152 | return true; |
michael@0 | 153 | } |
michael@0 | 154 | } |
michael@0 | 155 | return false; |
michael@0 | 156 | } |
michael@0 | 157 | |
michael@0 | 158 | bool GetMethodInfo(const UString &name, CMethodInfo2 &methodInfo) |
michael@0 | 159 | { |
michael@0 | 160 | for(int i = 0; i < g_Methods.Size(); i++) |
michael@0 | 161 | { |
michael@0 | 162 | const CMethodInfo2 &method = g_Methods[i]; |
michael@0 | 163 | if (method.Name.CompareNoCase(name) == 0) |
michael@0 | 164 | { |
michael@0 | 165 | methodInfo = method; |
michael@0 | 166 | return true; |
michael@0 | 167 | } |
michael@0 | 168 | } |
michael@0 | 169 | return false; |
michael@0 | 170 | } |
michael@0 | 171 | |
michael@0 | 172 | }} |
michael@0 | 173 | |
michael@0 | 174 |