|
2 | 2 |
|
3 | 3 | #include "pe.h" |
4 | 4 | #include "os.h" |
| 5 | +#include "winresource.h" |
5 | 6 |
|
| 7 | +#include "Logger.h" |
6 | 8 | #include "PoolAllocator.h" |
7 | 9 |
|
8 | 10 | extern "C" |
@@ -178,3 +180,121 @@ MdlpGetProcAddress( |
178 | 180 |
|
179 | 181 | return STATUS_NOT_FOUND; |
180 | 182 | } |
| 183 | + |
| 184 | +NTSTATUS |
| 185 | +MdlpGetProductVersion( |
| 186 | + _In_ HANDLE hModule, |
| 187 | + _Out_ PCWSTR* pPProductVersion |
| 188 | +) |
| 189 | +{ |
| 190 | + if (hModule == NULL || pPProductVersion == NULL) |
| 191 | + { |
| 192 | + return STATUS_INVALID_PARAMETER; |
| 193 | + } |
| 194 | + |
| 195 | + PCHAR pStart = (PCHAR)hModule; |
| 196 | + |
| 197 | + PIMAGE_DOS_HEADER pDosHeader = (PIMAGE_DOS_HEADER)pStart; |
| 198 | + PIMAGE_NT_HEADERS pPeHeader = (PIMAGE_NT_HEADERS)(pStart + pDosHeader->e_lfanew); |
| 199 | + |
| 200 | + PIMAGE_RESOURCE_DIRECTORY pResourceDirectory = (PIMAGE_RESOURCE_DIRECTORY)(pStart + |
| 201 | + pPeHeader->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_RESOURCE].VirtualAddress); |
| 202 | + |
| 203 | + // For calculating offsets. |
| 204 | + PCHAR pRsrcSectionStart = (PCHAR)pResourceDirectory; |
| 205 | + |
| 206 | + // The entries come right after the directory. |
| 207 | + PIMAGE_RESOURCE_DIRECTORY_ENTRY pEntryList = (PIMAGE_RESOURCE_DIRECTORY_ENTRY) |
| 208 | + (pResourceDirectory + 1); |
| 209 | + |
| 210 | + SIZE_T uTotalEntries = pResourceDirectory->NumberOfIdEntries |
| 211 | + + pResourceDirectory->NumberOfNamedEntries; |
| 212 | + |
| 213 | + WORD pResourcePath[] = |
| 214 | + { |
| 215 | + (WORD)(ULONG_PTR)RT_VERSION, |
| 216 | + // This param must be 1 |
| 217 | + // https://learn.microsoft.com/en-us/windows/win32/menurc/versioninfo-resource |
| 218 | + 1 |
| 219 | + }; |
| 220 | + |
| 221 | + const auto GoDownPath = [&](PIMAGE_RESOURCE_DIRECTORY_ENTRY pEntry) |
| 222 | + { |
| 223 | + pResourceDirectory = (PIMAGE_RESOURCE_DIRECTORY) |
| 224 | + (pRsrcSectionStart + pEntry->OffsetToDirectory); |
| 225 | + |
| 226 | + pEntryList = (PIMAGE_RESOURCE_DIRECTORY_ENTRY)(pResourceDirectory + 1); |
| 227 | + uTotalEntries = pResourceDirectory->NumberOfIdEntries |
| 228 | + + pResourceDirectory->NumberOfNamedEntries; |
| 229 | + }; |
| 230 | + |
| 231 | + for (SIZE_T i = 0; i < ARRAYSIZE(pResourcePath); ++i) |
| 232 | + { |
| 233 | + for (SIZE_T j = 0; j < uTotalEntries; ++j) |
| 234 | + { |
| 235 | + if (!pEntryList[j].NameIsString |
| 236 | + && pEntryList[j].Id == pResourcePath[i] |
| 237 | + && pEntryList[j].DataIsDirectory) |
| 238 | + { |
| 239 | + // Go down to the tree on this path. |
| 240 | + GoDownPath(&pEntryList[j]); |
| 241 | + |
| 242 | + Logger::LogTrace("Found level ", i + 1, " directory with ", uTotalEntries, |
| 243 | + " entries."); |
| 244 | + |
| 245 | + goto found; |
| 246 | + } |
| 247 | + } |
| 248 | + |
| 249 | + Logger::LogError("Cannot find level ", i + 1, " directory with ID ", pResourcePath[i]); |
| 250 | + return STATUS_RESOURCE_TYPE_NOT_FOUND; |
| 251 | + |
| 252 | + found: |
| 253 | + continue; |
| 254 | + } |
| 255 | + |
| 256 | + // Take the first directory type entry. Should be a leaf of the VS_VERSIONINFO data type. |
| 257 | + // https://learn.microsoft.com/en-us/windows/win32/menurc/vs-versioninfo |
| 258 | + for (SIZE_T i = 0; i < uTotalEntries; ++i) |
| 259 | + { |
| 260 | + if (!pEntryList[i].DataIsDirectory) |
| 261 | + { |
| 262 | + PIMAGE_RESOURCE_DATA_ENTRY pDataEntry = (PIMAGE_RESOURCE_DATA_ENTRY) |
| 263 | + (pRsrcSectionStart + pEntryList[i].OffsetToData); |
| 264 | + |
| 265 | + SIZE_T uVersionInfoLength = pDataEntry->Size; |
| 266 | + PCHAR pVersionInfoStart = pStart + pDataEntry->OffsetToData; |
| 267 | + |
| 268 | + PCHAR pProductVersion = pVersionInfoStart; |
| 269 | + |
| 270 | + // TODO: Implement actual resource string tree parsing. |
| 271 | + const WCHAR pPattern[] = L"ProductVersion"; |
| 272 | + while (pProductVersion + sizeof(pPattern) |
| 273 | + <= pVersionInfoStart + uVersionInfoLength) |
| 274 | + { |
| 275 | + if (wcsncmp((PCWSTR)pProductVersion, pPattern, sizeof(pPattern)) != 0) |
| 276 | + { |
| 277 | + ++pProductVersion; |
| 278 | + continue; |
| 279 | + } |
| 280 | + |
| 281 | + goto found_product_version; |
| 282 | + } |
| 283 | + |
| 284 | + Logger::LogError("Cannot find product version"); |
| 285 | + return STATUS_NOT_FOUND; |
| 286 | + |
| 287 | + found_product_version: |
| 288 | + // Align up a 32-bit boundary. |
| 289 | + PWCHAR pProductVersionValue = (PWCHAR)ALIGN_UP_BY( |
| 290 | + pProductVersion + sizeof(pPattern), 4); |
| 291 | + |
| 292 | + *pPProductVersion = pProductVersionValue; |
| 293 | + return STATUS_SUCCESS; |
| 294 | + } |
| 295 | + } |
| 296 | + |
| 297 | + // No languages available? |
| 298 | + Logger::LogError("Cannot find leaf"); |
| 299 | + return STATUS_RESOURCE_LANG_NOT_FOUND; |
| 300 | +} |
0 commit comments