| 1 |
/* |
| 2 |
* ResourceEditor.cpp |
| 3 |
* |
| 4 |
* This file is a part of NSIS. |
| 5 |
* |
| 6 |
* Copyright (C) 2002-2026 Amir Szekely <[email protected]> |
| 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 |
* Reviewed for Unicode support by Jim Park -- 08/21/2007 |
| 17 |
*/ |
| 18 |
|
| 19 |
#include "ResourceEditor.h" |
| 20 |
#include "util.h" |
| 21 |
#include "winchar.h" |
| 22 |
#include <queue> |
| 23 |
#include <cassert> |
| 24 |
#include "tchar.h" |
| 25 |
#include "utf.h" |
| 26 |
#include "BinInterop.h" |
| 27 |
using namespace std; |
| 28 |
|
| 29 |
////////////////////////////////////////////////////////////////////// |
| 30 |
// Utilities |
| 31 |
////////////////////////////////////////////////////////////////////// |
| 32 |
|
| 33 |
#define FIRSTRESDIRSTRADDRESS ( (WINWCHAR*)(~(size_t)0) ) |
| 34 |
#define WCHARPTR2WINWCHARPTR(s) ( (WINWCHAR*) (s) ) // Only for WinSDK structs like IMAGE_RESOURCE_DIR_STRING_U where we cannot change the WCHAR type! |
| 35 |
#define RALIGN(dwToAlign, dwAlignOn) ((dwToAlign%dwAlignOn == 0) ? dwToAlign : dwToAlign - (dwToAlign%dwAlignOn) + dwAlignOn) |
| 36 |
#define ALIGN(dwToAlign, dwAlignOn) dwToAlign = RALIGN((dwToAlign), (dwAlignOn)) |
| 37 |
|
| 38 |
static inline DWORD ConvertEndianness(DWORD d) { return FIX_ENDIAN_INT32(d); } |
| 39 |
static inline WORD ConvertEndianness(WORD w) { return FIX_ENDIAN_INT16(w); } |
| 40 |
|
| 41 |
#if !(defined(_WIN32) && defined(_UNICODE)) |
| 42 |
static void FreeUnicodeResString(WINWCHAR* s) { |
| 43 |
if (!IS_INTRESOURCE(s) && FIRSTRESDIRSTRADDRESS != (WINWCHAR*) s) |
| 44 |
free(s); |
| 45 |
} |
| 46 |
static WINWCHAR* ResStringToUnicode(const TCHAR *s) { |
| 47 |
if (IS_INTRESOURCE(s)) return MAKEINTRESOURCEWINW((ULONG_PTR)s); |
| 48 |
if (FIRSTRESDIRSTRADDRESS == (WINWCHAR*) s) return (WINWCHAR*) s; |
| 49 |
WINWCHAR *ws = WinWStrDupFromTChar(s); |
| 50 |
if (!ws) throw std::runtime_error("Unicode conversion failed"); |
| 51 |
return ws; |
| 52 |
} |
| 53 |
#endif //~ !(_WIN32 && _UNICODE) |
| 54 |
|
| 55 |
struct UTF16LEResString { |
| 56 |
WINWCHAR *m_s; |
| 57 |
operator WINWCHAR*() const { return m_s; } |
| 58 |
#if defined(_WIN32) && defined(_UNICODE) |
| 59 |
UTF16LEResString(const TCHAR*tstr) : m_s((WINWCHAR*) tstr) {} |
| 60 |
#else |
| 61 |
UTF16LEResString(const TCHAR*tstr) : m_s(ResStringToUnicode(tstr)) { } |
| 62 |
~UTF16LEResString() { FreeUnicodeResString(m_s); } |
| 63 |
#endif |
| 64 |
}; |
| 65 |
|
| 66 |
PIMAGE_NT_HEADERS CResourceEditor::GetNTHeaders(BYTE* pbPE) { |
| 67 |
// Get dos header |
| 68 |
PIMAGE_DOS_HEADER dosHeader = (PIMAGE_DOS_HEADER) pbPE; |
| 69 |
if (dosHeader->e_magic != IMAGE_DOS_SIGNATURE) |
| 70 |
throw runtime_error("PE file contains invalid DOS header"); |
| 71 |
|
| 72 |
// Get NT headers |
| 73 |
PIMAGE_NT_HEADERS ntHeaders = (PIMAGE_NT_HEADERS)(pbPE + ConvertEndianness((DWORD)dosHeader->e_lfanew)); |
| 74 |
if (ntHeaders->Signature != IMAGE_NT_SIGNATURE) |
| 75 |
throw runtime_error("PE file missing NT signature"); |
| 76 |
|
| 77 |
// Make sure this is a supported PE format |
| 78 |
const WORD ohm = *GetCommonMemberFromPEOptHdr(ntHeaders->OptionalHeader, Magic); |
| 79 |
if (ohm != IMAGE_NT_OPTIONAL_HDR32_MAGIC && ohm != IMAGE_NT_OPTIONAL_HDR64_MAGIC) |
| 80 |
throw runtime_error("Unsupported PE format"); |
| 81 |
|
| 82 |
return ntHeaders; |
| 83 |
} |
| 84 |
|
| 85 |
PRESOURCE_DIRECTORY CResourceEditor::GetResourceDirectory( |
| 86 |
BYTE* pbPE, |
| 87 |
DWORD dwSize, |
| 88 |
PIMAGE_NT_HEADERS ntHeaders, |
| 89 |
DWORD *pdwResSecVA /*=NULL*/, |
| 90 |
DWORD *pdwSectionIndex /*=NULL*/ |
| 91 |
) { |
| 92 |
PIMAGE_DATA_DIRECTORY dataDirectory = *GetMemberFromPEOptHdr(ntHeaders->OptionalHeader, DataDirectory); |
| 93 |
DWORD dwNumberOfRvaAndSizes = *GetMemberFromPEOptHdr(ntHeaders->OptionalHeader, NumberOfRvaAndSizes); |
| 94 |
|
| 95 |
if (ConvertEndianness(dwNumberOfRvaAndSizes) <= IMAGE_DIRECTORY_ENTRY_RESOURCE) |
| 96 |
throw runtime_error("No resource section found"); |
| 97 |
// Get resource section virtual address |
| 98 |
DWORD dwResSecVA = ConvertEndianness(dataDirectory[IMAGE_DIRECTORY_ENTRY_RESOURCE].VirtualAddress); |
| 99 |
// Pointer to the sections headers array |
| 100 |
PIMAGE_SECTION_HEADER sectionHeadersArray = IMAGE_FIRST_SECTION(ntHeaders); |
| 101 |
|
| 102 |
DWORD dwSectionIndex = (DWORD) -1; |
| 103 |
|
| 104 |
// Find resource section index in the array |
| 105 |
for (int i = 0; i < ConvertEndianness(ntHeaders->FileHeader.NumberOfSections); i++) { |
| 106 |
if (dwResSecVA == ConvertEndianness(sectionHeadersArray[i].VirtualAddress)) { |
| 107 |
// Remember resource section index |
| 108 |
dwSectionIndex = i; |
| 109 |
// Check for invalid resource section pointer |
| 110 |
if (!sectionHeadersArray[i].PointerToRawData) |
| 111 |
throw runtime_error("Invalid resource section pointer"); |
| 112 |
|
| 113 |
break; |
| 114 |
} |
| 115 |
|
| 116 |
// Invalid section pointer (goes beyond the PE image) |
| 117 |
if (ConvertEndianness(sectionHeadersArray[i].PointerToRawData) > dwSize) |
| 118 |
throw runtime_error("Invalid section pointer"); |
| 119 |
} |
| 120 |
|
| 121 |
// No resource section... |
| 122 |
if (dwSectionIndex == (DWORD) -1) |
| 123 |
throw runtime_error("PE file doesn't contain any resource section"); |
| 124 |
|
| 125 |
// Return extra parameters |
| 126 |
if (pdwSectionIndex) |
| 127 |
*pdwSectionIndex = dwSectionIndex; |
| 128 |
if (pdwResSecVA) |
| 129 |
*pdwResSecVA = dwResSecVA; |
| 130 |
|
| 131 |
// Pointer to section data, the first resource directory |
| 132 |
DWORD dwResSecPtr = ConvertEndianness(sectionHeadersArray[dwSectionIndex].PointerToRawData); |
| 133 |
return PRESOURCE_DIRECTORY(pbPE + dwResSecPtr); |
| 134 |
} |
| 135 |
|
| 136 |
static int ParseSimpleInt(const TCHAR*parse, int*success) |
| 137 |
{ |
| 138 |
int base = 0, num; |
| 139 |
TCHAR *end; |
| 140 |
if (parse[0] == _T('0') && (parse[1] >= _T('0') && parse[1] <= _T('9'))) base = 10; // Avoid evil octal |
| 141 |
num = (int)_tcstoul(parse, &end, base); |
| 142 |
if (success) *success = !(int)(*end); |
| 143 |
return num; |
| 144 |
} |
| 145 |
|
| 146 |
const TCHAR* CResourceEditor::ParseResourceTypeString(const TCHAR*Str) |
| 147 |
{ |
| 148 |
if (Str[0] == _T('#')) // Special character used by KERNEL32!FindResource |
| 149 |
{ |
| 150 |
++Str; |
| 151 |
if (!_tcsicmp(Str, _T("Bitmap"))) return (TCHAR*) RT_BITMAP; |
| 152 |
if (!_tcsicmp(Str, _T("IconImage"))) return (TCHAR*) RT_ICON; |
| 153 |
if (!_tcsicmp(Str, _T("Icon"))) return (TCHAR*) RT_GROUP_ICON; |
| 154 |
if (!_tcsicmp(Str, _T("CursorImage"))) return (TCHAR*) RT_CURSOR; |
| 155 |
if (!_tcsicmp(Str, _T("Cursor"))) return (TCHAR*) RT_GROUP_CURSOR; |
| 156 |
if (!_tcsicmp(Str, _T("Dialog"))) return (TCHAR*) RT_DIALOG; |
| 157 |
if (!_tcsicmp(Str, _T("Menu"))) return (TCHAR*) MAKEINTRESOURCE(4); |
| 158 |
if (!_tcsicmp(Str, _T("Version"))) return (TCHAR*) RT_VERSION; |
| 159 |
if (!_tcsicmp(Str, _T("HTML"))) return (TCHAR*) MAKEINTRESOURCE(23); |
| 160 |
if (!_tcsicmp(Str, _T("Manifest"))) return (TCHAR*) MAKEINTRESOURCE(24); |
| 161 |
int succ, num = ParseSimpleInt(Str, &succ); |
| 162 |
return succ && IS_INTRESOURCE(num) ? (TCHAR*) MAKEINTRESOURCE(num) : NULL; |
| 163 |
} |
| 164 |
return *Str ? Str : NULL; |
| 165 |
} |
| 166 |
|
| 167 |
#include "exehead/resource.h" // IDI_ICON2 |
| 168 |
#include "exehead/config.h" // NSIS_DEFAULT_LANG |
| 169 |
const TCHAR* CResourceEditor::ParseResourceNameString(const TCHAR*Str, bool AllowFirst) { |
| 170 |
if (Str[0] == _T('#')) |
| 171 |
{ |
| 172 |
++Str; |
| 173 |
if (!_tcsicmp(Str, _T("Version"))) return (TCHAR*) MAKEINTRESOURCE(1); |
| 174 |
if (!_tcsicmp(Str, _T("Icon"))) return (TCHAR*) MAKEINTRESOURCE(IDI_ICON2); |
| 175 |
if (AllowFirst && Str[0] == '?' && !Str[1]) return (TCHAR*) FIRSTRESDIRSTRADDRESS; |
| 176 |
int succ, num = ParseSimpleInt(Str, &succ); |
| 177 |
return succ && IS_INTRESOURCE(num) ? (TCHAR*) MAKEINTRESOURCE(num) : NULL; |
| 178 |
} |
| 179 |
return EditorSupportsStringNames() && *Str ? Str : NULL; |
| 180 |
} |
| 181 |
|
| 182 |
LANGID CResourceEditor::ParseResourceLangString(const TCHAR*Str) { |
| 183 |
if (!_tcsicmp(Str, _T("Any"))) return ANYLANGID; |
| 184 |
if (!_tcsicmp(Str, _T("All"))) return ALLLANGID; |
| 185 |
if (!_tcsicmp(Str, _T("Neutral"))) return 0x0000; //MAKELANGID(0, 0); |
| 186 |
if (!_tcsicmp(Str, _T("Default"))) return NSIS_DEFAULT_LANG; |
| 187 |
int succ, num = ParseSimpleInt(Str, &succ); |
| 188 |
return succ ? num : INVALIDLANGID; |
| 189 |
} |
| 190 |
|
| 191 |
LANGID CResourceEditor::ParseResourceTypeNameLangString(const TCHAR**Type, const TCHAR**Name, const TCHAR*Lang, bool AllowFirst) { |
| 192 |
if (!(*Type = ParseResourceTypeString(*Type))) return INVALIDLANGID; |
| 193 |
if (!(*Name = ParseResourceNameString(*Name, AllowFirst))) return INVALIDLANGID; |
| 194 |
return ParseResourceLangString(Lang); |
| 195 |
} |
| 196 |
|
| 197 |
UINT CResourceEditor::IsResProtocol(const TCHAR*Url) |
| 198 |
{ |
| 199 |
if ('r' == S7ChLwr(Url[0]) && 'e' == S7ChLwr(Url[1]) && 's' == S7ChLwr(Url[2])) |
| 200 |
if (':' == Url[3] && '/' == Url[4] && '/' == Url[5]) |
| 201 |
return 6; |
| 202 |
return 0; |
| 203 |
} |
| 204 |
|
| 205 |
static TCHAR* ParseResProtocolAlloc(const TCHAR*Url, const TCHAR*&Type, const TCHAR*&Name, LANGID&Lang) { |
| 206 |
//msdn.microsoft.com/library/aa767740#res Protocol |
| 207 |
TCHAR *path = 0, *buf = 0, *pD, ch; |
| 208 |
UINT prefix, mswin = Platform_IsWindows(), bad = false, pipe = 0, skip = 0; |
| 209 |
size_t typestart = 0, namestart = 0, i, cch; |
| 210 |
if (!(prefix = CResourceEditor::IsResProtocol(Url))) |
| 211 |
return path; |
| 212 |
for (Url += prefix, i = 0; Url[i]; ++i) |
| 213 |
if (Url[i] == '/') |
| 214 |
typestart = namestart, namestart = i; |
| 215 |
const TCHAR*pS = Url; |
| 216 |
if (namestart > 2 && (buf = (TCHAR*) malloc((cch = ++i) * sizeof(*Url)))) { |
| 217 |
if (pS[0] == '/') ++pS; |
| 218 |
ch = S7ChLwr(pS[0]); |
| 219 |
if (ch >= 'a' && ch <= 'z' && (pS[1] == ':' || pS[1] == '|') && IsAgnosticPathSeparator(pS[2])) { // IEBlog:"File URIs in Windows" says %3A is not a drive delimiter. |
| 220 |
if (Url[0] == '/') ++skip; // "res:///C:/.." => "res://C:/.." (Even on POSIX so that our FOPEN can do "c:/.." => "/c/..") |
| 221 |
pipe = (UINT)(size_t) ((pS + 1) - (Url + skip)); |
| 222 |
} |
| 223 |
typestart -= skip, namestart -= skip; |
| 224 |
const TCHAR *rt = buf + typestart + 1, *rn = buf + namestart + 1, *rl = _T("Any"); |
| 225 |
my_strncpy(buf, Url + skip, cch); |
| 226 |
buf[typestart] = buf[namestart] = '\0'; // Note: Type and Name are not decoded. |
| 227 |
if (pipe) buf[pipe] = ':'; // "res://C|/.." => "res://C:/.." (The | replacement is technically a file:// legacy feature but we support it for res:// as well) |
| 228 |
for (pD = buf, pS = pD;; ++pS, ++pD) { |
| 229 |
if ((ch = *pS) == '%') { // Deal with percent-encoding |
| 230 |
if (*++pS != '%') { |
| 231 |
TCHAR hex[3] = { pS[0], pS[0] ? pS[1] : _T('\0'), _T('\0') }; |
| 232 |
ch = ChIsHex(pS[0]) && ChIsHex(pS[1]) ? (TCHAR) _tcstol(hex, 0, 16) : 0; |
| 233 |
if (ch) ++pS; else ++bad; |
| 234 |
} |
| 235 |
} |
| 236 |
if (!(*pD = (mswin && ch == '/') ? '\\' : ch)) break; // Convert path if needed and stop at the end. |
| 237 |
} |
| 238 |
Lang = CResourceEditor::ParseResourceTypeNameLangString(&rt, &rn, rl, true); |
| 239 |
if (!bad && Lang != CResourceEditor::INVALIDLANGID) |
| 240 |
path = buf, Type = rt, Name = rn; |
| 241 |
} |
| 242 |
if (!path) free(buf); |
| 243 |
return path; |
| 244 |
} |
| 245 |
|
| 246 |
template<class T> static WORD GetDependentType(T Type) { |
| 247 |
if (!IS_INTRESOURCE((size_t) Type)) return 0; |
| 248 |
if (MAKEINTRESOURCE((size_t) Type) == RT_GROUP_ICON) return (WORD)(size_t) RT_ICON; |
| 249 |
if (MAKEINTRESOURCE((size_t) Type) == RT_GROUP_CURSOR) return (WORD)(size_t) RT_CURSOR; |
| 250 |
return 0; |
| 251 |
} |
| 252 |
|
| 253 |
template<class T> static WORD IsIcoCurSingleImageType(T Type) { |
| 254 |
WORD t = IS_INTRESOURCE((size_t) Type) ? (WORD)(size_t) Type : 0; |
| 255 |
return t == (WORD)(size_t) RT_ICON || t == (WORD)(size_t) RT_CURSOR; |
| 256 |
} |
| 257 |
|
| 258 |
template<class T> static WORD IsIcoCurGroupType(T Type) { |
| 259 |
return IsIcoCurSingleImageType(GetDependentType(Type)); |
| 260 |
} |
| 261 |
|
| 262 |
template<class T> static WORD IsIcoCurType(T Type) { |
| 263 |
return IsIcoCurSingleImageType(Type) || IsIcoCurGroupType(Type); |
| 264 |
} |
| 265 |
|
| 266 |
////////////////////////////////////////////////////////////////////// |
| 267 |
////////////////////////////////////////////////////////////////////// |
| 268 |
// CResourceEditor |
| 269 |
////////////////////////////////////////////////////////////////////// |
| 270 |
////////////////////////////////////////////////////////////////////// |
| 271 |
|
| 272 |
////////////////////////////////////////////////////////////////////// |
| 273 |
// Construction/Destruction |
| 274 |
////////////////////////////////////////////////////////////////////// |
| 275 |
|
| 276 |
CResourceEditor::CResourceEditor(void* pbPE, int iSize, bool bKeepData /*=true*/) { |
| 277 |
// Copy the data pointer |
| 278 |
m_pbPE = (BYTE*) pbPE; |
| 279 |
m_iSize = iSize; |
| 280 |
m_bKeepData = bKeepData; |
| 281 |
|
| 282 |
assert(!EditorSupportsStringNames()); |
| 283 |
assert(!EditorSupportsCursorPng()); |
| 284 |
|
| 285 |
// Get NT headers |
| 286 |
m_ntHeaders = GetNTHeaders(m_pbPE); |
| 287 |
|
| 288 |
// No check sum support yet... |
| 289 |
DWORD* pdwCheckSum = GetCommonMemberFromPEOptHdr(m_ntHeaders->OptionalHeader, CheckSum); |
| 290 |
if (*pdwCheckSum) |
| 291 |
{ |
| 292 |
// clear checksum (should be [re]calculated after all changes done) |
| 293 |
pdwCheckSum = 0; |
| 294 |
//throw runtime_error("CResourceEditor doesn't yet support check sum"); |
| 295 |
} |
| 296 |
|
| 297 |
// Get resource section virtual address, resource section index and pointer to resource directory |
| 298 |
PRESOURCE_DIRECTORY rdRoot = GetResourceDirectory(m_pbPE, iSize, m_ntHeaders, &m_dwResourceSectionVA, &m_dwResourceSectionIndex); |
| 299 |
|
| 300 |
// Scan the resource directory |
| 301 |
m_cResDir = ScanDirectory(rdRoot, rdRoot); |
| 302 |
} |
| 303 |
|
| 304 |
CResourceEditor::~CResourceEditor() { |
| 305 |
delete m_cResDir; |
| 306 |
} |
| 307 |
|
| 308 |
////////////////////////////////////////////////////////////////////// |
| 309 |
// Methods |
| 310 |
////////////////////////////////////////////////////////////////////// |
| 311 |
|
| 312 |
bool CResourceEditor::CanOpen(const void*Data, size_t Size) |
| 313 |
{ |
| 314 |
return 'P' == GetExeType(Data, Size); // We only understand PE, not NE/LE |
| 315 |
} |
| 316 |
|
| 317 |
CResourceDataEntry* CResourceEditor::FindResourceW(const WINWCHAR*RT, const WINWCHAR*RN, LANGID RL, CResourceDirectoryEntry**ppTE, CResourceDirectoryEntry**ppNE, CResourceDirectoryEntry**ppLE) const { |
| 318 |
int i = m_cResDir->Find(RT); |
| 319 |
CResourceDirectoryEntry*pTDE = -1 != i ? m_cResDir->GetEntry(i) : 0, *pNDE, *pLDE; |
| 320 |
if (pTDE) { |
| 321 |
CResourceDirectory *pND = pTDE->GetSubDirectory(); |
| 322 |
i = FIRSTRESDIRSTRADDRESS == RN ? 0 : pND->Find(RN); |
| 323 |
if ((pNDE = (-1 != i) ? pND->GetEntry(i) : 0)) { |
| 324 |
CResourceDirectory *pLD = pNDE->GetSubDirectory(); |
| 325 |
i = ANYLANGID == RL ? 0 : pLD->Find(RL); |
| 326 |
if ((pLDE = (-1 != i) ? pLD->GetEntry(i) : 0)) { |
| 327 |
if (ppTE) *ppTE = pTDE; |
| 328 |
if (ppNE) *ppNE = pNDE; |
| 329 |
if (ppLE) *ppLE = pLDE; |
| 330 |
return pLDE->GetDataEntry(); |
| 331 |
} |
| 332 |
} |
| 333 |
} |
| 334 |
return 0; |
| 335 |
} |
| 336 |
|
| 337 |
CResourceDataEntry* CResourceEditor::FindResourceT(const TCHAR*RT, const TCHAR*RN, LANGID RL, CResourceDirectoryEntry**ppTE, CResourceDirectoryEntry**ppNE, CResourceDirectoryEntry**ppLE) const { |
| 338 |
return FindResourceW(UTF16LEResString(RT), UTF16LEResString(RN), RL, ppTE, ppNE, ppLE); |
| 339 |
} |
| 340 |
|
| 341 |
CResourceDirectoryEntry* CResourceEditor::FindResourceLanguageDirEntryW(const WINWCHAR* RT, const WINWCHAR* RN, LANGID RL) const { |
| 342 |
CResourceDirectoryEntry*pLDE = 0; |
| 343 |
FindResourceW(RT, RN, RL, 0, 0, &pLDE); |
| 344 |
return pLDE; |
| 345 |
} |
| 346 |
|
| 347 |
CResourceDataEntry* CResourceEditor::FindResource(const WINWCHAR* RT, const WINWCHAR* RN, LANGID RL) const { |
| 348 |
CResourceDirectoryEntry*pDE = FindResourceLanguageDirEntryW(RT, RN, RL); |
| 349 |
return pDE ? pDE->GetDataEntry() : 0; |
| 350 |
} |
| 351 |
|
| 352 |
template<class T> static void UpdateManipulationType(CResourceEditor::TYPEMANIPULATION &Manip, const T* szType, const void*Data, size_t Size) { |
| 353 |
WORD dependenttype = GetDependentType(szType); |
| 354 |
if (Manip == CResourceEditor::TM_AUTO) |
| 355 |
if (IsIcoCurSingleImageType(dependenttype) && IsICOCURFile(Data, Size)) |
| 356 |
Manip = CResourceEditor::TM_ICONFILE; |
| 357 |
} |
| 358 |
|
| 359 |
// Adds/Replaces/Removes a simple resource. |
| 360 |
// If lpData is 0 UpdateResource removes the resource. |
| 361 |
bool CResourceEditor::UpdateResourceW(const WINWCHAR* szType, WINWCHAR* szName, LANGID wLanguage, BYTE* lpData, DWORD dwSize, TYPEMANIPULATION Manip) { |
| 362 |
CResourceDirectory* nameDir = 0; |
| 363 |
CResourceDirectory* langDir = 0; |
| 364 |
CResourceDataEntry* data = 0; |
| 365 |
IMAGE_RESOURCE_DIRECTORY rd = {0, /*time(0),*/}; |
| 366 |
int iTypeIdx = -1, iNameIdx = -1, iLangIdx = -1; |
| 367 |
|
| 368 |
iTypeIdx = m_cResDir->Find(szType); |
| 369 |
if (iTypeIdx > -1) { |
| 370 |
nameDir = m_cResDir->GetEntry(iTypeIdx)->GetSubDirectory(); |
| 371 |
iNameIdx = nameDir->Find(szName); |
| 372 |
if (iNameIdx > -1) { |
| 373 |
langDir = nameDir->GetEntry(iNameIdx)->GetSubDirectory(); |
| 374 |
iLangIdx = langDir->Find(wLanguage); |
| 375 |
if (iLangIdx > -1) { |
| 376 |
data = langDir->GetEntry(iLangIdx)->GetDataEntry(); |
| 377 |
} |
| 378 |
} |
| 379 |
} |
| 380 |
|
| 381 |
bool deleteoperation = !lpData, success = true, handlecomplexicon = false; |
| 382 |
UpdateManipulationType(Manip, szType, lpData, dwSize); |
| 383 |
WORD dependenttype = GetDependentType(szType); |
| 384 |
|
| 385 |
if (dependenttype && Manip != TM_RAW) { |
| 386 |
if (Manip == TM_AUTO && IsIcoCurSingleImageType(dependenttype)) |
| 387 |
Manip = TM_ICON; // A non-TM_ICONFILE operation that is probably going to fail |
| 388 |
|
| 389 |
if (Manip & TM_ICON) |
| 390 |
handlecomplexicon = true; // Group and images |
| 391 |
|
| 392 |
if (handlecomplexicon && !deleteoperation) |
| 393 |
if (Manip == TM_AUTO || (Manip & TM_ICONRSRC)) |
| 394 |
return false; // It is impossible to add a icon from a resource-based plain data buffer because it doesn't use offsets for the images |
| 395 |
|
| 396 |
if ((size_t) szType == (size_t) RT_GROUP_ICON && (size_t) szName == (size_t) IDI_ICON2) |
| 397 |
return false; // The main icon is special, don't allow high-level RT_GROUP_ICON updates to touch RT_ICON. |
| 398 |
} |
| 399 |
|
| 400 |
if (lpData && Manip == TM_AUTO && (size_t) szType == (size_t) RT_BITMAP) |
| 401 |
{ |
| 402 |
if (IsBMPFile(lpData, dwSize)) |
| 403 |
{ |
| 404 |
lpData += 14, dwSize -= 14; // Remove BITMAPFILEHEADER (blogs.msdn.microsoft.com/oldnewthing/20091211-00/?p=15693#) |
| 405 |
Manip = TM_RAW; |
| 406 |
} |
| 407 |
} |
| 408 |
|
| 409 |
if (!deleteoperation) { |
| 410 |
// Replace/Add the resource |
| 411 |
if (handlecomplexicon) { |
| 412 |
if (data) |
| 413 |
DeleteIconImagesW(szType, szName, wLanguage); // Delete the RT_ICONs that belong to the RT_GROUP_ICON we are replacing |
| 414 |
return AddExtraIconFromFile(szType, szName, wLanguage, lpData, dwSize); // Add RT_GROUP_ICON and RT_ICONs |
| 415 |
} |
| 416 |
|
| 417 |
if (data) { |
| 418 |
data->SetData(lpData, dwSize); |
| 419 |
return true; |
| 420 |
} |
| 421 |
|
| 422 |
if (!nameDir) { |
| 423 |
// Type doesn't yet exist |
| 424 |
nameDir = new CResourceDirectory(&rd); |
| 425 |
CResourceDirectoryEntry *pRDE = new CResourceDirectoryEntry(szType, nameDir); |
| 426 |
if (!m_cResDir->AddEntry(pRDE)) delete pRDE; |
| 427 |
} |
| 428 |
if (!langDir) { |
| 429 |
// Name doesn't yet exist |
| 430 |
langDir = new CResourceDirectory(&rd); |
| 431 |
CResourceDirectoryEntry *pRDE = new CResourceDirectoryEntry(szName, langDir); |
| 432 |
if (!nameDir->AddEntry(pRDE)) delete pRDE; |
| 433 |
} |
| 434 |
if (!data) { |
| 435 |
// Language doesn't yet exist, hence data neither |
| 436 |
data = new CResourceDataEntry(lpData, dwSize); |
| 437 |
CResourceDirectoryEntry *pRDE = new CResourceDirectoryEntry(MAKEINTRESOURCEWINW(wLanguage), data); |
| 438 |
if (!langDir->AddEntry(pRDE)) delete pRDE; |
| 439 |
} |
| 440 |
} |
| 441 |
else if (data) { |
| 442 |
if (handlecomplexicon) { |
| 443 |
success = DeleteIconImagesW(szType, szName, wLanguage); // Delete the RT_ICONs that belong to the RT_GROUP_ICON we are deleting |
| 444 |
} |
| 445 |
// Delete the resource |
| 446 |
delete data; |
| 447 |
langDir->RemoveEntry(iLangIdx); |
| 448 |
// Delete directories holding the resource if empty |
| 449 |
if (!langDir->CountEntries()) { |
| 450 |
delete langDir; |
| 451 |
nameDir->RemoveEntry(iNameIdx); |
| 452 |
if (!nameDir->CountEntries()) { |
| 453 |
delete nameDir; |
| 454 |
m_cResDir->RemoveEntry(iTypeIdx); |
| 455 |
} |
| 456 |
} |
| 457 |
} |
| 458 |
else |
| 459 |
success = false; |
| 460 |
return success; |
| 461 |
} |
| 462 |
|
| 463 |
CResourceDirectoryEntry* CResourceEditor::FindResourceLanguageDirEntryT(const TCHAR* RT, const TCHAR* RN, LANGID RL) const { |
| 464 |
assert(!EditorSupportsStringNames() && sizeof(RN)); |
| 465 |
#if defined(_WIN32) && defined(_UNICODE) |
| 466 |
return FindResourceLanguageDirEntryW((WINWCHAR*)RT, (WINWCHAR*)RN, RL); |
| 467 |
#else |
| 468 |
WINWCHAR* szwType = ResStringToUnicode(RT); |
| 469 |
CResourceDirectoryEntry* result = FindResourceLanguageDirEntryW(szwType, (WINWCHAR*)RN, RL); |
| 470 |
FreeUnicodeResString(szwType); |
| 471 |
return result; |
| 472 |
#endif |
| 473 |
} |
| 474 |
|
| 475 |
bool CResourceEditor::UpdateResourceT(const TCHAR* szType, WORD szName, LANGID wLanguage, BYTE* lpData, DWORD dwSize, TYPEMANIPULATION Manip) { |
| 476 |
assert(!EditorSupportsStringNames() && sizeof(szName)); |
| 477 |
#if defined(_WIN32) && defined(_UNICODE) |
| 478 |
return UpdateResourceW((WINWCHAR*)szType, MAKEINTRESOURCEWINW(szName), wLanguage, lpData, dwSize, Manip); |
| 479 |
#else |
| 480 |
WINWCHAR* szwType = ResStringToUnicode(szType); |
| 481 |
bool result = UpdateResourceW(szwType, MAKEINTRESOURCEWINW(szName), wLanguage, lpData, dwSize, Manip); |
| 482 |
FreeUnicodeResString(szwType); |
| 483 |
return result; |
| 484 |
#endif |
| 485 |
} |
| 486 |
|
| 487 |
bool CResourceEditor::UpdateResourceT(const TCHAR* szType, WORD szName, LANGID wLanguage, FILE*Data, TYPEMANIPULATION Manip) { |
| 488 |
assert(!EditorSupportsStringNames() && sizeof(szName)); |
| 489 |
bool result = false; |
| 490 |
unsigned long size; |
| 491 |
BYTE *data = alloc_and_read_file(Data, size); |
| 492 |
if (!data) |
| 493 |
return false; |
| 494 |
result = UpdateResourceT(szType, szName, wLanguage, data, size, Manip); |
| 495 |
free(data); |
| 496 |
return result; |
| 497 |
} |
| 498 |
|
| 499 |
bool CResourceEditor::DeleteResourceT(const TCHAR* szType, WORD szName, LANGID wLanguage, TYPEMANIPULATION Manip) { |
| 500 |
if (wLanguage != ALLLANGID) |
| 501 |
return UpdateResourceT(szType, szName, wLanguage, 0, 0, Manip); |
| 502 |
|
| 503 |
assert(!EditorSupportsStringNames() && sizeof(szName)); |
| 504 |
const TCHAR *name = (const TCHAR*) MAKEINTRESOURCE(szName); |
| 505 |
unsigned int deleted = 0; |
| 506 |
for (;; ++deleted) { |
| 507 |
CResourceDirectoryEntry*pDir = FindResourceLanguageDirEntryT(szType, name, ANYLANGID); |
| 508 |
if (!pDir || !UpdateResourceT(szType, szName, pDir->GetId(), 0, 0, Manip)) break; |
| 509 |
} |
| 510 |
return deleted != 0; |
| 511 |
} |
| 512 |
|
| 513 |
// Returns a copy of the requested resource |
| 514 |
// Returns 0 if the requested resource can't be found |
| 515 |
BYTE* CResourceEditor::GetResourceW(const WINWCHAR* szType, WINWCHAR* szName, LANGID wLanguage) { |
| 516 |
CResourceDataEntry* data = FindResource(szType, szName, wLanguage); |
| 517 |
return DupData(data); |
| 518 |
} |
| 519 |
|
| 520 |
BYTE* CResourceEditor::GetResourceT(const TCHAR* szType, WORD szName, LANGID wLanguage) { |
| 521 |
assert(!EditorSupportsStringNames() && sizeof(szName)); |
| 522 |
#if defined(_WIN32) && defined(_UNICODE) |
| 523 |
return GetResourceW((WINWCHAR*)szType, MAKEINTRESOURCEWINW(szName), wLanguage); |
| 524 |
#else |
| 525 |
WINWCHAR* szwType = ResStringToUnicode(szType); |
| 526 |
BYTE* result = GetResourceW(szwType, MAKEINTRESOURCEWINW(szName), wLanguage); |
| 527 |
FreeUnicodeResString(szwType); |
| 528 |
return result; |
| 529 |
#endif |
| 530 |
} |
| 531 |
|
| 532 |
// Returns the size of the requested resource |
| 533 |
// Returns -1 if the requested resource can't be found |
| 534 |
int CResourceEditor::GetResourceSizeW(const WINWCHAR* szType, WINWCHAR* szName, LANGID wLanguage) { |
| 535 |
CResourceDataEntry* data = FindResource(szType, szName, wLanguage); |
| 536 |
return data ? data->GetSize() : -1; |
| 537 |
} |
| 538 |
|
| 539 |
int CResourceEditor::GetResourceSizeT(const TCHAR* szType, WORD szName, LANGID wLanguage) { |
| 540 |
assert(!EditorSupportsStringNames()); |
| 541 |
#if defined(_WIN32) && defined(_UNICODE) |
| 542 |
return GetResourceSizeW((WINWCHAR*)szType, MAKEINTRESOURCEWINW(szName), wLanguage); |
| 543 |
#else |
| 544 |
WINWCHAR* szwType = ResStringToUnicode(szType); |
| 545 |
int result = GetResourceSizeW(szwType, MAKEINTRESOURCEWINW(szName), wLanguage); |
| 546 |
FreeUnicodeResString(szwType); |
| 547 |
return result; |
| 548 |
#endif |
| 549 |
} |
| 550 |
|
| 551 |
bool CResourceEditor::ResourceExistsT(const TCHAR* szType, WORD szName, LANGID wLanguage, LANGID*pFoundLanguage) { |
| 552 |
assert(!EditorSupportsStringNames() && sizeof(szName)); |
| 553 |
const TCHAR *name = (const TCHAR*) MAKEINTRESOURCE(szName); |
| 554 |
if (wLanguage == ALLLANGID) wLanguage = ANYLANGID; |
| 555 |
CResourceDirectoryEntry *pRDE = FindResourceLanguageDirEntryT(szType, name, wLanguage); |
| 556 |
if (pFoundLanguage) *pFoundLanguage = pRDE ? pRDE->GetId() : INVALIDLANGID; |
| 557 |
return pRDE != 0; |
| 558 |
} |
| 559 |
|
| 560 |
// Returns the offset of the requested resource in the original PE |
| 561 |
// Returns -1 if the requested resource can't be found |
| 562 |
DWORD CResourceEditor::GetResourceOffsetW(const WINWCHAR* szType, WINWCHAR* szName, LANGID wLanguage) { |
| 563 |
CResourceDataEntry* data = FindResource(szType, szName, wLanguage); |
| 564 |
return data ? data->GetOffset() : DWORD(-1); |
| 565 |
} |
| 566 |
|
| 567 |
DWORD CResourceEditor::GetResourceOffsetT(const TCHAR* szType, WORD szName, LANGID wLanguage) { |
| 568 |
assert(!EditorSupportsStringNames() && sizeof(szName)); |
| 569 |
#if defined(_WIN32) && defined(_UNICODE) |
| 570 |
return GetResourceOffsetW((WINWCHAR*)szType, MAKEINTRESOURCEWINW(szName), wLanguage); |
| 571 |
#else |
| 572 |
WINWCHAR* szwType = ResStringToUnicode(szType); |
| 573 |
DWORD result = GetResourceOffsetW(szwType, MAKEINTRESOURCEWINW(szName), wLanguage); |
| 574 |
FreeUnicodeResString(szwType); |
| 575 |
return result; |
| 576 |
#endif |
| 577 |
} |
| 578 |
|
| 579 |
// Returns a copy of the resource data from the first resource of a specific type |
| 580 |
BYTE* CResourceEditor::GetFirstResourceW(const WINWCHAR* szType, size_t&cbData) { |
| 581 |
CResourceDataEntry *pDE = FindResource(szType, FIRSTRESDIRSTRADDRESS, ANYLANGID); |
| 582 |
if (pDE) |
| 583 |
{ |
| 584 |
cbData = pDE->GetSize(); |
| 585 |
return DupData(pDE); |
| 586 |
} |
| 587 |
return NULL; |
| 588 |
} |
| 589 |
|
| 590 |
BYTE* CResourceEditor::GetFirstResourceT(const TCHAR* szType, size_t&cbData) { |
| 591 |
#if defined(_WIN32) && defined(_UNICODE) |
| 592 |
return GetFirstResourceW((WINWCHAR*)szType, cbData); |
| 593 |
#else |
| 594 |
WINWCHAR* szwType = ResStringToUnicode(szType); |
| 595 |
BYTE* result = GetFirstResourceW(szwType, cbData); |
| 596 |
FreeUnicodeResString(szwType); |
| 597 |
return result; |
| 598 |
#endif |
| 599 |
} |
| 600 |
|
| 601 |
BYTE* CResourceEditor::DupData(CResourceDataEntry*pDE) { |
| 602 |
if (!m_bKeepData) |
| 603 |
throw runtime_error("Can't get resource data when bKeepData is false"); |
| 604 |
if (pDE) |
| 605 |
{ |
| 606 |
size_t cb = pDE->GetSize(); |
| 607 |
BYTE* p = new BYTE[cb]; // Free with FreeResource() |
| 608 |
if (p) CopyMemory(p, pDE->GetData(), cb); |
| 609 |
return p; |
| 610 |
} |
| 611 |
return NULL; |
| 612 |
} |
| 613 |
|
| 614 |
void CResourceEditor::FreeResource(BYTE* pbResource) |
| 615 |
{ |
| 616 |
if (pbResource) |
| 617 |
delete [] pbResource; |
| 618 |
} |
| 619 |
|
| 620 |
// Saves the edited PE into a buffer and returns it. |
| 621 |
DWORD CResourceEditor::Save(BYTE* pbBuf, DWORD &dwSize) { |
| 622 |
if (!m_bKeepData) |
| 623 |
throw runtime_error("Can't Save() when bKeepData is false"); |
| 624 |
|
| 625 |
unsigned int i; |
| 626 |
DWORD dwReqSize, temp32; |
| 627 |
|
| 628 |
temp32 = *GetCommonMemberFromPEOptHdr(m_ntHeaders->OptionalHeader, FileAlignment); |
| 629 |
const DWORD dwFileAlign = ConvertEndianness(temp32); |
| 630 |
temp32 = *GetCommonMemberFromPEOptHdr(m_ntHeaders->OptionalHeader, SectionAlignment); |
| 631 |
const DWORD dwSecAlign = ConvertEndianness(temp32); |
| 632 |
|
| 633 |
DWORD dwRsrcSize = m_cResDir->GetSize(); // Size of new resource section |
| 634 |
DWORD dwRsrcSizeAligned = RALIGN(dwRsrcSize, dwFileAlign); // Align it to FileAlignment |
| 635 |
|
| 636 |
// Calculate the total new PE size |
| 637 |
DWORD dwOldRsrcSize = ConvertEndianness(IMAGE_FIRST_SECTION(m_ntHeaders)[m_dwResourceSectionIndex].SizeOfRawData); |
| 638 |
dwReqSize = m_iSize - dwOldRsrcSize + dwRsrcSizeAligned; |
| 639 |
|
| 640 |
if (!pbBuf || dwSize < dwReqSize) |
| 641 |
return dwReqSize; |
| 642 |
|
| 643 |
// Use buffer |
| 644 |
BYTE* pbNewPE = pbBuf; |
| 645 |
dwSize = dwReqSize; |
| 646 |
// Fill buffer with zeros |
| 647 |
ZeroMemory(pbNewPE, dwSize); |
| 648 |
|
| 649 |
BYTE* seeker = pbNewPE; |
| 650 |
BYTE* oldSeeker = m_pbPE; |
| 651 |
|
| 652 |
PIMAGE_SECTION_HEADER old_sectionHeadersArray = IMAGE_FIRST_SECTION(m_ntHeaders); |
| 653 |
|
| 654 |
DWORD dwHeaderSize = ConvertEndianness(old_sectionHeadersArray[m_dwResourceSectionIndex].PointerToRawData); |
| 655 |
WORD wNumberOfSections = ConvertEndianness(m_ntHeaders->FileHeader.NumberOfSections); |
| 656 |
|
| 657 |
// Copy everything until the resource section (including headers and everything that might come after them) |
| 658 |
// We don't use SizeOfHeaders because sometimes (using VC6) it can extend beyond the first section |
| 659 |
// or (Borland) there could be some more information between the headers and the first section. |
| 660 |
CopyMemory(seeker, oldSeeker, dwHeaderSize); |
| 661 |
|
| 662 |
// Skip the headers and whatever comes after them |
| 663 |
seeker += dwHeaderSize; |
| 664 |
oldSeeker += dwHeaderSize; |
| 665 |
|
| 666 |
// Get new nt headers pointer |
| 667 |
PIMAGE_NT_HEADERS ntHeaders = GetNTHeaders(pbNewPE); |
| 668 |
// Get a pointer to the new section headers |
| 669 |
PIMAGE_SECTION_HEADER sectionHeadersArray = IMAGE_FIRST_SECTION(ntHeaders); |
| 670 |
|
| 671 |
// Skip the resource section in the old PE seeker. |
| 672 |
oldSeeker += dwOldRsrcSize; |
| 673 |
|
| 674 |
// Save the old virtual size of the resource section |
| 675 |
DWORD dwNewVirtualSize = RALIGN(dwRsrcSize, dwSecAlign); |
| 676 |
DWORD dwOldVirtualSize = ConvertEndianness(sectionHeadersArray[m_dwResourceSectionIndex].Misc.VirtualSize); |
| 677 |
ALIGN(dwOldVirtualSize, dwSecAlign); |
| 678 |
DWORD dwVAAdjustment = dwNewVirtualSize - dwOldVirtualSize; |
| 679 |
|
| 680 |
// Set the new size of the resource section (size aligned to FileAlignment) |
| 681 |
sectionHeadersArray[m_dwResourceSectionIndex].SizeOfRawData = ConvertEndianness(dwRsrcSizeAligned); |
| 682 |
// Set the virtual size as well (in memory) |
| 683 |
sectionHeadersArray[m_dwResourceSectionIndex].Misc.VirtualSize = ConvertEndianness(dwRsrcSize); |
| 684 |
(*GetMemberFromPEOptHdr(ntHeaders->OptionalHeader, DataDirectory))[IMAGE_DIRECTORY_ENTRY_RESOURCE].Size = ConvertEndianness(dwRsrcSize); |
| 685 |
|
| 686 |
// Set the new virtual size of the image |
| 687 |
DWORD* pdwSizeOfImage = GetCommonMemberFromPEOptHdr(ntHeaders->OptionalHeader, SizeOfImage); |
| 688 |
*pdwSizeOfImage = AlignVA(*GetCommonMemberFromPEOptHdr(ntHeaders->OptionalHeader, SizeOfHeaders)); |
| 689 |
for (i = 0; i < wNumberOfSections; i++) { |
| 690 |
DWORD dwSecSize = ConvertEndianness(sectionHeadersArray[i].Misc.VirtualSize); |
| 691 |
*pdwSizeOfImage = AlignVA(AdjustVA(*pdwSizeOfImage, dwSecSize)); |
| 692 |
} |
| 693 |
|
| 694 |
// Set the new AddressOfEntryPoint if needed |
| 695 |
DWORD* pdwAddressOfEntryPoint = GetCommonMemberFromPEOptHdr(ntHeaders->OptionalHeader, AddressOfEntryPoint); |
| 696 |
if (ConvertEndianness(*pdwAddressOfEntryPoint) > m_dwResourceSectionVA) |
| 697 |
*pdwAddressOfEntryPoint = AdjustVA(*pdwAddressOfEntryPoint, dwVAAdjustment); |
| 698 |
|
| 699 |
// Set the new BaseOfCode if needed |
| 700 |
DWORD* pdwBaseOfCode = GetCommonMemberFromPEOptHdr(ntHeaders->OptionalHeader, BaseOfCode); |
| 701 |
if (ConvertEndianness(*pdwBaseOfCode) > m_dwResourceSectionVA) |
| 702 |
*pdwBaseOfCode = AdjustVA(*pdwBaseOfCode, dwVAAdjustment); |
| 703 |
|
| 704 |
// Set the new BaseOfData if needed |
| 705 |
if (*GetCommonMemberFromPEOptHdr(ntHeaders->OptionalHeader, Magic) == IMAGE_NT_OPTIONAL_HDR32_MAGIC) { |
| 706 |
DWORD* pdwBaseOfData = &((PIMAGE_OPTIONAL_HEADER32)&ntHeaders->OptionalHeader)->BaseOfData; |
| 707 |
if (ConvertEndianness(*pdwBaseOfData) > m_dwResourceSectionVA) |
| 708 |
*pdwBaseOfData = AdjustVA(*pdwBaseOfData, dwVAAdjustment); |
| 709 |
} |
| 710 |
|
| 711 |
// Refresh the headers of the sections that come after the resource section, and the data directory |
| 712 |
DWORD dwNumberOfRvaAndSizes = *GetMemberFromPEOptHdr(ntHeaders->OptionalHeader, NumberOfRvaAndSizes); |
| 713 |
PIMAGE_DATA_DIRECTORY pDataDirectory = *GetMemberFromPEOptHdr(ntHeaders->OptionalHeader, DataDirectory); |
| 714 |
for (i = m_dwResourceSectionIndex + 1; i < wNumberOfSections; i++) { |
| 715 |
if (sectionHeadersArray[i].PointerToRawData) { |
| 716 |
AdjustVA(sectionHeadersArray[i].PointerToRawData, dwRsrcSizeAligned - dwOldRsrcSize); |
| 717 |
} |
| 718 |
|
| 719 |
// We must find the right data directory entry before we change the virtual address |
| 720 |
unsigned int uDataDirIdx = 0; |
| 721 |
for (unsigned int j = 0; j < ConvertEndianness(dwNumberOfRvaAndSizes); j++) |
| 722 |
if (pDataDirectory[j].VirtualAddress == sectionHeadersArray[i].VirtualAddress) |
| 723 |
uDataDirIdx = j; |
| 724 |
|
| 725 |
sectionHeadersArray[i].VirtualAddress = AdjustVA(sectionHeadersArray[i].VirtualAddress, dwVAAdjustment); |
| 726 |
|
| 727 |
// Change the virtual address in the data directory too |
| 728 |
if (uDataDirIdx) |
| 729 |
pDataDirectory[uDataDirIdx].VirtualAddress = sectionHeadersArray[i].VirtualAddress; |
| 730 |
} |
| 731 |
|
| 732 |
// Write the resource section |
| 733 |
WriteRsrcSec(seeker); |
| 734 |
// Advance the pointer |
| 735 |
seeker += dwRsrcSizeAligned; |
| 736 |
|
| 737 |
// Copy everything that comes after the resource section (other sections and tacked data) |
| 738 |
size_t cbLeft = m_iSize - (oldSeeker - m_pbPE); |
| 739 |
if (cbLeft) |
| 740 |
CopyMemory(seeker, oldSeeker, cbLeft); |
| 741 |
|
| 742 |
seeker += cbLeft; |
| 743 |
oldSeeker += cbLeft; |
| 744 |
|
| 745 |
/********************************************************** |
| 746 |
* To add checksum to the header use MapFileAndCheckSum |
| 747 |
**********************************************************/ |
| 748 |
|
| 749 |
// From now on, we are working on the new PE |
| 750 |
// Freeing the old PE memory is up to the user |
| 751 |
m_pbPE = pbNewPE; |
| 752 |
m_iSize = dwSize; |
| 753 |
m_ntHeaders = ntHeaders; |
| 754 |
// We just wrote the resource section according to m_cResDir, so we don't need to rescan |
| 755 |
// m_dwResourceSectionIndex and m_dwResourceSectionVA have also been left unchanged as |
| 756 |
// we didn't move the resources section |
| 757 |
|
| 758 |
return 0; |
| 759 |
} |
| 760 |
|
| 761 |
// This function scans exe sections and after find a match with given name |
| 762 |
// increments it's virtual size (auto fixes image size based on section alignment, etc) |
| 763 |
// Jim Park: The section name must be ASCII code. Do not TCHAR this stuff. |
| 764 |
bool CResourceEditor::SetPESectionVirtualSize(const char* pszSectionName, DWORD newsize) |
| 765 |
{ |
| 766 |
PIMAGE_SECTION_HEADER sectionHeadersArray = IMAGE_FIRST_SECTION(m_ntHeaders); |
| 767 |
|
| 768 |
// Refresh the headers of the sections that come after the resource section, and the data directory |
| 769 |
for (int i = 0; i < ConvertEndianness(m_ntHeaders->FileHeader.NumberOfSections); i++) { |
| 770 |
if (!strcmp((LPCSTR)sectionHeadersArray[i].Name, pszSectionName)) { |
| 771 |
sectionHeadersArray[i].Misc.VirtualSize = AlignVA(ConvertEndianness(newsize)); |
| 772 |
|
| 773 |
sectionHeadersArray[i].Characteristics &= ConvertEndianness((DWORD) ~IMAGE_SCN_MEM_DISCARDABLE); |
| 774 |
|
| 775 |
// now fix any section after |
| 776 |
for (int k = i + 1; k < ConvertEndianness(m_ntHeaders->FileHeader.NumberOfSections); k++, i++) { |
| 777 |
DWORD dwLastSecVA = ConvertEndianness(sectionHeadersArray[i].VirtualAddress); |
| 778 |
DWORD dwLastSecSize = ConvertEndianness(sectionHeadersArray[i].Misc.VirtualSize); |
| 779 |
DWORD dwSecVA = AlignVA(ConvertEndianness(dwLastSecVA + dwLastSecSize)); |
| 780 |
|
| 781 |
sectionHeadersArray[k].VirtualAddress = dwSecVA; |
| 782 |
|
| 783 |
if (m_dwResourceSectionIndex == (DWORD) k) |
| 784 |
{ |
| 785 |
// fix the resources virtual address if it changed |
| 786 |
PIMAGE_DATA_DIRECTORY pDataDirectory = *GetMemberFromPEOptHdr(m_ntHeaders->OptionalHeader, DataDirectory); |
| 787 |
pDataDirectory[IMAGE_DIRECTORY_ENTRY_RESOURCE].VirtualAddress = dwSecVA; |
| 788 |
m_dwResourceSectionVA = ConvertEndianness(dwSecVA); |
| 789 |
} |
| 790 |
} |
| 791 |
|
| 792 |
return true; |
| 793 |
} |
| 794 |
} |
| 795 |
|
| 796 |
return false; |
| 797 |
} |
| 798 |
|
| 799 |
static char* GetFirstICOCURGroupEntry(const void*headerdata, WORD*pImgType, WORD*pCount) { |
| 800 |
WORD *p16 = (WORD*) headerdata; |
| 801 |
if (IsICOCURFile(headerdata)) { // The first 6+ bytes of the resource format matches the file format |
| 802 |
if (pImgType) |
| 803 |
*pImgType = FIX_ENDIAN_INT16(p16[1]) == 2 ? (WORD)(size_t) RT_CURSOR : (WORD)(size_t) RT_ICON; |
| 804 |
if (pCount) |
| 805 |
*pCount = FIX_ENDIAN_INT16(p16[2]); |
| 806 |
return ((char*) headerdata) + 6; |
| 807 |
} |
| 808 |
return 0; |
| 809 |
} |
| 810 |
|
| 811 |
bool CResourceEditor::DeleteIconImages(const CResourceDirectoryEntry& LangDir) { |
| 812 |
WORD imgType, count, *pRGE = (WORD*) GetFirstICOCURGroupEntry(LangDir.GetDataEntry()->GetData(), &imgType, &count); |
| 813 |
if (!pRGE) |
| 814 |
return false; |
| 815 |
for (unsigned int i = 0, cbRGE = 14; i < count; ++i, pRGE += cbRGE / sizeof(*pRGE)) |
| 816 |
if (!DeleteResource(MAKEINTRESOURCE(imgType), FIX_ENDIAN_INT16(pRGE[6]), LangDir.GetId())) |
| 817 |
return false; |
| 818 |
return true; |
| 819 |
} |
| 820 |
|
| 821 |
bool CResourceEditor::DeleteIconImagesW(const WINWCHAR* OwnerType, WINWCHAR* Name, LANGID LangId) { |
| 822 |
CResourceDirectoryEntry*pLangDir = FindResourceLanguageDirEntryW(OwnerType, Name, LangId); |
| 823 |
return pLangDir ? DeleteIconImages(*pLangDir) : false; |
| 824 |
} |
| 825 |
|
| 826 |
static WORD FindFreeIconImageId(CResourceEditor& re, WORD ImgType) { |
| 827 |
for (unsigned int i = MAIN_ICON_LAST_IMAGE + 1; i <= 0xffff; ++i) |
| 828 |
if (!re.ResourceExists(MAKEINTRESOURCE(ImgType), (WORD) i, CResourceEditor::ANYLANGID)) |
| 829 |
return (WORD) i; |
| 830 |
return 0; |
| 831 |
} |
| 832 |
|
| 833 |
typedef struct { BYTE Width, Height, Palette, Reserved; WORD Planes, BPP; UINT32 Size, Offset; } FILEICOGROUPENTRY; |
| 834 |
typedef struct { BYTE Width, Height, Palette, Reserved; WORD Planes, BPP, SizeLo, SizeHi, Id; } RSRCICOGROUPENTRY; |
| 835 |
typedef struct { WORD Width, Height; WORD Planes, BPP, SizeLo, SizeHi, Id; } RSRCCURGROUPENTRY; //msdn.microsoft.com/en-us/library/windows/desktop/ms648011(v=vs.85).aspx |
| 836 |
|
| 837 |
bool CResourceEditor::AddExtraIconFromFile(const WINWCHAR* Type, WINWCHAR* Name, LANGID LangId, BYTE* Data, DWORD Size) { |
| 838 |
assert(sizeof(RSRCICOGROUPENTRY) == 12+2 && sizeof(FILEICOGROUPENTRY) == 12+4); |
| 839 |
assert(sizeof(RSRCICOGROUPENTRY) == sizeof(RSRCCURGROUPENTRY)); |
| 840 |
|
| 841 |
WORD failed = 0, count, imgType, imgId; |
| 842 |
FILEICOGROUPENTRY *pSrcFGE = (FILEICOGROUPENTRY*) GetFirstICOCURGroupEntry(Data, &imgType, &count); |
| 843 |
if (!pSrcFGE) |
| 844 |
return false; |
| 845 |
|
| 846 |
unsigned int cbDstGrp = 6 + (count * sizeof(RSRCICOGROUPENTRY)); |
| 847 |
BYTE *pDstGrp = new BYTE[cbDstGrp], isCursor = MAKEINTRESOURCE(imgType) == RT_CURSOR; |
| 848 |
memcpy(pDstGrp, Data, 6); |
| 849 |
RSRCICOGROUPENTRY *pDstRGE = (RSRCICOGROUPENTRY*) (((char*) pDstGrp) + 6); |
| 850 |
|
| 851 |
for (unsigned int i = 0; i < count; ++i) { |
| 852 |
if (!(imgId = FindFreeIconImageId(*this, imgType))) |
| 853 |
goto fail; |
| 854 |
memcpy(&pDstRGE[i], &pSrcFGE[i], sizeof(RSRCICOGROUPENTRY)); // Copy the image information |
| 855 |
pDstRGE[i].Id = FIX_ENDIAN_INT16(imgId); // and assign the new resource id |
| 856 |
UINT32 imgSize = FIX_ENDIAN_INT32(pSrcFGE[i].Size); |
| 857 |
BYTE *pImg = (BYTE*) (((char*) Data) + FIX_ENDIAN_INT32(pSrcFGE[i].Offset)), *pCursor = 0; |
| 858 |
|
| 859 |
if (isCursor) { // We must prepend the hotspot to the image data and change the group entry |
| 860 |
assert(!EditorSupportsCursorPng()); |
| 861 |
GENERICIMAGEINFO info; |
| 862 |
if (/*!IsPNGFile(pImg, imgSize, &info) &&*/ !GetDIBHeaderInfo(pImg, imgSize, info)) // Are PNG cursor images allowed? |
| 863 |
goto fail; |
| 864 |
//if (info.IsTopDownBitmap() && isDib) |
| 865 |
// goto fail; // Are TopDown DIBs allowed? Probably not but we play it safe. |
| 866 |
|
| 867 |
typedef struct { WORD x, y; } CURSORIMGHDR; //msdn.microsoft.com/en-us/library/windows/desktop/ms648017(v=vs.85).aspx |
| 868 |
pCursor = new BYTE[imgSize + 4]; |
| 869 |
CURSORIMGHDR *pLH = (CURSORIMGHDR*) pCursor; |
| 870 |
pLH->x = pSrcFGE[i].Planes, pLH->y = pSrcFGE[i].BPP; |
| 871 |
memcpy(pCursor + sizeof(CURSORIMGHDR), pImg, imgSize); |
| 872 |
pImg = pCursor, imgSize += 4; // Our new image data is ready |
| 873 |
|
| 874 |
RSRCCURGROUPENTRY *pCGE = (RSRCCURGROUPENTRY*) &pDstRGE[i]; |
| 875 |
pCGE->Width = FIX_ENDIAN_INT16(info.Width > 0xffff ? 0 : (WORD) info.Width); |
| 876 |
pCGE->Height = FIX_ENDIAN_INT16(info.Height > 0xffff ? 0 : (WORD) info.Height); |
| 877 |
pCGE->Planes = FIX_ENDIAN_INT16(info.Planes), pCGE->BPP = FIX_ENDIAN_INT16(info.BPP); |
| 878 |
((FILEICOGROUPENTRY*)pCGE)->Size = FIX_ENDIAN_INT32(imgSize); // The size of the image data has changed |
| 879 |
} |
| 880 |
|
| 881 |
bool succ = UpdateResource(MAKEINTRESOURCE(imgType), imgId, LangId, pImg, imgSize); |
| 882 |
if (pCursor) |
| 883 |
delete [] pCursor; |
| 884 |
if (!succ) |
| 885 |
goto fail; |
| 886 |
} |
| 887 |
if (!UpdateResourceW(Type, Name, LangId, pDstGrp, cbDstGrp)) |
| 888 |
fail: ++failed; |
| 889 |
|
| 890 |
delete [] pDstGrp; |
| 891 |
return !failed; |
| 892 |
} |
| 893 |
|
| 894 |
template<class C, class P> static bool Contains(C&Map, P*p) { |
| 895 |
return p && (size_t) Map.base <= (size_t) p && (size_t) Map.base + Map.size > (size_t) p; |
| 896 |
} |
| 897 |
|
| 898 |
void CResourceEditor::FreeExternal(EXTERNAL&X) { |
| 899 |
assert(sizeof(FILEVIEW) <= sizeof(X.Map)); |
| 900 |
if (X.Data) { |
| 901 |
FILEVIEW &map = *(FILEVIEW*) &X.Map; |
| 902 |
if (!Contains(map, X.Data)) free(X.Data); |
| 903 |
close_file_view(map); |
| 904 |
free(X.FreeThis); // ParseResProtocolAlloc |
| 905 |
} |
| 906 |
} |
| 907 |
|
| 908 |
// Maps a file into memory and locates a resource inside it, a manipulated from a resource inside it or the file itself. Free with FreeExternal. |
| 909 |
const TCHAR* CResourceEditor::MapExternal(const TCHAR*File, TYPEMANIPULATION Manip, EXTERNAL&X) { |
| 910 |
TCHAR *resproto = ParseResProtocolAlloc(File, X.RT, X.RN, X.RL); |
| 911 |
if (resproto) { |
| 912 |
File = resproto; |
| 913 |
} |
| 914 |
|
| 915 |
FILEVIEW &map = *(FILEVIEW*) &X.Map; |
| 916 |
size_t datasize = 0; |
| 917 |
char *filedata = create_file_view_readonly(File, map), *data = 0, *dataalloc = 0; |
| 918 |
if (filedata) { |
| 919 |
if (resproto) { |
| 920 |
signed char exetype = GetExeType(filedata, map.size); |
| 921 |
if (exetype == 'P') { |
| 922 |
CResourceEditor re(filedata, (int) map.size); |
| 923 |
DWORD ofs, siz = 0, firstname = X.RN == (TCHAR*) FIRSTRESDIRSTRADDRESS; |
| 924 |
CResourceDirectoryEntry*pNRDE, *pLRDE; |
| 925 |
CResourceDataEntry*pRE = re.FindResourceT(X.RT, X.RN, X.RL, 0, &pNRDE, &pLRDE); |
| 926 |
if (pRE) { |
| 927 |
const WINWCHAR *wrn = pNRDE->GetNameOrId(); |
| 928 |
if (firstname) X.RN = IS_INTRESOURCE(wrn) ? (TCHAR*)(size_t) wrn : _T(""); |
| 929 |
X.RL = pLRDE->GetId(); |
| 930 |
ofs = pRE->GetOffset(), siz = pRE->GetSize(); |
| 931 |
} |
| 932 |
if (siz && IsIcoCurGroupType(X.RT) && (Manip == TM_AUTO || (Manip & TM_ICON))) { // Must create a fake .ico file |
| 933 |
data = dataalloc = (char*) re.ExtractIcoCur(*pRE, X.RL, datasize), siz = 0; |
| 934 |
} |
| 935 |
if (siz && siz != DWORD(-1)) { |
| 936 |
data = filedata + ofs, datasize = siz; // Raw resource data |
| 937 |
} |
| 938 |
} |
| 939 |
} |
| 940 |
else { |
| 941 |
data = filedata, datasize = map.size; // Just a normal file |
| 942 |
} |
| 943 |
} |
| 944 |
X.Data = (BYTE*) data, X.cbData = datasize; |
| 945 |
X.FreeThis = resproto; |
| 946 |
if (data) return File; |
| 947 |
FreeExternal(X); |
| 948 |
return 0; |
| 949 |
} |
| 950 |
|
| 951 |
bool CResourceEditor::UpdateResourceFromExternalT(const TCHAR* Type, WORD Name, LANGID Lang, const TCHAR*File, TYPEMANIPULATION Manip) { |
| 952 |
EXTERNAL x; |
| 953 |
size_t &datasize = x.cbData; |
| 954 |
bool success = false; |
| 955 |
const TCHAR *parsedpath = MapExternal(File, Manip, x); |
| 956 |
if (parsedpath) { |
| 957 |
if ((DWORD) datasize == datasize) { |
| 958 |
success = this->UpdateResource(Type, Name, Lang, (BYTE*) x.Data, (DWORD) datasize, Manip); |
| 959 |
} |
| 960 |
FreeExternal(x); |
| 961 |
} |
| 962 |
return success; |
| 963 |
} |
| 964 |
|
| 965 |
CResourceDataEntry* CResourceEditor::FindIcoCurDataEntry(WORD Type, WORD Id, LANGID PrefLang) const { |
| 966 |
CResourceDataEntry*pRDE = FindResource(MAKEINTRESOURCEWINW(Type), MAKEINTRESOURCEWINW(Id), PrefLang); |
| 967 |
return pRDE ? pRDE : FindResource(MAKEINTRESOURCEWINW(Type), MAKEINTRESOURCEWINW(Id), ANYLANGID); |
| 968 |
} |
| 969 |
|
| 970 |
BYTE* CResourceEditor::ExtractIcoCurW(const WINWCHAR* szType, const WINWCHAR* szName, LANGID wLanguage, size_t&cbData) const { |
| 971 |
CResourceDirectoryEntry*pLangDir = FindResourceLanguageDirEntryW(szType, szName, wLanguage); |
| 972 |
if (!pLangDir) |
| 973 |
return 0; |
| 974 |
CResourceDataEntry*pRDE = pLangDir->GetDataEntry(); |
| 975 |
return ExtractIcoCur(*pRDE, pLangDir->GetId(), cbData); // Uses the "real" LANGID |
| 976 |
} |
| 977 |
|
| 978 |
BYTE* CResourceEditor::ExtractIcoCur(const CResourceDataEntry&rde, LANGID ChildLang, size_t&cbData) const { |
| 979 |
const CResourceDataEntry*pRDE = &rde; |
| 980 |
BYTE*pSH = pRDE->GetData(), cbRGE = 14, cbFGE = 16, *pResData; |
| 981 |
DWORD i, cbRes, failed = false; |
| 982 |
if (pRDE->GetSize() < 6) // Must at least have a ICO file header |
| 983 |
return 0; |
| 984 |
WORD imgResType, count, *pFirstRGE = (WORD*) GetFirstICOCURGroupEntry(pSH, &imgResType, &count), *pRGE; |
| 985 |
if (!pFirstRGE) |
| 986 |
return 0; |
| 987 |
|
| 988 |
WORD *pDH = 0, isCursor = imgResType == (size_t) RT_CURSOR; |
| 989 |
DWORD imgsOfs = 6 + (count * cbFGE), cbTot = imgsOfs, cbImages = 0, grpsOfs = 6; |
| 990 |
|
| 991 |
// Get the size of all images |
| 992 |
for (i = 0, pRGE = pFirstRGE; i < count; ++i, pRGE += cbRGE / sizeof(*pRGE)) { |
| 993 |
pRDE = FindIcoCurDataEntry(imgResType, ((RSRCICOGROUPENTRY*)pRGE)->Id, ChildLang); |
| 994 |
if (pRDE && pRDE->GetData()) cbImages += FIX_ENDIAN_INT32(((FILEICOGROUPENTRY*)pRGE)->Size); else count = 0; |
| 995 |
} |
| 996 |
// Build the .ICO file |
| 997 |
GENERICIMAGEINFO ii; |
| 998 |
if (count && (pDH = (WORD*) malloc(cbTot += cbImages))) { |
| 999 |
pDH[0] = 0x0000, pDH[1] = FIX_ENDIAN_INT16(isCursor ? 2 : 1), pDH[2] = FIX_ENDIAN_INT16(count); |
| 1000 |
for (i = 0, pRGE = pFirstRGE; i < count; ++i, pRGE += cbRGE / sizeof(*pRGE)) { |
| 1001 |
pRDE = FindIcoCurDataEntry(imgResType, ((RSRCICOGROUPENTRY*)pRGE)->Id, ChildLang); |
| 1002 |
pResData = pRDE->GetData(), cbRes = pRDE->GetSize(); |
| 1003 |
FILEICOGROUPENTRY*pFGE = (FILEICOGROUPENTRY*) ((char*)pDH + grpsOfs); |
| 1004 |
memcpy(pFGE, pRGE, cbRGE), pFGE->Offset = FIX_ENDIAN_INT32(imgsOfs); // Initialize ICO group entry |
| 1005 |
DWORD cbImgFromGrp = FIX_ENDIAN_INT32(pFGE->Size), cbImg = cbImgFromGrp; |
| 1006 |
if (isCursor) { |
| 1007 |
pFGE->Width = (BYTE) FIX_ENDIAN_INT16(pRGE[0]), pFGE->Height = (BYTE) FIX_ENDIAN_INT16(pRGE[1]); |
| 1008 |
if (cbRes >= 4+12) { |
| 1009 |
assert(!EditorSupportsCursorPng()); |
| 1010 |
pFGE->Planes = ((WORD*)pResData)[0], pFGE->BPP = ((WORD*)pResData)[1], cbImg -= 4; // Hotspot |
| 1011 |
DWORD cbBMH = GetDIBHeaderInfo(pResData += 4, cbRes - 4, ii), cd = ii.BPP * ii.Planes; |
| 1012 |
pFGE->Palette = cbBMH && cd < 8 ? (BYTE)(1 << cd) : 0; // devblogs.microsoft.com/oldnewthing/20101018-00/?p=12513 says only for depths < 8! |
| 1013 |
pFGE->Reserved = 0; |
| 1014 |
} |
| 1015 |
else |
| 1016 |
++failed; |
| 1017 |
} |
| 1018 |
if (cbImg <= cbRes) { |
| 1019 |
memcpy((char*)pDH + imgsOfs, pResData, cbImg); |
| 1020 |
pFGE->Size = FIX_ENDIAN_INT32(cbImg); |
| 1021 |
imgsOfs += cbImg, grpsOfs += cbFGE; |
| 1022 |
} |
| 1023 |
else |
| 1024 |
++failed; |
| 1025 |
} |
| 1026 |
} |
| 1027 |
cbData = cbTot; |
| 1028 |
if (!count || failed) |
| 1029 |
free(pDH), pDH = 0; |
| 1030 |
return (BYTE*) pDH; |
| 1031 |
} |
| 1032 |
|
| 1033 |
BYTE* CResourceEditor::ExtractIcoCurT(const TCHAR* szType, WORD szName, LANGID wLanguage, size_t&cbData) const { |
| 1034 |
assert(!EditorSupportsStringNames() && sizeof(szName)); |
| 1035 |
#if defined(_WIN32) && defined(_UNICODE) |
| 1036 |
return ExtractIcoCurW((WINWCHAR*)szType, MAKEINTRESOURCEWINW(szName), wLanguage, cbData); |
| 1037 |
#else |
| 1038 |
WINWCHAR* szwType = ResStringToUnicode(szType); |
| 1039 |
BYTE* result = ExtractIcoCurW(szwType, MAKEINTRESOURCEWINW(szName), wLanguage, cbData); |
| 1040 |
FreeUnicodeResString(szwType); |
| 1041 |
return result; |
| 1042 |
#endif |
| 1043 |
} |
| 1044 |
|
| 1045 |
|
| 1046 |
////////////////////////////////////////////////////////////////////// |
| 1047 |
// Private Methods |
| 1048 |
////////////////////////////////////////////////////////////////////// |
| 1049 |
|
| 1050 |
// This function scans a given resource directory and returns a CResourceDirectory object |
| 1051 |
// rdRoot must point to the root directory of the resource section |
| 1052 |
CResourceDirectory* CResourceEditor::ScanDirectory(PRESOURCE_DIRECTORY rdRoot, PRESOURCE_DIRECTORY rdToScan) { |
| 1053 |
// Create CResourceDirectory from rdToScan |
| 1054 |
CResourceDirectory* rdc = new CResourceDirectory(PIMAGE_RESOURCE_DIRECTORY(rdToScan)); |
| 1055 |
WINWCHAR* szName; |
| 1056 |
PIMAGE_RESOURCE_DATA_ENTRY rde = NULL; |
| 1057 |
|
| 1058 |
// Go through all entries of this resource directory |
| 1059 |
int entries = ConvertEndianness(rdToScan->Header.NumberOfNamedEntries); |
| 1060 |
entries += ConvertEndianness(rdToScan->Header.NumberOfIdEntries); |
| 1061 |
|
| 1062 |
for (int i = 0; i < entries; i++) { |
| 1063 |
MY_IMAGE_RESOURCE_DIRECTORY_ENTRY rd = rdToScan->Entries[i]; |
| 1064 |
rd.UOffset.OffsetToData = ConvertEndianness(rd.UOffset.OffsetToData); |
| 1065 |
rd.UName.Name = ConvertEndianness(rd.UName.Name); |
| 1066 |
|
| 1067 |
// If this entry points to data entry get a pointer to it |
| 1068 |
if (!rd.UOffset.DirectoryOffset.DataIsDirectory) |
| 1069 |
rde = PIMAGE_RESOURCE_DATA_ENTRY(rd.UOffset.OffsetToData + (BYTE*)rdRoot); |
| 1070 |
|
| 1071 |
// If this entry has a name, translate it from Unicode |
| 1072 |
if (rd.UName.NameString.NameIsString) { |
| 1073 |
PIMAGE_RESOURCE_DIR_STRING_U rds = PIMAGE_RESOURCE_DIR_STRING_U(rd.UName.NameString.NameOffset + (char*)rdRoot); |
| 1074 |
|
| 1075 |
size_t nameSize = ConvertEndianness(rds->Length); |
| 1076 |
szName = new WINWCHAR[nameSize+1]; |
| 1077 |
WinWStrNCpy(szName, WCHARPTR2WINWCHARPTR(rds->NameString), nameSize); |
| 1078 |
szName[nameSize] = 0; |
| 1079 |
} |
| 1080 |
// Else, set the name to this entry's id |
| 1081 |
else |
| 1082 |
szName = MAKEINTRESOURCEWINW(ConvertEndianness(rdToScan->Entries[i].UName.Id)); |
| 1083 |
|
| 1084 |
if (rd.UOffset.DirectoryOffset.DataIsDirectory) |
| 1085 |
{ |
| 1086 |
CResourceDirectoryEntry *pRDE = new CResourceDirectoryEntry( |
| 1087 |
szName, |
| 1088 |
ScanDirectory( |
| 1089 |
rdRoot, |
| 1090 |
PRESOURCE_DIRECTORY(rd.UOffset.DirectoryOffset.OffsetToDirectory + (LPBYTE)rdRoot) |
| 1091 |
) |
| 1092 |
); |
| 1093 |
if (!rdc->AddEntry(pRDE)) delete pRDE; |
| 1094 |
} |
| 1095 |
else |
| 1096 |
{ |
| 1097 |
LPBYTE pbData = (LPBYTE)rdRoot + ConvertEndianness(rde->OffsetToData) - m_dwResourceSectionVA; |
| 1098 |
DWORD dwOffset = DWORD(pbData - m_pbPE); |
| 1099 |
|
| 1100 |
if (m_bKeepData) |
| 1101 |
{ |
| 1102 |
if (dwOffset > DWORD(m_iSize)) |
| 1103 |
{ |
| 1104 |
throw runtime_error("Invalid resource entry data pointer, possibly compressed resources"); |
| 1105 |
} |
| 1106 |
} |
| 1107 |
else |
| 1108 |
{ |
| 1109 |
pbData = m_pbPE; // dummy pointer to "nothing" |
| 1110 |
} |
| 1111 |
CResourceDirectoryEntry *pRDE = new CResourceDirectoryEntry( |
| 1112 |
szName, |
| 1113 |
new CResourceDataEntry( |
| 1114 |
pbData, |
| 1115 |
ConvertEndianness(rde->Size), |
| 1116 |
ConvertEndianness(rde->CodePage), |
| 1117 |
dwOffset |
| 1118 |
) |
| 1119 |
); |
| 1120 |
if (!rdc->AddEntry(pRDE)) delete pRDE; |
| 1121 |
} |
| 1122 |
|
| 1123 |
// Delete the dynamically allocated name if it is a name and not an id |
| 1124 |
if (!IS_INTRESOURCE(szName)) |
| 1125 |
delete [] szName; |
| 1126 |
} |
| 1127 |
|
| 1128 |
return rdc; |
| 1129 |
} |
| 1130 |
|
| 1131 |
// This function writes into a given place in memory (pbRsrcSec) the edited resource section |
| 1132 |
void CResourceEditor::WriteRsrcSec(BYTE* pbRsrcSec) { |
| 1133 |
BYTE* seeker = pbRsrcSec; |
| 1134 |
|
| 1135 |
queue<CResourceDirectory*> qDirs; // Used to scan the tree by level |
| 1136 |
queue<CResourceDataEntry*> qDataEntries; // Used for writing the data entries |
| 1137 |
queue<CResourceDataEntry*> qDataEntries2; // Used for writing raw resources data |
| 1138 |
queue<CResourceDirectoryEntry*> qStrings; // Used for writing resources' names |
| 1139 |
|
| 1140 |
qDirs.push(m_cResDir); |
| 1141 |
|
| 1142 |
while (!qDirs.empty()) { |
| 1143 |
CResourceDirectory* crd = qDirs.front(); |
| 1144 |
|
| 1145 |
IMAGE_RESOURCE_DIRECTORY rdDir = crd->GetInfo(); |
| 1146 |
|
| 1147 |
rdDir.NumberOfNamedEntries = ConvertEndianness(rdDir.NumberOfNamedEntries); |
| 1148 |
rdDir.NumberOfIdEntries = ConvertEndianness(rdDir.NumberOfIdEntries); |
| 1149 |
|
| 1150 |
CopyMemory(seeker, &rdDir, sizeof(IMAGE_RESOURCE_DIRECTORY)); |
| 1151 |
crd->m_ulWrittenAt = (ULONG_PTR)(seeker); |
| 1152 |
seeker += sizeof(IMAGE_RESOURCE_DIRECTORY); |
| 1153 |
|
| 1154 |
for (unsigned int i = 0; i < crd->CountEntries(); i++) { |
| 1155 |
if (crd->GetEntry(i)->HasName()) |
| 1156 |
qStrings.push(crd->GetEntry(i)); |
| 1157 |
if (crd->GetEntry(i)->IsDataDirectory()) |
| 1158 |
qDirs.push(crd->GetEntry(i)->GetSubDirectory()); |
| 1159 |
else { |
| 1160 |
qDataEntries.push(crd->GetEntry(i)->GetDataEntry()); |
| 1161 |
qDataEntries2.push(crd->GetEntry(i)->GetDataEntry()); |
| 1162 |
} |
| 1163 |
|
| 1164 |
MY_IMAGE_RESOURCE_DIRECTORY_ENTRY rDirE; |
| 1165 |
ZeroMemory(&rDirE, sizeof(rDirE)); |
| 1166 |
rDirE.UOffset.DirectoryOffset.DataIsDirectory = crd->GetEntry(i)->IsDataDirectory(); |
| 1167 |
rDirE.UName.Id = crd->GetEntry(i)->HasName() ? 0 : crd->GetEntry(i)->GetId(); |
| 1168 |
rDirE.UName.Id = ConvertEndianness(rDirE.UName.Id); |
| 1169 |
rDirE.UName.NameString.NameIsString = (crd->GetEntry(i)->HasName()) ? 1 : 0; |
| 1170 |
|
| 1171 |
CopyMemory(seeker, &rDirE, sizeof(MY_IMAGE_RESOURCE_DIRECTORY_ENTRY)); |
| 1172 |
crd->GetEntry(i)->m_ulWrittenAt = (ULONG_PTR)(seeker); |
| 1173 |
seeker += sizeof(MY_IMAGE_RESOURCE_DIRECTORY_ENTRY); |
| 1174 |
} |
| 1175 |
qDirs.pop(); |
| 1176 |
} |
| 1177 |
|
| 1178 |
/* |
| 1179 |
* Write IMAGE_RESOURCE_DATA_ENTRYs. |
| 1180 |
*/ |
| 1181 |
while (!qDataEntries.empty()) { |
| 1182 |
CResourceDataEntry* cRDataE = qDataEntries.front(); |
| 1183 |
IMAGE_RESOURCE_DATA_ENTRY rDataE = {0,}; |
| 1184 |
rDataE.CodePage = ConvertEndianness(cRDataE->GetCodePage()); |
| 1185 |
rDataE.Size = ConvertEndianness(cRDataE->GetSize()); |
| 1186 |
|
| 1187 |
CopyMemory(seeker, &rDataE, sizeof(IMAGE_RESOURCE_DATA_ENTRY)); |
| 1188 |
cRDataE->m_ulWrittenAt = (ULONG_PTR)(seeker); |
| 1189 |
seeker += sizeof(IMAGE_RESOURCE_DATA_ENTRY); |
| 1190 |
|
| 1191 |
qDataEntries.pop(); |
| 1192 |
} |
| 1193 |
|
| 1194 |
/* |
| 1195 |
* Write strings |
| 1196 |
*/ |
| 1197 |
while (!qStrings.empty()) { |
| 1198 |
CResourceDirectoryEntry* cRDirE = qStrings.front(); |
| 1199 |
|
| 1200 |
size_t resdirstr = (size_t)(seeker - pbRsrcSec); |
| 1201 |
assert(RALIGN(resdirstr, 4) == resdirstr); // PE spec says these are ?machine? word aligned |
| 1202 |
PMY_IMAGE_RESOURCE_DIRECTORY_ENTRY(cRDirE->m_ulWrittenAt)->UName.NameString.NameOffset = ConvertEndianness((DWORD) resdirstr); |
| 1203 |
|
| 1204 |
const WINWCHAR* szName = cRDirE->GetName(); |
| 1205 |
WORD iLen = (WORD) (WinWStrLen(szName)); // No terminator |
| 1206 |
*(WORD*)seeker = ConvertEndianness(iLen); |
| 1207 |
CopyMemory(seeker + sizeof(WORD), szName, iLen * sizeof(WINWCHAR)); |
| 1208 |
|
| 1209 |
seeker += RALIGN(sizeof(WORD) + (iLen * sizeof(WINWCHAR)), 4); |
| 1210 |
|
| 1211 |
qStrings.pop(); |
| 1212 |
} |
| 1213 |
|
| 1214 |
/* |
| 1215 |
* Write raw resource data and set offsets in IMAGE_RESOURCE_DATA_ENTRYs. |
| 1216 |
*/ |
| 1217 |
while (!qDataEntries2.empty()) { |
| 1218 |
CResourceDataEntry* cRDataE = qDataEntries2.front(); |
| 1219 |
CopyMemory(seeker, cRDataE->GetData(), cRDataE->GetSize()); |
| 1220 |
PIMAGE_RESOURCE_DATA_ENTRY(cRDataE->m_ulWrittenAt)->OffsetToData = ConvertEndianness((DWORD)(seeker - pbRsrcSec) + m_dwResourceSectionVA); |
| 1221 |
|
| 1222 |
seeker += RALIGN(cRDataE->GetSize(), 8); |
| 1223 |
|
| 1224 |
qDataEntries2.pop(); |
| 1225 |
} |
| 1226 |
|
| 1227 |
/* |
| 1228 |
* Set all of the directory entries offsets. |
| 1229 |
*/ |
| 1230 |
SetOffsets(m_cResDir, (ULONG_PTR)(pbRsrcSec)); |
| 1231 |
} |
| 1232 |
|
| 1233 |
// Sets the offsets in directory entries |
| 1234 |
void CResourceEditor::SetOffsets(CResourceDirectory* resDir, ULONG_PTR newResDirAt) { |
| 1235 |
for (unsigned int i = 0; i < resDir->CountEntries(); i++) { |
| 1236 |
PMY_IMAGE_RESOURCE_DIRECTORY_ENTRY rde = PMY_IMAGE_RESOURCE_DIRECTORY_ENTRY(resDir->GetEntry(i)->m_ulWrittenAt); |
| 1237 |
if (resDir->GetEntry(i)->IsDataDirectory()) { |
| 1238 |
rde->UOffset.DirectoryOffset.DataIsDirectory = 1; |
| 1239 |
rde->UOffset.DirectoryOffset.OffsetToDirectory = resDir->GetEntry(i)->GetSubDirectory()->m_ulWrittenAt - newResDirAt; |
| 1240 |
rde->UOffset.OffsetToData = ConvertEndianness(rde->UOffset.OffsetToData); |
| 1241 |
SetOffsets(resDir->GetEntry(i)->GetSubDirectory(), newResDirAt); |
| 1242 |
} |
| 1243 |
else { |
| 1244 |
rde->UOffset.OffsetToData = ConvertEndianness((DWORD)(resDir->GetEntry(i)->GetDataEntry()->m_ulWrittenAt - newResDirAt)); |
| 1245 |
} |
| 1246 |
} |
| 1247 |
} |
| 1248 |
|
| 1249 |
// Adjusts a virtual address by a specific amount |
| 1250 |
DWORD CResourceEditor::AdjustVA(DWORD dwVirtualAddress, DWORD dwAdjustment) { |
| 1251 |
dwVirtualAddress = ConvertEndianness(dwVirtualAddress); |
| 1252 |
dwVirtualAddress += dwAdjustment; |
| 1253 |
dwVirtualAddress = ConvertEndianness(dwVirtualAddress); |
| 1254 |
|
| 1255 |
return dwVirtualAddress; |
| 1256 |
} |
| 1257 |
|
| 1258 |
// Aligns a virtual address to the section alignment |
| 1259 |
DWORD CResourceEditor::AlignVA(DWORD dwVirtualAddress) { |
| 1260 |
DWORD temp32 = *GetCommonMemberFromPEOptHdr(m_ntHeaders->OptionalHeader, SectionAlignment); |
| 1261 |
DWORD dwAlignment = ConvertEndianness(temp32); |
| 1262 |
|
| 1263 |
dwVirtualAddress = ConvertEndianness(dwVirtualAddress); |
| 1264 |
dwVirtualAddress = RALIGN(dwVirtualAddress, dwAlignment); |
| 1265 |
dwVirtualAddress = ConvertEndianness(dwVirtualAddress); |
| 1266 |
|
| 1267 |
return dwVirtualAddress; |
| 1268 |
} |
| 1269 |
|
| 1270 |
////////////////////////////////////////////////////////////////////// |
| 1271 |
////////////////////////////////////////////////////////////////////// |
| 1272 |
// CResourceDirectory |
| 1273 |
////////////////////////////////////////////////////////////////////// |
| 1274 |
////////////////////////////////////////////////////////////////////// |
| 1275 |
|
| 1276 |
////////////////////////////////////////////////////////////////////// |
| 1277 |
// Construction/Destruction |
| 1278 |
////////////////////////////////////////////////////////////////////// |
| 1279 |
|
| 1280 |
CResourceDirectory::CResourceDirectory(PIMAGE_RESOURCE_DIRECTORY prd) { |
| 1281 |
m_rdDir = *prd; |
| 1282 |
m_rdDir.NumberOfIdEntries = 0; |
| 1283 |
m_rdDir.NumberOfNamedEntries = 0; |
| 1284 |
} |
| 1285 |
|
| 1286 |
CResourceDirectory::~CResourceDirectory() { |
| 1287 |
Destroy(); |
| 1288 |
} |
| 1289 |
|
| 1290 |
////////////////////////////////////////////////////////////////////// |
| 1291 |
// Methods |
| 1292 |
////////////////////////////////////////////////////////////////////// |
| 1293 |
|
| 1294 |
IMAGE_RESOURCE_DIRECTORY CResourceDirectory::GetInfo() { |
| 1295 |
return m_rdDir; |
| 1296 |
} |
| 1297 |
|
| 1298 |
CResourceDirectoryEntry* CResourceDirectory::GetEntry(unsigned int i) { |
| 1299 |
if (m_vEntries.size() < i) |
| 1300 |
return 0; |
| 1301 |
return m_vEntries[i]; |
| 1302 |
} |
| 1303 |
|
| 1304 |
// This function inserts a new directory entry |
| 1305 |
// It also keeps the directory entries sorted |
| 1306 |
bool CResourceDirectory::AddEntry(CResourceDirectoryEntry* entry) { |
| 1307 |
int i = 0; |
| 1308 |
if (entry->HasName()) { |
| 1309 |
const WINWCHAR* szEntName = entry->GetName(); |
| 1310 |
for (i = 0; i < m_rdDir.NumberOfNamedEntries; i++) { |
| 1311 |
const WINWCHAR* szName = m_vEntries[i]->GetName(); |
| 1312 |
int cmp = WinWStrCmp(szName, szEntName); |
| 1313 |
if (cmp == 0) |
| 1314 |
return false; |
| 1315 |
if (cmp > 0) |
| 1316 |
break; |
| 1317 |
} |
| 1318 |
m_rdDir.NumberOfNamedEntries++; |
| 1319 |
} |
| 1320 |
else { |
| 1321 |
for (i = m_rdDir.NumberOfNamedEntries; i < m_rdDir.NumberOfNamedEntries+m_rdDir.NumberOfIdEntries; i++) { |
| 1322 |
if (m_vEntries[i]->GetId() == entry->GetId()) return false; |
| 1323 |
if (m_vEntries[i]->GetId() > entry->GetId()) break; |
| 1324 |
} |
| 1325 |
m_rdDir.NumberOfIdEntries++; |
| 1326 |
} |
| 1327 |
m_vEntries.insert(m_vEntries.begin() + i, entry); |
| 1328 |
return true; |
| 1329 |
} |
| 1330 |
|
| 1331 |
void CResourceDirectory::RemoveEntry(int i) { |
| 1332 |
if (m_vEntries[i]->HasName()) |
| 1333 |
m_rdDir.NumberOfNamedEntries--; |
| 1334 |
else |
| 1335 |
m_rdDir.NumberOfIdEntries--; |
| 1336 |
delete m_vEntries[i]; |
| 1337 |
m_vEntries.erase(m_vEntries.begin() + i); |
| 1338 |
} |
| 1339 |
|
| 1340 |
unsigned int CResourceDirectory::CountEntries() { |
| 1341 |
return truncate_cast(unsigned int,m_vEntries.size()); |
| 1342 |
} |
| 1343 |
|
| 1344 |
// Returns the index of a directory entry with the specified name |
| 1345 |
// Name can be a string or an id |
| 1346 |
// Returns -1 if can not be found |
| 1347 |
int CResourceDirectory::Find(const WINWCHAR* szName) { |
| 1348 |
if (IS_INTRESOURCE(szName)) |
| 1349 |
return Find((WORD) (ULONG_PTR) szName); |
| 1350 |
else |
| 1351 |
if (szName[0] == L'#') |
| 1352 |
return Find(WORD(WinWStrToInt(szName + 1))); |
| 1353 |
|
| 1354 |
for (unsigned int i = 0; i < m_vEntries.size(); i++) { |
| 1355 |
if (!m_vEntries[i]->HasName()) |
| 1356 |
continue; |
| 1357 |
|
| 1358 |
const WINWCHAR* szEntName = m_vEntries[i]->GetName(); |
| 1359 |
int cmp = WinWStrCmp(szName, szEntName); |
| 1360 |
if (!cmp) |
| 1361 |
return i; |
| 1362 |
} |
| 1363 |
return -1; |
| 1364 |
} |
| 1365 |
|
| 1366 |
// Returns the index of a directory entry with the specified id |
| 1367 |
// Returns -1 if can not be found |
| 1368 |
int CResourceDirectory::Find(WORD wId) { |
| 1369 |
for (unsigned int i = 0; i < m_vEntries.size(); i++) { |
| 1370 |
if (m_vEntries[i]->HasName()) continue; |
| 1371 |
if (wId == m_vEntries[i]->GetId()) return i; |
| 1372 |
} |
| 1373 |
return -1; |
| 1374 |
} |
| 1375 |
|
| 1376 |
// Get the size of this resource directory (including all of its children) |
| 1377 |
DWORD CResourceDirectory::GetSize() { |
| 1378 |
DWORD dwSize = sizeof(IMAGE_RESOURCE_DIRECTORY); |
| 1379 |
for (unsigned int i = 0; i < m_vEntries.size(); i++) { |
| 1380 |
dwSize += sizeof(MY_IMAGE_RESOURCE_DIRECTORY_ENTRY); |
| 1381 |
if (m_vEntries[i]->HasName()) |
| 1382 |
dwSize += sizeof(IMAGE_RESOURCE_DIR_STRING_U) + (m_vEntries[i]->GetNameLength()+1)*sizeof(WINWCHAR); |
| 1383 |
if (m_vEntries[i]->IsDataDirectory()) |
| 1384 |
dwSize += m_vEntries[i]->GetSubDirectory()->GetSize(); |
| 1385 |
else { |
| 1386 |
DWORD dwAligned = m_vEntries[i]->GetDataEntry()->GetSize(); |
| 1387 |
ALIGN(dwAligned, 8); |
| 1388 |
dwSize += sizeof(IMAGE_RESOURCE_DATA_ENTRY) + dwAligned; |
| 1389 |
} |
| 1390 |
} |
| 1391 |
return dwSize; |
| 1392 |
} |
| 1393 |
|
| 1394 |
// Destroys this directory and all of its children |
| 1395 |
void CResourceDirectory::Destroy() { |
| 1396 |
for (unsigned int i = 0; i < m_vEntries.size(); i++) { |
| 1397 |
if (m_vEntries[i]->IsDataDirectory()) { |
| 1398 |
m_vEntries[i]->GetSubDirectory()->Destroy(); |
| 1399 |
delete m_vEntries[i]->GetSubDirectory(); |
| 1400 |
} |
| 1401 |
else |
| 1402 |
delete m_vEntries[i]->GetDataEntry(); |
| 1403 |
delete m_vEntries[i]; |
| 1404 |
} |
| 1405 |
m_vEntries.clear(); |
| 1406 |
} |
| 1407 |
|
| 1408 |
////////////////////////////////////////////////////////////////////// |
| 1409 |
////////////////////////////////////////////////////////////////////// |
| 1410 |
// CResourceDirectoryEntry |
| 1411 |
////////////////////////////////////////////////////////////////////// |
| 1412 |
////////////////////////////////////////////////////////////////////// |
| 1413 |
|
| 1414 |
////////////////////////////////////////////////////////////////////// |
| 1415 |
// Construction/Destruction |
| 1416 |
////////////////////////////////////////////////////////////////////// |
| 1417 |
|
| 1418 |
CResourceDirectoryEntry::CResourceDirectoryEntry(const WINWCHAR* szName, CResourceDirectory* rdSubDir) { |
| 1419 |
if (IS_INTRESOURCE(szName)) { |
| 1420 |
m_bHasName = false; |
| 1421 |
m_szName = 0; |
| 1422 |
m_wId = (WORD) (ULONG_PTR) szName; |
| 1423 |
} |
| 1424 |
else { |
| 1425 |
m_bHasName = true; |
| 1426 |
m_szName = WinWStrDupFromWinWStr(szName); |
| 1427 |
} |
| 1428 |
m_bIsDataDirectory = true; |
| 1429 |
m_rdSubDir = rdSubDir; |
| 1430 |
} |
| 1431 |
|
| 1432 |
CResourceDirectoryEntry::CResourceDirectoryEntry(const WINWCHAR* szName, CResourceDataEntry* rdeData) { |
| 1433 |
if (IS_INTRESOURCE(szName)) { |
| 1434 |
m_bHasName = false; |
| 1435 |
m_szName = 0; |
| 1436 |
m_wId = (WORD) (ULONG_PTR) szName; |
| 1437 |
} |
| 1438 |
else { |
| 1439 |
m_bHasName = true; |
| 1440 |
m_szName = WinWStrDupFromWinWStr(szName); |
| 1441 |
} |
| 1442 |
m_bIsDataDirectory = false; |
| 1443 |
m_rdeData = rdeData; |
| 1444 |
} |
| 1445 |
|
| 1446 |
CResourceDirectoryEntry::~CResourceDirectoryEntry() { |
| 1447 |
if (m_bHasName) |
| 1448 |
free(m_szName); |
| 1449 |
} |
| 1450 |
|
| 1451 |
////////////////////////////////////////////////////////////////////// |
| 1452 |
// Methods |
| 1453 |
////////////////////////////////////////////////////////////////////// |
| 1454 |
|
| 1455 |
bool CResourceDirectoryEntry::HasName() const { |
| 1456 |
return m_bHasName; |
| 1457 |
} |
| 1458 |
|
| 1459 |
// Don't forget to free the memory used by the string after usage! |
| 1460 |
const WINWCHAR* CResourceDirectoryEntry::GetName() const { |
| 1461 |
return m_bHasName ? m_szName : 0; |
| 1462 |
} |
| 1463 |
|
| 1464 |
int CResourceDirectoryEntry::GetNameLength() const { |
| 1465 |
return (int) WinWStrLen(m_szName); |
| 1466 |
} |
| 1467 |
|
| 1468 |
WORD CResourceDirectoryEntry::GetId() const { |
| 1469 |
return m_bHasName ? 0 : m_wId; |
| 1470 |
} |
| 1471 |
|
| 1472 |
bool CResourceDirectoryEntry::IsDataDirectory() const { |
| 1473 |
return m_bIsDataDirectory; |
| 1474 |
} |
| 1475 |
|
| 1476 |
CResourceDirectory* CResourceDirectoryEntry::GetSubDirectory() const { |
| 1477 |
if (!m_bIsDataDirectory) |
| 1478 |
return NULL; |
| 1479 |
return m_rdSubDir; |
| 1480 |
} |
| 1481 |
|
| 1482 |
CResourceDataEntry* CResourceDirectoryEntry::GetDataEntry() const { |
| 1483 |
if (m_bIsDataDirectory) |
| 1484 |
return NULL; |
| 1485 |
return m_rdeData; |
| 1486 |
} |
| 1487 |
|
| 1488 |
////////////////////////////////////////////////////////////////////// |
| 1489 |
////////////////////////////////////////////////////////////////////// |
| 1490 |
// CResourceDataEntry |
| 1491 |
////////////////////////////////////////////////////////////////////// |
| 1492 |
////////////////////////////////////////////////////////////////////// |
| 1493 |
|
| 1494 |
////////////////////////////////////////////////////////////////////// |
| 1495 |
// Construction/Destruction |
| 1496 |
////////////////////////////////////////////////////////////////////// |
| 1497 |
|
| 1498 |
CResourceDataEntry::CResourceDataEntry(BYTE* pbData, DWORD dwSize, DWORD dwCodePage, DWORD dwOffset) { |
| 1499 |
m_pbData = 0; |
| 1500 |
SetData(pbData, dwSize, dwCodePage); |
| 1501 |
m_dwOffset = dwOffset; |
| 1502 |
} |
| 1503 |
|
| 1504 |
CResourceDataEntry::~CResourceDataEntry() { |
| 1505 |
if (m_pbData) |
| 1506 |
delete [] m_pbData; |
| 1507 |
} |
| 1508 |
|
| 1509 |
////////////////////////////////////////////////////////////////////// |
| 1510 |
// Methods |
| 1511 |
////////////////////////////////////////////////////////////////////// |
| 1512 |
|
| 1513 |
// To save memory this function doesn't give you a copy of the data |
| 1514 |
// Don't mess with the data returned from this function! |
| 1515 |
BYTE* CResourceDataEntry::GetData() const { |
| 1516 |
return m_pbData; |
| 1517 |
} |
| 1518 |
|
| 1519 |
void CResourceDataEntry::SetData(BYTE* pbData, DWORD dwSize) { |
| 1520 |
SetData(pbData, dwSize, m_dwCodePage); |
| 1521 |
} |
| 1522 |
|
| 1523 |
void CResourceDataEntry::SetData(BYTE* pbData, DWORD dwSize, DWORD dwCodePage) { |
| 1524 |
if (m_pbData) delete [] m_pbData; |
| 1525 |
m_pbData = new BYTE[dwSize]; |
| 1526 |
CopyMemory(m_pbData, pbData, dwSize); |
| 1527 |
m_dwSize = dwSize; |
| 1528 |
m_dwCodePage = dwCodePage; |
| 1529 |
m_dwOffset = DWORD(-1); // unset |
| 1530 |
} |
| 1531 |
|
| 1532 |
DWORD CResourceDataEntry::GetSize() const { |
| 1533 |
return m_dwSize; |
| 1534 |
} |
| 1535 |
|
| 1536 |
DWORD CResourceDataEntry::GetCodePage() const { |
| 1537 |
return m_dwCodePage; |
| 1538 |
} |
| 1539 |
|
| 1540 |
DWORD CResourceDataEntry::GetOffset() const { |
| 1541 |
return m_dwOffset; |
| 1542 |
} |