Debian Codegraph @

Source/Plugins.cpp

raw
1 /*
2 * Plugins.cpp
3 *
4 * This file is a part of NSIS.
5 *
6 * Copyright (C) 1999-2026 Nullsoft and Contributors
7 *
8 * Licensed under the zlib/libpng license (the "License");
9 * you may not use this file except in compliance with the License.
10 *
11 * Licence details can be found in the file COPYING.
12 *
13 * This software is provided 'as-is', without any express or implied
14 * warranty.
15 */
16
17 #include "exehead/config.h"
18 #ifdef NSIS_CONFIG_PLUGIN_SUPPORT
19
20 #include <map>
21 #include "tstring.h"
22 #include <fstream>
23
24 #include "Plugins.h"
25 #include "Platform.h"
26 #include "util.h"
27 #include "ResourceEditor.h"
28
29 #include "dirreader.h"
30
31 #ifdef _WIN32
32 # include <winnt.h>
33 #else
34 # include <sys/stat.h>
35 #endif
36
37 #include "boost/scoped_ptr.hpp"
38
39 using namespace std;
40
41 extern FILE *g_output;
42
43 namespace {
44
45 template <class C, class K>
46 bool contains(const C& cntnr, const K& key)
47 {
48 return cntnr.find(key) != cntnr.end();
49 }
50
51 template <class C, class K>
52 const typename C::const_iterator get_iterator(const C& cntnr, const K& key)
53 {
54 const typename C::const_iterator it = cntnr.find(key);
55 assert(cntnr.end() != it);
56 return it;
57 }
58
59 template <class C, class K>
60 typename C::const_iterator::value_type get_value(const C& cntnr, const K& key)
61 {
62 return *get_iterator(cntnr,key);
63 }
64
65 template <class C, class K>
66 typename C::value_type::second_type get_paired_value(const C& cntnr, const K& key)
67 {
68 return get_iterator(cntnr,key)->second;
69 }
70 template <class V, class C, class K>
71 V get_paired_value(const C& cntnr, const K& key, const V& defval)
72 {
73 typename C::const_iterator it = cntnr.find(key);
74 return cntnr.end() == it ? defval : it->second;
75 }
76
77 }
78
79 static tstring GetDllPath(const tstring&fullpath)
80 {
81 tstring::size_type platsep = fullpath.rfind(PLATFORM_PATH_SEPARATOR_STR);
82 tstring::size_type unixsep = platsep;
83 if (PLATFORM_PATH_SEPARATOR_C != _T('/')) unixsep = fullpath.rfind(_T("/")); // Ideally get_dir_name would do this for us
84 tstring::size_type lastsep = platsep != string::npos && unixsep != string::npos ? STD_MAX(platsep, unixsep) : STD_MIN(platsep, unixsep);
85 if (lastsep == string::npos) return _T("");
86 return fullpath.substr(0, lastsep);
87 }
88
89 static inline tstring GetDllName(const tstring&command)
90 {
91 return get_string_prefix(command, _T("::"));
92 }
93
94 static inline void PrintCommandSig(const tstring sig)
95 {
96 _ftprintf(g_output, _T(" + %") NPRIs _T("\n"), sig.c_str());
97 }
98
99 void Plugins::AddPluginsDir(const tstring &path, bool pe64, bool displayInfo)
100 {
101 boost::scoped_ptr<dir_reader> dr( new_dir_reader() );
102 dr->read(path);
103
104 for (dir_reader::iterator files_itr = dr->files().begin();
105 files_itr != dr->files().end();
106 files_itr++) // note: files are listed alphabetically, so plugin.dll will be listed before pluginW.dll
107 {
108 if (!dir_reader::matches(*files_itr, _T("*.dll")))
109 continue;
110
111 const tstring plugin = get_full_path(path + PLATFORM_PATH_SEPARATOR_C + *files_itr);
112 GetExports(plugin, pe64, displayInfo);
113 }
114 }
115
116 // VC6 cannot handle NSISException(const tstring& msg) : std::runtime_error(string(TtoCString(msg))) {}
117 struct NSISExceptionInner : public std::runtime_error
118 {
119 NSISExceptionInner(const char* msg) : std::runtime_error(string(msg)) {} // Unicode
120 NSISExceptionInner(const string&msg) : std::runtime_error(msg) {} // Ansi
121 };
122 struct NSISException : public NSISExceptionInner
123 {
124 NSISException(const tstring& msg) : NSISExceptionInner(TtoCString(msg)) {}
125 };
126
127 void Plugins::GetExports(const tstring &pathToDll, bool pe64, bool displayInfo)
128 {
129 PIMAGE_NT_HEADERS pNTHdrs;
130 unsigned long dllsize;
131 BYTE *dlldata = alloc_and_read_file(pathToDll.c_str(), dllsize);
132 MANAGE_WITH(dlldata, free);
133
134 try {
135 if (!dllsize) return ;
136 pNTHdrs = CResourceEditor::GetNTHeaders(&dlldata[0]); // This might throw
137 } catch (std::runtime_error&) {
138 return;
139 }
140
141 const WORD reqohm = pe64 ? IMAGE_NT_OPTIONAL_HDR64_MAGIC : IMAGE_NT_OPTIONAL_HDR32_MAGIC;
142 if (*GetCommonMemberFromPEOptHdr(pNTHdrs->OptionalHeader, Magic) != reqohm)
143 {
144 // Ignore DLLs that don't match our target
145 return;
146 }
147
148 tstring dllName = remove_file_extension(get_file_name(pathToDll));
149 if (DllHasDataHandle(dllName))
150 {
151 m_dllname_conflicts.insert(dllName);
152 }
153
154 FIX_ENDIAN_INT16_INPLACE(pNTHdrs->FileHeader.Characteristics);
155 if (pNTHdrs->FileHeader.Characteristics & IMAGE_FILE_DLL)
156 {
157 DWORD numrvaandsiz = *GetMemberFromPEOptHdr(pNTHdrs->OptionalHeader, NumberOfRvaAndSizes);
158 FIX_ENDIAN_INT32_INPLACE(numrvaandsiz);
159 if (numrvaandsiz <= IMAGE_DIRECTORY_ENTRY_EXPORT) return;
160
161 const IMAGE_DATA_DIRECTORY *pExportDir;
162 pExportDir = &(*GetMemberFromPEOptHdr(pNTHdrs->OptionalHeader, DataDirectory))[IMAGE_DIRECTORY_ENTRY_EXPORT];
163 const DWORD ExportDirVA = FIX_ENDIAN_INT32(pExportDir->VirtualAddress);
164 const DWORD ExportDirSize = FIX_ENDIAN_INT32(pExportDir->Size);
165
166 PIMAGE_SECTION_HEADER sections = IMAGE_FIRST_SECTION(pNTHdrs);
167 const WORD num_sections = FIX_ENDIAN_INT16(pNTHdrs->FileHeader.NumberOfSections);
168
169 for (DWORD i = 0; i < num_sections; i++)
170 {
171 DWORD va = FIX_ENDIAN_INT32(sections[i].VirtualAddress);
172 if (va <= ExportDirVA
173 && va + FIX_ENDIAN_INT32(sections[i].SizeOfRawData) >= ExportDirVA + ExportDirSize)
174 {
175 DWORD prd = FIX_ENDIAN_INT32(sections[i].PointerToRawData);
176 PIMAGE_EXPORT_DIRECTORY exports = PIMAGE_EXPORT_DIRECTORY(&dlldata[0] + prd + ExportDirVA - va);
177 DWORD na = FIX_ENDIAN_INT32(exports->AddressOfNames);
178 LPDWORD names = (LPDWORD)((ULONG_PTR)exports + na - ExportDirVA);
179 for (DWORD j = 0; j < FIX_ENDIAN_INT32(exports->NumberOfNames); j++)
180 {
181 if (0 == j) m_dllname_to_path[dllName] = pathToDll;
182
183 const string name = string((char*)exports + FIX_ENDIAN_INT32(names[j]) - ExportDirVA);
184 const tstring canoniccmd = dllName + _T("::") + tstring(CtoTString(name));
185 if (displayInfo)
186 {
187 bool hadCmd = contains(m_commands, canoniccmd);
188 if (!hadCmd) PrintCommandSig(canoniccmd);
189 }
190 m_commands.insert(canoniccmd);
191 }
192 break;
193 }
194 }
195 }
196 }
197
198 int Plugins::GetDllDataHandle(bool uninst, const tstring& command) const
199 {
200 const tstring dllname = GetDllName(command);
201 if (uninst)
202 return get_paired_value(m_dllname_to_unst_datahandle, dllname, -1);
203 else
204 return get_paired_value(m_dllname_to_inst_datahandle, dllname, -1);
205 }
206
207 void Plugins::SetDllDataHandle(bool uninst, tstring&canoniccmd, int dataHandle)
208 {
209 const tstring dllname = GetDllName(canoniccmd);
210 if (uninst)
211 m_dllname_to_unst_datahandle[dllname] = dataHandle;
212 else
213 m_dllname_to_inst_datahandle[dllname] = dataHandle;
214 }
215
216 bool Plugins::DllHasDataHandle(const tstring& dllnamelowercase) const
217 {
218 int h = GetDllDataHandle(false, dllnamelowercase);
219 if (-1 == h) h = GetDllDataHandle(true, dllnamelowercase);
220 return -1 != h;
221 }
222
223 bool Plugins::Initialize(const TCHAR*arcsubdir, bool pe64, bool displayInfo)
224 {
225 if (m_initialized) return true;
226 m_initialized = true;
227
228 AddPluginsDir(tstring(arcsubdir), pe64, displayInfo);
229
230 return true;
231 }
232
233 bool Plugins::FindDllPath(const tstring filename, tstring&dllPath)
234 {
235 tstring dllName = remove_file_extension(filename);
236 if (!contains(m_dllname_to_path, dllName)) return false;
237 dllPath = get_paired_value(m_dllname_to_path, dllName);
238 return true;
239 }
240
241 bool Plugins::GetCommandInfo(const tstring&command, tstring&canoniccmd, tstring&dllPath)
242 {
243 const tstring dllname = GetDllName(command);
244 dllPath = get_paired_value(m_dllname_to_path, dllname);
245 canoniccmd = get_value(m_commands, command);
246 return !contains(m_dllname_conflicts, dllname);
247 }
248
249 bool Plugins::IsPluginCommand(const tstring& token) const
250 {
251 return contains(m_commands, token);
252 }
253
254 bool Plugins::IsKnownPlugin(const tstring& token) const
255 {
256 const tstring dllname = GetDllName(token);
257 return contains(m_dllname_to_path, dllname);
258 }
259
260 bool Plugins::IsPluginCallSyntax(const tstring& token)
261 {
262 const tstring dllname = GetDllName(token);
263 return dllname.length() + 2 < token.length();
264 }
265
266 struct PrintPluginDirsHelper {
267 template<class C> static void print(const C&c, const char*indent = "")
268 {
269 std::/*unordered_*/set<NSIS_CXX_TYPENAME STL::mapped_type<C>::type
270 #ifdef _WIN32
271 , Plugins::strnocasecmp
272 #endif
273 > seen;
274 for (NSIS_CXX_TYPENAME C::const_iterator it = c.begin(); it != c.end(); ++it)
275 {
276 const tstring path = GetDllPath(it->second);
277 if (contains(seen, path)) continue;
278 seen.insert(path);
279 _ftprintf(g_output, _T("%") NPRIns _T("%") NPRIs _T("\n"), indent, path.c_str());
280 }
281 }
282 };
283
284 void Plugins::PrintPluginDirs()
285 {
286 _ftprintf(g_output, _T("Plugin directories:\n"));
287 PrintPluginDirsHelper::print(m_dllname_to_path, " ");
288 }
289
290 #endif