From 973a61fbcf1edda339d0ccb0e5c72112254a614f Mon Sep 17 00:00:00 2001 From: kichik Date: Tue, 24 Jun 2003 19:17:51 +0000 Subject: [PATCH] Standard tabing git-svn-id: https://svn.code.sf.net/p/nsis/code/NSIS/trunk@2675 212acab6-be3b-0410-9dea-997c60f758d6 --- Source/DialogTemplate.cpp | 748 ++++++++++++++--------------- Source/DialogTemplate.h | 170 +++---- Source/ResourceEditor.cpp | 972 +++++++++++++++++++------------------- Source/ResourceEditor.h | 162 +++---- 4 files changed, 1026 insertions(+), 1026 deletions(-) diff --git a/Source/DialogTemplate.cpp b/Source/DialogTemplate.cpp index 2aaba784..5878c640 100644 --- a/Source/DialogTemplate.cpp +++ b/Source/DialogTemplate.cpp @@ -1,23 +1,23 @@ /* - Copyright (C) 2002 Amir Szekely + Copyright (C) 2002 Amir Szekely - This software is provided 'as-is', without any express or implied - warranty. In no event will the authors be held liable for any damages - arising from the use of this software. + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. - Permission is granted to anyone to use this software for any purpose, - including commercial applications, and to alter it and redistribute it - freely, subject to the following restrictions: + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: - 1. The origin of this software must not be misrepresented; you must not - claim that you wrote the original software. If you use this software - in a product, an acknowledgment in the product documentation would be - appreciated but is not required. + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. - 2. Altered source versions must be plainly marked as such, and must not be - misrepresented as being the original software. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. - 3. This notice may not be removed or altered from any source distribution. + 3. This notice may not be removed or altered from any source distribution. */ #include "DialogTemplate.h" @@ -30,49 +30,49 @@ // returns the number of WCHARs in str including null charcter inline DWORD WCStrLen(WCHAR* szwStr) { - int i; - for (i = 0; szwStr[i]; i++); - return i+1; + int i; + for (i = 0; szwStr[i]; i++); + return i+1; } // Reads a variany length array from seeker into readInto and advances seeker void ReadVarLenArr(BYTE* &seeker, char* &readInto, unsigned int uCodePage) { - WORD* arr = (WORD*)seeker; - switch (arr[0]) { - case 0x0000: - readInto = 0; - seeker += sizeof(WORD); - break; - case 0xFFFF: - readInto = MAKEINTRESOURCE(arr[1]); - seeker += 2*sizeof(WORD); - break; - default: - { - int iStrLen = WideCharToMultiByte(uCodePage, 0, (WCHAR*)arr, -1, 0, 0, 0, 0); - readInto = new char[iStrLen]; - WideCharToMultiByte(uCodePage, 0, (WCHAR*)arr, -1, readInto, iStrLen, 0, 0); - seeker += WCStrLen((WCHAR*)arr)*sizeof(WCHAR); - } - break; - } + WORD* arr = (WORD*)seeker; + switch (arr[0]) { + case 0x0000: + readInto = 0; + seeker += sizeof(WORD); + break; + case 0xFFFF: + readInto = MAKEINTRESOURCE(arr[1]); + seeker += 2*sizeof(WORD); + break; + default: + { + int iStrLen = WideCharToMultiByte(uCodePage, 0, (WCHAR*)arr, -1, 0, 0, 0, 0); + readInto = new char[iStrLen]; + WideCharToMultiByte(uCodePage, 0, (WCHAR*)arr, -1, readInto, iStrLen, 0, 0); + seeker += WCStrLen((WCHAR*)arr)*sizeof(WCHAR); + } + break; + } } // A macro that writes a given string (that can be a number too) into the buffer #define WriteStringOrId(x) \ - if (x) \ - if (IS_INTRESOURCE(x)) { \ - *(WORD*)seeker = 0xFFFF; \ - seeker += sizeof(WORD); \ - *(WORD*)seeker = WORD(x); \ - seeker += sizeof(WORD); \ - } \ - else { \ - int us = MultiByteToWideChar(m_uCodePage, 0, x, -1, (WCHAR*)seeker, dwSize); \ - seeker += us*sizeof(WCHAR); \ - } \ - else \ - seeker += sizeof(WORD); + if (x) \ + if (IS_INTRESOURCE(x)) { \ + *(WORD*)seeker = 0xFFFF; \ + seeker += sizeof(WORD); \ + *(WORD*)seeker = WORD(x); \ + seeker += sizeof(WORD); \ + } \ + else { \ + int us = MultiByteToWideChar(m_uCodePage, 0, x, -1, (WCHAR*)seeker, dwSize); \ + seeker += us*sizeof(WCHAR); \ + } \ + else \ + seeker += sizeof(WORD); // A macro that adds the size of x (which can be a string a number, or nothing) to dwSize #define AddStringOrIdSize(x) dwSize += x ? (IS_INTRESOURCE(x) ? sizeof(DWORD) : (lstrlen(x)+1)*sizeof(WCHAR)) : sizeof(WORD) @@ -84,142 +84,142 @@ void ReadVarLenArr(BYTE* &seeker, char* &readInto, unsigned int uCodePage) { CDialogTemplate::CDialogTemplate(BYTE* pbData, unsigned int uCodePage) { m_uCodePage = uCodePage; - m_szClass = 0; - m_szFont = 0; - m_szMenu = 0; - m_szTitle = 0; + m_szClass = 0; + m_szFont = 0; + m_szMenu = 0; + m_szTitle = 0; - WORD wItems = 0; + WORD wItems = 0; - if (*(DWORD*)pbData == 0xFFFF0001) { // Extended dialog template signature - m_bExtended = true; + if (*(DWORD*)pbData == 0xFFFF0001) { // Extended dialog template signature + m_bExtended = true; - DLGTEMPLATEEX* dTemplateEx = (DLGTEMPLATEEX*)pbData; + DLGTEMPLATEEX* dTemplateEx = (DLGTEMPLATEEX*)pbData; - m_dwHelpId = dTemplateEx->helpID; - m_dwStyle = dTemplateEx->style; - m_dwExtStyle = dTemplateEx->exStyle; - m_sX = dTemplateEx->x; - m_sY = dTemplateEx->y; - m_sWidth = dTemplateEx->cx; - m_sHeight = dTemplateEx->cy; + m_dwHelpId = dTemplateEx->helpID; + m_dwStyle = dTemplateEx->style; + m_dwExtStyle = dTemplateEx->exStyle; + m_sX = dTemplateEx->x; + m_sY = dTemplateEx->y; + m_sWidth = dTemplateEx->cx; + m_sHeight = dTemplateEx->cy; - wItems = dTemplateEx->cDlgItems; - } - else { - m_bExtended = false; - DLGTEMPLATE* dTemplate = (DLGTEMPLATE*)pbData; + wItems = dTemplateEx->cDlgItems; + } + else { + m_bExtended = false; + DLGTEMPLATE* dTemplate = (DLGTEMPLATE*)pbData; - m_dwStyle = dTemplate->style; - m_dwExtStyle = dTemplate->dwExtendedStyle; - m_sX = dTemplate->x; - m_sY = dTemplate->y; - m_sWidth = dTemplate->cx; - m_sHeight = dTemplate->cy; + m_dwStyle = dTemplate->style; + m_dwExtStyle = dTemplate->dwExtendedStyle; + m_sX = dTemplate->x; + m_sY = dTemplate->y; + m_sWidth = dTemplate->cx; + m_sHeight = dTemplate->cy; - wItems = dTemplate->cdit; - } + wItems = dTemplate->cdit; + } - BYTE* seeker = pbData + (m_bExtended ? sizeof(DLGTEMPLATEEX) : sizeof(DLGTEMPLATE)); + BYTE* seeker = pbData + (m_bExtended ? sizeof(DLGTEMPLATEEX) : sizeof(DLGTEMPLATE)); - // Read menu variant length array - ReadVarLenArr(seeker, m_szMenu, m_uCodePage); - // Read class variant length array - ReadVarLenArr(seeker, m_szClass, m_uCodePage); - // Read title variant length array - ReadVarLenArr(seeker, m_szTitle, m_uCodePage); - // Read font size and variant length array (only if style DS_SETFONT is used!) - if (m_dwStyle & DS_SETFONT) { - m_sFontSize = *(short*)seeker; - seeker += sizeof(short); + // Read menu variant length array + ReadVarLenArr(seeker, m_szMenu, m_uCodePage); + // Read class variant length array + ReadVarLenArr(seeker, m_szClass, m_uCodePage); + // Read title variant length array + ReadVarLenArr(seeker, m_szTitle, m_uCodePage); + // Read font size and variant length array (only if style DS_SETFONT is used!) + if (m_dwStyle & DS_SETFONT) { + m_sFontSize = *(short*)seeker; + seeker += sizeof(short); if (m_bExtended) { - m_sFontWeight = *(short*)seeker; - seeker += sizeof(short); - m_bItalic = *(BYTE*)seeker; - seeker += sizeof(BYTE); + m_sFontWeight = *(short*)seeker; + seeker += sizeof(short); + m_bItalic = *(BYTE*)seeker; + seeker += sizeof(BYTE); m_bCharset = *(BYTE*)seeker; seeker += sizeof(BYTE); - } - ReadVarLenArr(seeker, m_szFont, m_uCodePage); - } + } + ReadVarLenArr(seeker, m_szFont, m_uCodePage); + } // Read items - for (int i = 0; i < wItems; i++) { - // DLGITEMTEMPLATE[EX]s must be aligned on DWORD boundry - if (DWORD(seeker - pbData) % sizeof(DWORD)) - seeker += sizeof(WORD); + for (int i = 0; i < wItems; i++) { + // DLGITEMTEMPLATE[EX]s must be aligned on DWORD boundry + if (DWORD(seeker - pbData) % sizeof(DWORD)) + seeker += sizeof(WORD); - DialogItemTemplate* item = new DialogItemTemplate; - ZeroMemory(item, sizeof(DialogItemTemplate)); + DialogItemTemplate* item = new DialogItemTemplate; + ZeroMemory(item, sizeof(DialogItemTemplate)); - if (m_bExtended) { - DLGITEMTEMPLATEEX* rawItem = (DLGITEMTEMPLATEEX*)seeker; + if (m_bExtended) { + DLGITEMTEMPLATEEX* rawItem = (DLGITEMTEMPLATEEX*)seeker; - item->dwHelpId = rawItem->helpID; - item->dwStyle = rawItem->style; - item->dwExtStyle = rawItem->exStyle; - item->sX = rawItem->x; - item->sY = rawItem->y; - item->sWidth = rawItem->cx; - item->sHeight = rawItem->cy; - item->wId = rawItem->id; + item->dwHelpId = rawItem->helpID; + item->dwStyle = rawItem->style; + item->dwExtStyle = rawItem->exStyle; + item->sX = rawItem->x; + item->sY = rawItem->y; + item->sWidth = rawItem->cx; + item->sHeight = rawItem->cy; + item->wId = rawItem->id; - seeker += sizeof(DLGITEMTEMPLATEEX); - } - else { - DLGITEMTEMPLATE* rawItem = (DLGITEMTEMPLATE*)seeker; + seeker += sizeof(DLGITEMTEMPLATEEX); + } + else { + DLGITEMTEMPLATE* rawItem = (DLGITEMTEMPLATE*)seeker; - item->dwStyle = rawItem->style; - item->dwExtStyle = rawItem->dwExtendedStyle; - item->sX = rawItem->x; - item->sY = rawItem->y; - item->sWidth = rawItem->cx; - item->sHeight = rawItem->cy; - item->wId = rawItem->id; + item->dwStyle = rawItem->style; + item->dwExtStyle = rawItem->dwExtendedStyle; + item->sX = rawItem->x; + item->sY = rawItem->y; + item->sWidth = rawItem->cx; + item->sHeight = rawItem->cy; + item->wId = rawItem->id; - seeker += sizeof(DLGITEMTEMPLATE); - } + seeker += sizeof(DLGITEMTEMPLATE); + } - // Read class variant length array - ReadVarLenArr(seeker, item->szClass, m_uCodePage); - // Read title variant length array - ReadVarLenArr(seeker, item->szTitle, m_uCodePage); + // Read class variant length array + ReadVarLenArr(seeker, item->szClass, m_uCodePage); + // Read title variant length array + ReadVarLenArr(seeker, item->szTitle, m_uCodePage); - // Read creation data variant length array - // First read the size of the array (no null termination) - item->wCreateDataSize = *(WORD*)seeker; - seeker += sizeof(WORD); - // Then read the array it self (if size is not 0) - if (item->wCreateDataSize) { - item->wCreateDataSize -= sizeof(WORD); // Size includes size field itself... - item->szCreationData = new char[item->wCreateDataSize]; - CopyMemory(item->szCreationData, seeker, item->wCreateDataSize); - seeker += item->wCreateDataSize; - } + // Read creation data variant length array + // First read the size of the array (no null termination) + item->wCreateDataSize = *(WORD*)seeker; + seeker += sizeof(WORD); + // Then read the array it self (if size is not 0) + if (item->wCreateDataSize) { + item->wCreateDataSize -= sizeof(WORD); // Size includes size field itself... + item->szCreationData = new char[item->wCreateDataSize]; + CopyMemory(item->szCreationData, seeker, item->wCreateDataSize); + seeker += item->wCreateDataSize; + } - // Add the item to the vector - m_vItems.push_back(item); - } + // Add the item to the vector + m_vItems.push_back(item); + } } CDialogTemplate::~CDialogTemplate() { - if (m_szMenu && !IS_INTRESOURCE(m_szMenu)) - delete [] m_szMenu; - if (m_szClass && !IS_INTRESOURCE(m_szClass)) - delete [] m_szClass; - if (m_szTitle) - delete [] m_szTitle; - if (m_szFont) - delete [] m_szTitle; + if (m_szMenu && !IS_INTRESOURCE(m_szMenu)) + delete [] m_szMenu; + if (m_szClass && !IS_INTRESOURCE(m_szClass)) + delete [] m_szClass; + if (m_szTitle) + delete [] m_szTitle; + if (m_szFont) + delete [] m_szTitle; - for (unsigned int i = 0; i < m_vItems.size(); i++) { - if (m_vItems[i]->szClass && !IS_INTRESOURCE(m_vItems[i]->szClass)) - delete [] m_vItems[i]->szClass; - if (m_vItems[i]->szTitle && !IS_INTRESOURCE(m_vItems[i]->szTitle)) - delete [] m_vItems[i]->szTitle; - if (m_vItems[i]->szCreationData) - delete [] m_vItems[i]->szCreationData; - } + for (unsigned int i = 0; i < m_vItems.size(); i++) { + if (m_vItems[i]->szClass && !IS_INTRESOURCE(m_vItems[i]->szClass)) + delete [] m_vItems[i]->szClass; + if (m_vItems[i]->szTitle && !IS_INTRESOURCE(m_vItems[i]->szTitle)) + delete [] m_vItems[i]->szTitle; + if (m_vItems[i]->szCreationData) + delete [] m_vItems[i]->szCreationData; + } } ////////////////////////////////////////////////////////////////////// @@ -228,32 +228,32 @@ CDialogTemplate::~CDialogTemplate() { // Returns the width of the dialog short CDialogTemplate::GetWidth() { - return m_sWidth; + return m_sWidth; } // Returns the height of the dialog short CDialogTemplate::GetHeight() { - return m_sHeight; + return m_sHeight; } // Returns info about the item with the id wId DialogItemTemplate* CDialogTemplate::GetItem(WORD wId) { - for (unsigned int i = 0; i < m_vItems.size(); i++) - if (m_vItems[i]->wId == wId) - return m_vItems[i]; - return 0; + for (unsigned int i = 0; i < m_vItems.size(); i++) + if (m_vItems[i]->wId == wId) + return m_vItems[i]; + return 0; } // Returns info about the item with the indexed i DialogItemTemplate* CDialogTemplate::GetItemByIdx(DWORD i) { - if (i >= m_vItems.size()) return 0; - return m_vItems[i]; + if (i >= m_vItems.size()) return 0; + return m_vItems[i]; } // Removes an item void CDialogTemplate::RemoveItem(WORD wId) { for (unsigned int i = 0; i < m_vItems.size(); i++) - if (m_vItems[i]->wId == wId) + if (m_vItems[i]->wId == wId) m_vItems.erase(m_vItems.begin() + i); } @@ -274,145 +274,145 @@ void CDialogTemplate::SetFont(char* szFaceName, WORD wFontSize) { m_bExtended = true; } m_dwStyle |= DS_SETFONT; - if (m_szFont) delete [] m_szFont; - m_szFont = new char[lstrlen(szFaceName)]; - lstrcpy(m_szFont, szFaceName); - m_sFontSize = wFontSize; + if (m_szFont) delete [] m_szFont; + m_szFont = new char[lstrlen(szFaceName)]; + lstrcpy(m_szFont, szFaceName); + m_sFontSize = wFontSize; } // Adds an item to the dialog void CDialogTemplate::AddItem(DialogItemTemplate item) { - DialogItemTemplate* newItem = new DialogItemTemplate; - CopyMemory(newItem, &item, sizeof(DialogItemTemplate)); + DialogItemTemplate* newItem = new DialogItemTemplate; + CopyMemory(newItem, &item, sizeof(DialogItemTemplate)); - if (item.szClass && !IS_INTRESOURCE(item.szClass)) { - newItem->szClass = new char[lstrlen(item.szClass)+1]; - lstrcpy(newItem->szClass, item.szClass); - } - if (item.szTitle && !IS_INTRESOURCE(item.szTitle)) { - newItem->szTitle = new char[lstrlen(item.szTitle)+1]; - lstrcpy(newItem->szTitle, item.szTitle); - } - if (item.wCreateDataSize) { - newItem->szCreationData = new char[item.wCreateDataSize]; - memcpy(newItem->szCreationData, item.szCreationData, item.wCreateDataSize); - } - m_vItems.push_back(newItem); + if (item.szClass && !IS_INTRESOURCE(item.szClass)) { + newItem->szClass = new char[lstrlen(item.szClass)+1]; + lstrcpy(newItem->szClass, item.szClass); + } + if (item.szTitle && !IS_INTRESOURCE(item.szTitle)) { + newItem->szTitle = new char[lstrlen(item.szTitle)+1]; + lstrcpy(newItem->szTitle, item.szTitle); + } + if (item.wCreateDataSize) { + newItem->szCreationData = new char[item.wCreateDataSize]; + memcpy(newItem->szCreationData, item.szCreationData, item.wCreateDataSize); + } + m_vItems.push_back(newItem); } // Moves all of the items in the dialog by (x,y) void CDialogTemplate::MoveAll(short x, short y) { - for (unsigned int i = 0; i < m_vItems.size(); i++) { - m_vItems[i]->sX += x; - m_vItems[i]->sY += y; - } + for (unsigned int i = 0; i < m_vItems.size(); i++) { + m_vItems[i]->sX += x; + m_vItems[i]->sY += y; + } } // Resizes the dialog by (x,y) void CDialogTemplate::Resize(short x, short y) { - m_sWidth += x; - m_sHeight += y; + m_sWidth += x; + m_sHeight += y; } // Creates a dummy dialog that is used for converting units HWND CDialogTemplate::CreateDummyDialog() { - DWORD dwTemp; - BYTE* pbDlg = Save(dwTemp); - HWND hDlg = CreateDialogIndirect(GetModuleHandle(0), (DLGTEMPLATE*)pbDlg, 0, 0); - delete [] pbDlg; - if (!hDlg) - throw runtime_error("Can't create dialog from template!"); + DWORD dwTemp; + BYTE* pbDlg = Save(dwTemp); + HWND hDlg = CreateDialogIndirect(GetModuleHandle(0), (DLGTEMPLATE*)pbDlg, 0, 0); + delete [] pbDlg; + if (!hDlg) + throw runtime_error("Can't create dialog from template!"); - return hDlg; + return hDlg; } // Converts pixels to this dialog's units void CDialogTemplate::PixelsToDlgUnits(short& x, short& y) { - HWND hDlg = CreateDummyDialog(); - RECT r = {0, 0, 10000, 10000}; - MapDialogRect(hDlg, &r); - DestroyWindow(hDlg); + HWND hDlg = CreateDummyDialog(); + RECT r = {0, 0, 10000, 10000}; + MapDialogRect(hDlg, &r); + DestroyWindow(hDlg); - x = short(float(x) / (float(r.right)/10000)); - y = short(float(y) / (float(r.bottom)/10000)); + x = short(float(x) / (float(r.right)/10000)); + y = short(float(y) / (float(r.bottom)/10000)); } // Converts pixels to this dialog's units void CDialogTemplate::DlgUnitsToPixels(short& x, short& y) { - HWND hDlg = CreateDummyDialog(); - RECT r = {0, 0, 10000, 10000}; - MapDialogRect(hDlg, &r); - DestroyWindow(hDlg); + HWND hDlg = CreateDummyDialog(); + RECT r = {0, 0, 10000, 10000}; + MapDialogRect(hDlg, &r); + DestroyWindow(hDlg); - x = short(float(x) * (float(r.right)/10000)); - y = short(float(y) * (float(r.bottom)/10000)); + x = short(float(x) * (float(r.right)/10000)); + y = short(float(y) * (float(r.bottom)/10000)); } // Returns the size of a string in the dialog (in dialog units) SIZE CDialogTemplate::GetStringSize(WORD id, char *str) { - HWND hDlg = CreateDummyDialog(); + HWND hDlg = CreateDummyDialog(); - LOGFONT f; - GetObject((HFONT)SendMessage(hDlg, WM_GETFONT, 0, 0), sizeof(LOGFONT), &f); + LOGFONT f; + GetObject((HFONT)SendMessage(hDlg, WM_GETFONT, 0, 0), sizeof(LOGFONT), &f); - HDC memDC = CreateCompatibleDC(GetDC(hDlg)); - HFONT font = CreateFontIndirect(&f); - SelectObject(memDC, font); + HDC memDC = CreateCompatibleDC(GetDC(hDlg)); + HFONT font = CreateFontIndirect(&f); + SelectObject(memDC, font); - SIZE size; - GetTextExtentPoint32(memDC, str, lstrlen(str), &size); + SIZE size; + GetTextExtentPoint32(memDC, str, lstrlen(str), &size); - DestroyWindow(hDlg); - DeleteObject(font); - DeleteDC(memDC); + DestroyWindow(hDlg); + DeleteObject(font); + DeleteDC(memDC); - PixelsToDlgUnits((short&)size.cx, (short&)size.cy); - - return size; + PixelsToDlgUnits((short&)size.cx, (short&)size.cy); + + return size; } // Trims the right margins of a control to fit a given text string size. void CDialogTemplate::RTrimToString(WORD id, char *str, int margins) { - DialogItemTemplate* item = GetItem(id); - if (!item) return; + DialogItemTemplate* item = GetItem(id); + if (!item) return; - SIZE size = GetStringSize(id, str); + SIZE size = GetStringSize(id, str); - size.cx += margins; - size.cy += 2; + size.cx += margins; + size.cy += 2; - item->sWidth = short(size.cx); - item->sHeight = short(size.cy); + item->sWidth = short(size.cx); + item->sHeight = short(size.cy); } // Trims the left margins of a control to fit a given text string size. void CDialogTemplate::LTrimToString(WORD id, char *str, int margins) { - DialogItemTemplate* item = GetItem(id); - if (!item) return; + DialogItemTemplate* item = GetItem(id); + if (!item) return; - SIZE size = GetStringSize(id, str); + SIZE size = GetStringSize(id, str); - size.cx += margins; - size.cy += 2; + size.cx += margins; + size.cy += 2; - item->sX += item->sWidth - short(size.cx); - item->sWidth = short(size.cx); - item->sHeight = short(size.cy); + item->sX += item->sWidth - short(size.cx); + item->sWidth = short(size.cx); + item->sHeight = short(size.cy); } // Trims the left and right margins of a control to fit a given text string size. void CDialogTemplate::CTrimToString(WORD id, char *str, int margins) { - DialogItemTemplate* item = GetItem(id); - if (!item) return; + DialogItemTemplate* item = GetItem(id); + if (!item) return; - SIZE size = GetStringSize(id, str); + SIZE size = GetStringSize(id, str); - size.cx += margins; - size.cy += 2; + size.cx += margins; + size.cy += 2; - item->sX += item->sWidth/2 - short(size.cx/2); - item->sWidth = short(size.cx); - item->sHeight = short(size.cy); + item->sX += item->sWidth/2 - short(size.cx/2); + item->sWidth = short(size.cx); + item->sHeight = short(size.cy); } // Moves every item right and gives it the WS_EX_RIGHT extended style @@ -439,161 +439,161 @@ void CDialogTemplate::ConvertToRTL() { } else addExStyle = true; if (addExStyle) - m_vItems[i]->dwExtStyle |= WS_EX_RIGHT; + m_vItems[i]->dwExtStyle |= WS_EX_RIGHT; m_vItems[i]->sX = m_sWidth - m_vItems[i]->sWidth - m_vItems[i]->sX; - } + } m_dwExtStyle |= WS_EX_RIGHT; } // Saves the dialog in the form of DLGTEMPLATE[EX] BYTE* CDialogTemplate::Save(DWORD& dwSize) { - // We need the size first to know how much memory to allocate - dwSize = GetSize(); - BYTE* pbDlg = new BYTE[dwSize]; - ZeroMemory(pbDlg, dwSize); - BYTE* seeker = pbDlg; + // We need the size first to know how much memory to allocate + dwSize = GetSize(); + BYTE* pbDlg = new BYTE[dwSize]; + ZeroMemory(pbDlg, dwSize); + BYTE* seeker = pbDlg; - if (m_bExtended) { - DLGTEMPLATEEX dh = { - 0x0001, - 0xFFFF, - m_dwHelpId, - m_dwExtStyle, - m_dwStyle, - m_vItems.size(), - m_sX, - m_sY, - m_sWidth, - m_sHeight - }; + if (m_bExtended) { + DLGTEMPLATEEX dh = { + 0x0001, + 0xFFFF, + m_dwHelpId, + m_dwExtStyle, + m_dwStyle, + m_vItems.size(), + m_sX, + m_sY, + m_sWidth, + m_sHeight + }; - CopyMemory(seeker, &dh, sizeof(DLGTEMPLATEEX)); - seeker += sizeof(DLGTEMPLATEEX); - } - else { - DLGTEMPLATE dh = { - m_dwStyle, - m_dwExtStyle, - m_vItems.size(), - m_sX, - m_sY, - m_sWidth, - m_sHeight - }; + CopyMemory(seeker, &dh, sizeof(DLGTEMPLATEEX)); + seeker += sizeof(DLGTEMPLATEEX); + } + else { + DLGTEMPLATE dh = { + m_dwStyle, + m_dwExtStyle, + m_vItems.size(), + m_sX, + m_sY, + m_sWidth, + m_sHeight + }; - CopyMemory(seeker, &dh, sizeof(DLGTEMPLATE)); - seeker += sizeof(DLGTEMPLATE); - } + CopyMemory(seeker, &dh, sizeof(DLGTEMPLATE)); + seeker += sizeof(DLGTEMPLATE); + } - // Write menu variant length array - WriteStringOrId(m_szMenu); - // Write class variant length array - WriteStringOrId(m_szClass); - // Write title variant length array - WriteStringOrId(m_szTitle); + // Write menu variant length array + WriteStringOrId(m_szMenu); + // Write class variant length array + WriteStringOrId(m_szClass); + // Write title variant length array + WriteStringOrId(m_szTitle); - // Write font variant length array, size, and extended info (if needed) - if (m_dwStyle & DS_SETFONT) { - *(short*)seeker = m_sFontSize; - seeker += sizeof(short); - if (m_bExtended) { - *(short*)seeker = m_sFontWeight; - seeker += sizeof(short); - *(BYTE*)seeker = m_bItalic; - seeker += sizeof(BYTE); + // Write font variant length array, size, and extended info (if needed) + if (m_dwStyle & DS_SETFONT) { + *(short*)seeker = m_sFontSize; + seeker += sizeof(short); + if (m_bExtended) { + *(short*)seeker = m_sFontWeight; + seeker += sizeof(short); + *(BYTE*)seeker = m_bItalic; + seeker += sizeof(BYTE); *(BYTE*)seeker = m_bCharset; - seeker += sizeof(BYTE); + seeker += sizeof(BYTE); } - WriteStringOrId(m_szFont); - } + WriteStringOrId(m_szFont); + } - // Write all of the items - for (unsigned int i = 0; i < m_vItems.size(); i++) { - // DLGITEMTEMPLATE[EX]s must be aligned on DWORD boundry - if (DWORD(seeker - pbDlg) % sizeof(DWORD)) - seeker += sizeof(WORD); + // Write all of the items + for (unsigned int i = 0; i < m_vItems.size(); i++) { + // DLGITEMTEMPLATE[EX]s must be aligned on DWORD boundry + if (DWORD(seeker - pbDlg) % sizeof(DWORD)) + seeker += sizeof(WORD); - if (m_bExtended) { - DLGITEMTEMPLATEEX dih = { - m_vItems[i]->dwHelpId, - m_vItems[i]->dwExtStyle, - m_vItems[i]->dwStyle, - m_vItems[i]->sX, - m_vItems[i]->sY, - m_vItems[i]->sWidth, - m_vItems[i]->sHeight, - m_vItems[i]->wId - }; + if (m_bExtended) { + DLGITEMTEMPLATEEX dih = { + m_vItems[i]->dwHelpId, + m_vItems[i]->dwExtStyle, + m_vItems[i]->dwStyle, + m_vItems[i]->sX, + m_vItems[i]->sY, + m_vItems[i]->sWidth, + m_vItems[i]->sHeight, + m_vItems[i]->wId + }; - CopyMemory(seeker, &dih, sizeof(DLGITEMTEMPLATEEX)); - seeker += sizeof(DLGITEMTEMPLATEEX); - } - else { - DLGITEMTEMPLATE dih = { - m_vItems[i]->dwStyle, - m_vItems[i]->dwExtStyle, - m_vItems[i]->sX, - m_vItems[i]->sY, - m_vItems[i]->sWidth, - m_vItems[i]->sHeight, - m_vItems[i]->wId - }; + CopyMemory(seeker, &dih, sizeof(DLGITEMTEMPLATEEX)); + seeker += sizeof(DLGITEMTEMPLATEEX); + } + else { + DLGITEMTEMPLATE dih = { + m_vItems[i]->dwStyle, + m_vItems[i]->dwExtStyle, + m_vItems[i]->sX, + m_vItems[i]->sY, + m_vItems[i]->sWidth, + m_vItems[i]->sHeight, + m_vItems[i]->wId + }; - CopyMemory(seeker, &dih, sizeof(DLGITEMTEMPLATE)); - seeker += sizeof(DLGITEMTEMPLATE); - } + CopyMemory(seeker, &dih, sizeof(DLGITEMTEMPLATE)); + seeker += sizeof(DLGITEMTEMPLATE); + } - // Write class variant length array - WriteStringOrId(m_vItems[i]->szClass); - // Write title variant length array - WriteStringOrId(m_vItems[i]->szTitle); + // Write class variant length array + WriteStringOrId(m_vItems[i]->szClass); + // Write title variant length array + WriteStringOrId(m_vItems[i]->szTitle); - // Write creation data variant length array - // First write its size - if (m_vItems[i]->wCreateDataSize) m_vItems[i]->wCreateDataSize += sizeof(WORD); - *(WORD*)seeker = m_vItems[i]->wCreateDataSize; - seeker += sizeof(WORD); - // If size is nonzero write the data too - if (m_vItems[i]->wCreateDataSize) { - CopyMemory(seeker, m_vItems[i]->szCreationData, m_vItems[i]->wCreateDataSize); - seeker += m_vItems[i]->wCreateDataSize; - } - } + // Write creation data variant length array + // First write its size + if (m_vItems[i]->wCreateDataSize) m_vItems[i]->wCreateDataSize += sizeof(WORD); + *(WORD*)seeker = m_vItems[i]->wCreateDataSize; + seeker += sizeof(WORD); + // If size is nonzero write the data too + if (m_vItems[i]->wCreateDataSize) { + CopyMemory(seeker, m_vItems[i]->szCreationData, m_vItems[i]->wCreateDataSize); + seeker += m_vItems[i]->wCreateDataSize; + } + } - // DONE! - return pbDlg; + // DONE! + return pbDlg; } // Returns the size that the DLGTEMPLATE[EX] will take when saved DWORD CDialogTemplate::GetSize() { - DWORD dwSize = m_bExtended ? sizeof(DLGTEMPLATEEX) : sizeof(DLGTEMPLATE); + DWORD dwSize = m_bExtended ? sizeof(DLGTEMPLATEEX) : sizeof(DLGTEMPLATE); - // Menu - AddStringOrIdSize(m_szMenu); - // Class - AddStringOrIdSize(m_szClass); - // Title - AddStringOrIdSize(m_szTitle); + // Menu + AddStringOrIdSize(m_szMenu); + // Class + AddStringOrIdSize(m_szClass); + // Title + AddStringOrIdSize(m_szTitle); - // Font - if (m_dwStyle & DS_SETFONT) { - dwSize += sizeof(WORD) + (m_bExtended ? sizeof(short) + 2*sizeof(BYTE) : 0); - AddStringOrIdSize(m_szFont); - } + // Font + if (m_dwStyle & DS_SETFONT) { + dwSize += sizeof(WORD) + (m_bExtended ? sizeof(short) + 2*sizeof(BYTE) : 0); + AddStringOrIdSize(m_szFont); + } - for (unsigned int i = 0; i < m_vItems.size(); i++) { - // DLGITEMTEMPLATE[EX]s must be aligned on DWORD boundry - ALIGN(dwSize, sizeof(DWORD)); + for (unsigned int i = 0; i < m_vItems.size(); i++) { + // DLGITEMTEMPLATE[EX]s must be aligned on DWORD boundry + ALIGN(dwSize, sizeof(DWORD)); - dwSize += m_bExtended ? sizeof(DLGITEMTEMPLATEEX) : sizeof(DLGITEMTEMPLATE); + dwSize += m_bExtended ? sizeof(DLGITEMTEMPLATEEX) : sizeof(DLGITEMTEMPLATE); - // Class - AddStringOrIdSize(m_vItems[i]->szClass); - // Title - AddStringOrIdSize(m_vItems[i]->szTitle); + // Class + AddStringOrIdSize(m_vItems[i]->szClass); + // Title + AddStringOrIdSize(m_vItems[i]->szTitle); - dwSize += sizeof(WORD) + m_vItems[i]->wCreateDataSize; - } + dwSize += sizeof(WORD) + m_vItems[i]->wCreateDataSize; + } - return dwSize; + return dwSize; } \ No newline at end of file diff --git a/Source/DialogTemplate.h b/Source/DialogTemplate.h index 44365991..ef40b1c3 100644 --- a/Source/DialogTemplate.h +++ b/Source/DialogTemplate.h @@ -1,23 +1,23 @@ /* - Copyright (C) 2002 Amir Szekely + Copyright (C) 2002 Amir Szekely - This software is provided 'as-is', without any express or implied - warranty. In no event will the authors be held liable for any damages - arising from the use of this software. + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. - Permission is granted to anyone to use this software for any purpose, - including commercial applications, and to alter it and redistribute it - freely, subject to the following restrictions: + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: - 1. The origin of this software must not be misrepresented; you must not - claim that you wrote the original software. If you use this software - in a product, an acknowledgment in the product documentation would be - appreciated but is not required. + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. - 2. Altered source versions must be plainly marked as such, and must not be - misrepresented as being the original software. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. - 3. This notice may not be removed or altered from any source distribution. + 3. This notice may not be removed or altered from any source distribution. */ #if !defined(AFX_DIALOGTEMPLATE_H__C5A973AF_0F56_4BEC_814A_79318E2EB4AC__INCLUDED_) @@ -41,105 +41,105 @@ using namespace std; #endif struct DialogItemTemplate { - DWORD dwHelpId; // Extended only + DWORD dwHelpId; // Extended only - short sX; - short sY; - short sWidth; - short sHeight; - DWORD dwExtStyle; - DWORD dwStyle; - WORD wId; + short sX; + short sY; + short sWidth; + short sHeight; + DWORD dwExtStyle; + DWORD dwStyle; + WORD wId; - char *szClass; - char *szTitle; - char *szCreationData; + char *szClass; + char *szTitle; + char *szCreationData; - WORD wCreateDataSize; + WORD wCreateDataSize; }; #pragma pack(push, 1) typedef struct { - WORD dlgVer; - WORD signature; - DWORD helpID; - DWORD exStyle; - DWORD style; - WORD cDlgItems; - short x; - short y; - short cx; - short cy; + WORD dlgVer; + WORD signature; + DWORD helpID; + DWORD exStyle; + DWORD style; + WORD cDlgItems; + short x; + short y; + short cx; + short cy; } DLGTEMPLATEEX; typedef struct { - DWORD helpID; - DWORD exStyle; - DWORD style; - short x; - short y; - short cx; - short cy; - WORD id; - WORD _miscrosoft_docs_are_wrong; + DWORD helpID; + DWORD exStyle; + DWORD style; + short x; + short y; + short cx; + short cy; + WORD id; + WORD _miscrosoft_docs_are_wrong; } DLGITEMTEMPLATEEX; #pragma pack(pop) class CDialogTemplate { public: - CDialogTemplate(BYTE* pbData, unsigned int uCodePage=CP_ACP); - virtual ~CDialogTemplate(); + CDialogTemplate(BYTE* pbData, unsigned int uCodePage=CP_ACP); + virtual ~CDialogTemplate(); - short GetWidth(); - short GetHeight(); - DialogItemTemplate* GetItem(WORD wId); - DialogItemTemplate* GetItemByIdx(DWORD i); - void RemoveItem(WORD wId); - void SetFont(char* szFaceName, WORD wFontSize); - void AddItem(DialogItemTemplate item); - HWND CreateDummyDialog(); - void MoveAll(short x, short y); - void Resize(short x, short y); - void PixelsToDlgUnits(short& x, short& y); - void DlgUnitsToPixels(short& x, short& y); - SIZE GetStringSize(WORD id, char *str); - void RTrimToString(WORD id, char *str, int margins); - void LTrimToString(WORD id, char *str, int margins); - void CTrimToString(WORD id, char *str, int margins); - void ConvertToRTL(); - BYTE* Save(DWORD& dwSize); - DWORD GetSize(); + short GetWidth(); + short GetHeight(); + DialogItemTemplate* GetItem(WORD wId); + DialogItemTemplate* GetItemByIdx(DWORD i); + void RemoveItem(WORD wId); + void SetFont(char* szFaceName, WORD wFontSize); + void AddItem(DialogItemTemplate item); + HWND CreateDummyDialog(); + void MoveAll(short x, short y); + void Resize(short x, short y); + void PixelsToDlgUnits(short& x, short& y); + void DlgUnitsToPixels(short& x, short& y); + SIZE GetStringSize(WORD id, char *str); + void RTrimToString(WORD id, char *str, int margins); + void LTrimToString(WORD id, char *str, int margins); + void CTrimToString(WORD id, char *str, int margins); + void ConvertToRTL(); + BYTE* Save(DWORD& dwSize); + DWORD GetSize(); private: - bool m_bExtended; + bool m_bExtended; - DWORD m_dwHelpId; // Extended only + DWORD m_dwHelpId; // Extended only - short m_sX; - short m_sY; - short m_sWidth; - short m_sHeight; - DWORD m_dwExtStyle; - DWORD m_dwStyle; + short m_sX; + short m_sY; + short m_sWidth; + short m_sHeight; + DWORD m_dwExtStyle; + DWORD m_dwStyle; - char* m_szMenu; - char* m_szClass; - char* m_szTitle; + char* m_szMenu; + char* m_szClass; + char* m_szTitle; - // Only if DS_FONT style is set - short m_sFontSize; - short m_sFontWeight; // Extended only - BYTE m_bItalic; // Extended only - BYTE m_bCharset; // Extended only - char* m_szFont; + // Only if DS_FONT style is set + short m_sFontSize; + short m_sFontWeight; // Extended only + BYTE m_bItalic; // Extended only + BYTE m_bCharset; // Extended only + char* m_szFont; // For (en/de)coding Unicode unsigned int m_uCodePage; - // Items vector - vector m_vItems; + // Items vector + vector m_vItems; }; #endif // !defined(AFX_DIALOGTEMPLATE_H__C5A973AF_0F56_4BEC_814A_79318E2EB4AC__INCLUDED_) diff --git a/Source/ResourceEditor.cpp b/Source/ResourceEditor.cpp index 1e2e6659..0a6e1598 100644 --- a/Source/ResourceEditor.cpp +++ b/Source/ResourceEditor.cpp @@ -1,23 +1,23 @@ /* - Copyright (C) 2002 Amir Szekely + Copyright (C) 2002 Amir Szekely - This software is provided 'as-is', without any express or implied - warranty. In no event will the authors be held liable for any damages - arising from the use of this software. + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. - Permission is granted to anyone to use this software for any purpose, - including commercial applications, and to alter it and redistribute it - freely, subject to the following restrictions: + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: - 1. The origin of this software must not be misrepresented; you must not - claim that you wrote the original software. If you use this software - in a product, an acknowledgment in the product documentation would be - appreciated but is not required. + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. - 2. Altered source versions must be plainly marked as such, and must not be - misrepresented as being the original software. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. - 3. This notice may not be removed or altered from any source distribution. + 3. This notice may not be removed or altered from any source distribution. */ #define RESOURCE_EDITOR_NO_API @@ -41,51 +41,51 @@ ////////////////////////////////////////////////////////////////////// CResourceEditor::CResourceEditor(BYTE* pbPE, int iSize) { - // Copy the data - m_iSize = iSize; - m_pbPE = new BYTE[iSize]; - CopyMemory(m_pbPE, pbPE, iSize); + // Copy the data + m_iSize = iSize; + m_pbPE = new BYTE[iSize]; + CopyMemory(m_pbPE, pbPE, iSize); - // Get dos header - m_dosHeader = (PIMAGE_DOS_HEADER)m_pbPE; - if (m_dosHeader->e_magic != IMAGE_DOS_SIGNATURE) - throw runtime_error("PE file contains invalid DOS header"); + // Get dos header + m_dosHeader = (PIMAGE_DOS_HEADER)m_pbPE; + if (m_dosHeader->e_magic != IMAGE_DOS_SIGNATURE) + throw runtime_error("PE file contains invalid DOS header"); - // Get NT headers - m_ntHeaders = (PIMAGE_NT_HEADERS)(m_pbPE + m_dosHeader->e_lfanew); - if (m_ntHeaders->Signature != IMAGE_NT_SIGNATURE) - throw runtime_error("PE file missing NT signature"); + // Get NT headers + m_ntHeaders = (PIMAGE_NT_HEADERS)(m_pbPE + m_dosHeader->e_lfanew); + if (m_ntHeaders->Signature != IMAGE_NT_SIGNATURE) + throw runtime_error("PE file missing NT signature"); - // No check sum support yet... - if (m_ntHeaders->OptionalHeader.CheckSum) - throw runtime_error("CResourceEditor doesn't yet support check sum"); + // No check sum support yet... + if (m_ntHeaders->OptionalHeader.CheckSum) + throw runtime_error("CResourceEditor doesn't yet support check sum"); - // Get resource section virtual address - m_dwResourceSectionVA = m_ntHeaders->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_RESOURCE].VirtualAddress; - // Pointer to the sections headers array - PIMAGE_SECTION_HEADER sectionHeadersArray = IMAGE_FIRST_SECTION(m_ntHeaders); + // Get resource section virtual address + m_dwResourceSectionVA = m_ntHeaders->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_RESOURCE].VirtualAddress; + // Pointer to the sections headers array + PIMAGE_SECTION_HEADER sectionHeadersArray = IMAGE_FIRST_SECTION(m_ntHeaders); - // Find resource section index in the array - for (m_dwResourceSectionIndex = 0; m_dwResourceSectionIndex < m_ntHeaders->FileHeader.NumberOfSections; m_dwResourceSectionIndex++) - if (m_dwResourceSectionVA == sectionHeadersArray[m_dwResourceSectionIndex].VirtualAddress) - break; + // Find resource section index in the array + for (m_dwResourceSectionIndex = 0; m_dwResourceSectionIndex < m_ntHeaders->FileHeader.NumberOfSections; m_dwResourceSectionIndex++) + if (m_dwResourceSectionVA == sectionHeadersArray[m_dwResourceSectionIndex].VirtualAddress) + break; - // No resource section... - if (m_dwResourceSectionIndex == m_ntHeaders->FileHeader.NumberOfSections) - throw runtime_error("PE file doesn't contain any resource section"); + // No resource section... + if (m_dwResourceSectionIndex == m_ntHeaders->FileHeader.NumberOfSections) + throw runtime_error("PE file doesn't contain any resource section"); - // Pointer to section data, the first resource directory - PRESOURCE_DIRECTORY rdRoot = PRESOURCE_DIRECTORY(m_pbPE + sectionHeadersArray[m_dwResourceSectionIndex].PointerToRawData); + // Pointer to section data, the first resource directory + PRESOURCE_DIRECTORY rdRoot = PRESOURCE_DIRECTORY(m_pbPE + sectionHeadersArray[m_dwResourceSectionIndex].PointerToRawData); - // Scan the resource directory - m_cResDir = ScanDirectory(rdRoot, rdRoot); + // Scan the resource directory + m_cResDir = ScanDirectory(rdRoot, rdRoot); } CResourceEditor::~CResourceEditor() { - if (m_cResDir) { - m_cResDir->Destroy(); - delete m_cResDir; - } + if (m_cResDir) { + m_cResDir->Destroy(); + delete m_cResDir; + } } ////////////////////////////////////////////////////////////////////// @@ -95,210 +95,210 @@ CResourceEditor::~CResourceEditor() { // Adds/Replaces/Removes a resource. // If lpData is 0 UpdateResource removes the resource. bool CResourceEditor::UpdateResource(char* szType, char* szName, LANGID wLanguage, BYTE* lpData, DWORD dwSize) { - CResourceDirectory* nameDir = 0; - CResourceDirectory* langDir = 0; - CResourceDataEntry* data = 0; - IMAGE_RESOURCE_DIRECTORY rd = {0, /*time(0),*/}; - int iTypeIdx, iNameIdx, iLangIdx; + CResourceDirectory* nameDir = 0; + CResourceDirectory* langDir = 0; + CResourceDataEntry* data = 0; + IMAGE_RESOURCE_DIRECTORY rd = {0, /*time(0),*/}; + int iTypeIdx, iNameIdx, iLangIdx; - iTypeIdx = m_cResDir->Find(szType); - if (iTypeIdx > -1) { - nameDir = m_cResDir->GetEntry(iTypeIdx)->GetSubDirectory(); - iNameIdx = nameDir->Find(szName); - if (iNameIdx > -1) { - langDir = nameDir->GetEntry(iNameIdx)->GetSubDirectory(); - iLangIdx = langDir->Find(wLanguage); - if (iLangIdx > -1) { - data = langDir->GetEntry(iLangIdx)->GetDataEntry(); - } - } - } - - if (lpData) { - // Replace/Add the resource - if (data) { - data->SetData(lpData, dwSize); - return true; - } + iTypeIdx = m_cResDir->Find(szType); + if (iTypeIdx > -1) { + nameDir = m_cResDir->GetEntry(iTypeIdx)->GetSubDirectory(); + iNameIdx = nameDir->Find(szName); + if (iNameIdx > -1) { + langDir = nameDir->GetEntry(iNameIdx)->GetSubDirectory(); + iLangIdx = langDir->Find(wLanguage); + if (iLangIdx > -1) { + data = langDir->GetEntry(iLangIdx)->GetDataEntry(); + } + } + } + + if (lpData) { + // Replace/Add the resource + if (data) { + data->SetData(lpData, dwSize); + return true; + } - if (!nameDir) { - // Type doesn't yet exist - nameDir = new CResourceDirectory(&rd); - m_cResDir->AddEntry(new CResourceDirectoryEntry(szType, nameDir)); - } - if (!langDir) { - // Name doesn't yet exist - langDir = new CResourceDirectory(&rd); - nameDir->AddEntry(new CResourceDirectoryEntry(szName, langDir)); - } - if (!data) { - // Language doesn't yet exist, hence data nither - data = new CResourceDataEntry(lpData, dwSize); - langDir->AddEntry(new CResourceDirectoryEntry(MAKEINTRESOURCE(wLanguage), data)); - } - } - else if (data) { - // Delete the resource - delete data; - langDir->RemoveEntry(iLangIdx); - // Delete directories holding the resource if empty - if (!langDir->CountEntries()) { - delete langDir; - nameDir->RemoveEntry(iNameIdx); - if (!nameDir->CountEntries()) { - delete nameDir; - m_cResDir->RemoveEntry(iTypeIdx); - } - } - } + if (!nameDir) { + // Type doesn't yet exist + nameDir = new CResourceDirectory(&rd); + m_cResDir->AddEntry(new CResourceDirectoryEntry(szType, nameDir)); + } + if (!langDir) { + // Name doesn't yet exist + langDir = new CResourceDirectory(&rd); + nameDir->AddEntry(new CResourceDirectoryEntry(szName, langDir)); + } + if (!data) { + // Language doesn't yet exist, hence data nither + data = new CResourceDataEntry(lpData, dwSize); + langDir->AddEntry(new CResourceDirectoryEntry(MAKEINTRESOURCE(wLanguage), data)); + } + } + else if (data) { + // Delete the resource + delete data; + langDir->RemoveEntry(iLangIdx); + // Delete directories holding the resource if empty + if (!langDir->CountEntries()) { + delete langDir; + nameDir->RemoveEntry(iNameIdx); + if (!nameDir->CountEntries()) { + delete nameDir; + m_cResDir->RemoveEntry(iTypeIdx); + } + } + } else return false; return true; } bool CResourceEditor::UpdateResource(WORD szType, char* szName, LANGID wLanguage, BYTE* lpData, DWORD dwSize) { - return UpdateResource(MAKEINTRESOURCE(szType), szName, wLanguage, lpData, dwSize); + return UpdateResource(MAKEINTRESOURCE(szType), szName, wLanguage, lpData, dwSize); } bool CResourceEditor::UpdateResource(char* szType, WORD szName, LANGID wLanguage, BYTE* lpData, DWORD dwSize) { - return UpdateResource(szType, MAKEINTRESOURCE(szName), wLanguage, lpData, dwSize); + return UpdateResource(szType, MAKEINTRESOURCE(szName), wLanguage, lpData, dwSize); } bool CResourceEditor::UpdateResource(WORD szType, WORD szName, LANGID wLanguage, BYTE* lpData, DWORD dwSize) { - return UpdateResource(MAKEINTRESOURCE(szType), MAKEINTRESOURCE(szName), wLanguage, lpData, dwSize); + return UpdateResource(MAKEINTRESOURCE(szType), MAKEINTRESOURCE(szName), wLanguage, lpData, dwSize); } // Returns a copy of the resource requested // Returns 0 if resource can't be found BYTE* CResourceEditor::GetResource(char* szType, char* szName, LANGID wLanguage) { - CResourceDirectory* nameDir = 0; - CResourceDirectory* langDir = 0; - CResourceDataEntry* data = 0; + CResourceDirectory* nameDir = 0; + CResourceDirectory* langDir = 0; + CResourceDataEntry* data = 0; - int i = m_cResDir->Find(szType); - if (i > -1) { - nameDir = m_cResDir->GetEntry(i)->GetSubDirectory(); - i = nameDir->Find(szName); - if (i > -1) { - langDir = nameDir->GetEntry(i)->GetSubDirectory(); - i = langDir->Find(wLanguage); - if (i > -1) { - data = langDir->GetEntry(i)->GetDataEntry(); - } - } - } + int i = m_cResDir->Find(szType); + if (i > -1) { + nameDir = m_cResDir->GetEntry(i)->GetSubDirectory(); + i = nameDir->Find(szName); + if (i > -1) { + langDir = nameDir->GetEntry(i)->GetSubDirectory(); + i = langDir->Find(wLanguage); + if (i > -1) { + data = langDir->GetEntry(i)->GetDataEntry(); + } + } + } - if (data) { - BYTE* toReturn = new BYTE[data->GetSize()]; - CopyMemory(toReturn, data->GetData(), data->GetSize()); - return toReturn; - } - else - return 0; + if (data) { + BYTE* toReturn = new BYTE[data->GetSize()]; + CopyMemory(toReturn, data->GetData(), data->GetSize()); + return toReturn; + } + else + return 0; } // Saves the edited PE into a buffer and returns it. BYTE* CResourceEditor::Save(DWORD &dwSize) { - unsigned int i; + unsigned int i; - DWORD dwRsrcSize = m_cResDir->GetSize(); // Size of new resource section - DWORD dwRsrcSizeAligned = RALIGN(dwRsrcSize, m_ntHeaders->OptionalHeader.FileAlignment); // Align it to FileAlignment + DWORD dwRsrcSize = m_cResDir->GetSize(); // Size of new resource section + DWORD dwRsrcSizeAligned = RALIGN(dwRsrcSize, m_ntHeaders->OptionalHeader.FileAlignment); // Align it to FileAlignment - // Calculate the total new PE size - dwSize = m_iSize - IMAGE_FIRST_SECTION(m_ntHeaders)[m_dwResourceSectionIndex].SizeOfRawData + dwRsrcSizeAligned; + // Calculate the total new PE size + dwSize = m_iSize - IMAGE_FIRST_SECTION(m_ntHeaders)[m_dwResourceSectionIndex].SizeOfRawData + dwRsrcSizeAligned; - // Allocate memory for the new PE - BYTE* pbNewPE = new BYTE[dwSize]; - // Fill it with zeros - ZeroMemory(pbNewPE, dwSize); + // Allocate memory for the new PE + BYTE* pbNewPE = new BYTE[dwSize]; + // Fill it with zeros + ZeroMemory(pbNewPE, dwSize); - BYTE* seeker = pbNewPE; - BYTE* oldSeeker = m_pbPE; + BYTE* seeker = pbNewPE; + BYTE* oldSeeker = m_pbPE; - // Copy old headers - CopyMemory(seeker, oldSeeker, m_ntHeaders->OptionalHeader.SizeOfHeaders); + // Copy old headers + CopyMemory(seeker, oldSeeker, m_ntHeaders->OptionalHeader.SizeOfHeaders); - // Get new nt headers pointer - PIMAGE_NT_HEADERS ntHeaders = PIMAGE_NT_HEADERS(pbNewPE + PIMAGE_DOS_HEADER(pbNewPE)->e_lfanew); - // Get a pointer to the new section headers - PIMAGE_SECTION_HEADER sectionHeadersArray = IMAGE_FIRST_SECTION(ntHeaders); + // Get new nt headers pointer + PIMAGE_NT_HEADERS ntHeaders = PIMAGE_NT_HEADERS(pbNewPE + PIMAGE_DOS_HEADER(pbNewPE)->e_lfanew); + // Get a pointer to the new section headers + PIMAGE_SECTION_HEADER sectionHeadersArray = IMAGE_FIRST_SECTION(ntHeaders); - // Copy everything between the headers and the sections (Borland stuff...) - CopyMemory(seeker, oldSeeker, sectionHeadersArray[0].PointerToRawData-ntHeaders->OptionalHeader.SizeOfHeaders); + // Copy everything between the headers and the sections (Borland stuff...) + CopyMemory(seeker, oldSeeker, sectionHeadersArray[0].PointerToRawData-ntHeaders->OptionalHeader.SizeOfHeaders); - // Skip some stuff between the headers and the sections (which is??? ask Borland...) - seeker += sectionHeadersArray[0].PointerToRawData-ntHeaders->OptionalHeader.SizeOfHeaders; - oldSeeker += sectionHeadersArray[0].PointerToRawData-ntHeaders->OptionalHeader.SizeOfHeaders; + // Skip some stuff between the headers and the sections (which is??? ask Borland...) + seeker += sectionHeadersArray[0].PointerToRawData-ntHeaders->OptionalHeader.SizeOfHeaders; + oldSeeker += sectionHeadersArray[0].PointerToRawData-ntHeaders->OptionalHeader.SizeOfHeaders; - // Skip the headers - seeker += ntHeaders->OptionalHeader.SizeOfHeaders; - oldSeeker += m_ntHeaders->OptionalHeader.SizeOfHeaders; + // Skip the headers + seeker += ntHeaders->OptionalHeader.SizeOfHeaders; + oldSeeker += m_ntHeaders->OptionalHeader.SizeOfHeaders; - // Copy all of the section up until the resource section - DWORD dwSectionsSize = 0; - for (i = 0; i < m_dwResourceSectionIndex; i++) - dwSectionsSize += sectionHeadersArray[i].SizeOfRawData; + // Copy all of the section up until the resource section + DWORD dwSectionsSize = 0; + for (i = 0; i < m_dwResourceSectionIndex; i++) + dwSectionsSize += sectionHeadersArray[i].SizeOfRawData; - CopyMemory(seeker, oldSeeker, dwSectionsSize); - seeker += dwSectionsSize; - oldSeeker += dwSectionsSize; + CopyMemory(seeker, oldSeeker, dwSectionsSize); + seeker += dwSectionsSize; + oldSeeker += dwSectionsSize; - // Skip the resource section in the old PE seeker. - oldSeeker += sectionHeadersArray[m_dwResourceSectionIndex].SizeOfRawData; + // Skip the resource section in the old PE seeker. + oldSeeker += sectionHeadersArray[m_dwResourceSectionIndex].SizeOfRawData; - // Save the old virtual size of the resource section - DWORD dwOldVirtualSize = sectionHeadersArray[m_dwResourceSectionIndex].Misc.VirtualSize; + // Save the old virtual size of the resource section + DWORD dwOldVirtualSize = sectionHeadersArray[m_dwResourceSectionIndex].Misc.VirtualSize; - // Set the new size of the resource section (size aligned to FileAlignment) - sectionHeadersArray[m_dwResourceSectionIndex].SizeOfRawData = dwRsrcSizeAligned; - // Set the virtual size as well (in memory) - sectionHeadersArray[m_dwResourceSectionIndex].Misc.VirtualSize = RALIGN(dwRsrcSize, ntHeaders->OptionalHeader.SectionAlignment); - ntHeaders->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_RESOURCE].Size = sectionHeadersArray[m_dwResourceSectionIndex].Misc.VirtualSize; + // Set the new size of the resource section (size aligned to FileAlignment) + sectionHeadersArray[m_dwResourceSectionIndex].SizeOfRawData = dwRsrcSizeAligned; + // Set the virtual size as well (in memory) + sectionHeadersArray[m_dwResourceSectionIndex].Misc.VirtualSize = RALIGN(dwRsrcSize, ntHeaders->OptionalHeader.SectionAlignment); + ntHeaders->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_RESOURCE].Size = sectionHeadersArray[m_dwResourceSectionIndex].Misc.VirtualSize; - // Set the new virtual size of the image - DWORD old = ntHeaders->OptionalHeader.SizeOfImage; - ntHeaders->OptionalHeader.SizeOfImage = RALIGN(ntHeaders->OptionalHeader.SizeOfHeaders, ntHeaders->OptionalHeader.SectionAlignment); - for (int j = 0; j < ntHeaders->FileHeader.NumberOfSections; j++) - ntHeaders->OptionalHeader.SizeOfImage += RALIGN(sectionHeadersArray[j].Misc.VirtualSize, ntHeaders->OptionalHeader.SectionAlignment); + // Set the new virtual size of the image + DWORD old = ntHeaders->OptionalHeader.SizeOfImage; + ntHeaders->OptionalHeader.SizeOfImage = RALIGN(ntHeaders->OptionalHeader.SizeOfHeaders, ntHeaders->OptionalHeader.SectionAlignment); + for (int j = 0; j < ntHeaders->FileHeader.NumberOfSections; j++) + ntHeaders->OptionalHeader.SizeOfImage += RALIGN(sectionHeadersArray[j].Misc.VirtualSize, ntHeaders->OptionalHeader.SectionAlignment); - // Set the new AddressOfEntryPoint if needed - if (ntHeaders->OptionalHeader.AddressOfEntryPoint > sectionHeadersArray[m_dwResourceSectionIndex].VirtualAddress) - ntHeaders->OptionalHeader.AddressOfEntryPoint += sectionHeadersArray[m_dwResourceSectionIndex].Misc.VirtualSize - dwOldVirtualSize; + // Set the new AddressOfEntryPoint if needed + if (ntHeaders->OptionalHeader.AddressOfEntryPoint > sectionHeadersArray[m_dwResourceSectionIndex].VirtualAddress) + ntHeaders->OptionalHeader.AddressOfEntryPoint += sectionHeadersArray[m_dwResourceSectionIndex].Misc.VirtualSize - dwOldVirtualSize; - // Set the new BaseOfCode if needed - if (ntHeaders->OptionalHeader.BaseOfCode > sectionHeadersArray[m_dwResourceSectionIndex].VirtualAddress) - ntHeaders->OptionalHeader.BaseOfCode += sectionHeadersArray[m_dwResourceSectionIndex].Misc.VirtualSize - dwOldVirtualSize; + // Set the new BaseOfCode if needed + if (ntHeaders->OptionalHeader.BaseOfCode > sectionHeadersArray[m_dwResourceSectionIndex].VirtualAddress) + ntHeaders->OptionalHeader.BaseOfCode += sectionHeadersArray[m_dwResourceSectionIndex].Misc.VirtualSize - dwOldVirtualSize; - // Set the new BaseOfData if needed - if (ntHeaders->OptionalHeader.BaseOfData > sectionHeadersArray[m_dwResourceSectionIndex].VirtualAddress) - ntHeaders->OptionalHeader.BaseOfData += sectionHeadersArray[m_dwResourceSectionIndex].Misc.VirtualSize - dwOldVirtualSize; + // Set the new BaseOfData if needed + if (ntHeaders->OptionalHeader.BaseOfData > sectionHeadersArray[m_dwResourceSectionIndex].VirtualAddress) + ntHeaders->OptionalHeader.BaseOfData += sectionHeadersArray[m_dwResourceSectionIndex].Misc.VirtualSize - dwOldVirtualSize; - // Refresh the headers of the sections that come after the resource section, and the data directory - for (i++; i < ntHeaders->FileHeader.NumberOfSections; i++) { + // Refresh the headers of the sections that come after the resource section, and the data directory + for (i++; i < ntHeaders->FileHeader.NumberOfSections; i++) { if ( sectionHeadersArray[i].PointerToRawData ) { sectionHeadersArray[i].PointerToRawData -= IMAGE_FIRST_SECTION(m_ntHeaders)[m_dwResourceSectionIndex].SizeOfRawData; sectionHeadersArray[i].PointerToRawData += dwRsrcSizeAligned; } - int secInDataDir = 0; - for (unsigned int j = 0; j < ntHeaders->OptionalHeader.NumberOfRvaAndSizes; j++) - if (ntHeaders->OptionalHeader.DataDirectory[j].VirtualAddress == sectionHeadersArray[i].VirtualAddress) - secInDataDir = j; - sectionHeadersArray[i].VirtualAddress -= RALIGN(dwOldVirtualSize, ntHeaders->OptionalHeader.SectionAlignment); - sectionHeadersArray[i].VirtualAddress += RALIGN(sectionHeadersArray[m_dwResourceSectionIndex].Misc.VirtualSize, ntHeaders->OptionalHeader.SectionAlignment); - if (secInDataDir) - ntHeaders->OptionalHeader.DataDirectory[secInDataDir].VirtualAddress = sectionHeadersArray[i].VirtualAddress; - } + int secInDataDir = 0; + for (unsigned int j = 0; j < ntHeaders->OptionalHeader.NumberOfRvaAndSizes; j++) + if (ntHeaders->OptionalHeader.DataDirectory[j].VirtualAddress == sectionHeadersArray[i].VirtualAddress) + secInDataDir = j; + sectionHeadersArray[i].VirtualAddress -= RALIGN(dwOldVirtualSize, ntHeaders->OptionalHeader.SectionAlignment); + sectionHeadersArray[i].VirtualAddress += RALIGN(sectionHeadersArray[m_dwResourceSectionIndex].Misc.VirtualSize, ntHeaders->OptionalHeader.SectionAlignment); + if (secInDataDir) + ntHeaders->OptionalHeader.DataDirectory[secInDataDir].VirtualAddress = sectionHeadersArray[i].VirtualAddress; + } - // Write the resource section - WriteRsrcSec(seeker); - // Advance the pointer - seeker += dwRsrcSizeAligned; + // Write the resource section + WriteRsrcSec(seeker); + // Advance the pointer + seeker += dwRsrcSizeAligned; - // Write all the sections that come after the resource section - dwSectionsSize = 0; - for (i = m_dwResourceSectionIndex + 1; i < m_ntHeaders->FileHeader.NumberOfSections; i++) - dwSectionsSize += sectionHeadersArray[i].SizeOfRawData; + // Write all the sections that come after the resource section + dwSectionsSize = 0; + for (i = m_dwResourceSectionIndex + 1; i < m_ntHeaders->FileHeader.NumberOfSections; i++) + dwSectionsSize += sectionHeadersArray[i].SizeOfRawData; - CopyMemory(seeker, oldSeeker, dwSectionsSize); + CopyMemory(seeker, oldSeeker, dwSectionsSize); seeker += dwSectionsSize; oldSeeker += dwSectionsSize; @@ -310,9 +310,9 @@ BYTE* CResourceEditor::Save(DWORD &dwSize) { seeker += dwTackedSize; oldSeeker += dwTackedSize; - /********************************************************** - * To add checksum to the header use MapFileAndCheckSum - **********************************************************/ + /********************************************************** + * To add checksum to the header use MapFileAndCheckSum + **********************************************************/ // From now on, we are working on the new PE // Freeing the old PE memory is up to the user @@ -324,7 +324,7 @@ BYTE* CResourceEditor::Save(DWORD &dwSize) { // m_dwResourceSectionIndex and m_dwResourceSectionVA have also been left unchanged as // we didn't move the resources section - return pbNewPE; + return pbNewPE; } // This function scans exe sections and after find a match with given name @@ -333,8 +333,8 @@ bool CResourceEditor::AddExtraVirtualSize2PESection(const char* pszSectionName, { PIMAGE_SECTION_HEADER sectionHeadersArray = IMAGE_FIRST_SECTION(m_ntHeaders); - // Refresh the headers of the sections that come after the resource section, and the data directory - for (int i =0; i < m_ntHeaders->FileHeader.NumberOfSections; i++) { + // Refresh the headers of the sections that come after the resource section, and the data directory + for (int i =0; i < m_ntHeaders->FileHeader.NumberOfSections; i++) { if ( !strcmp((LPCSTR)sectionHeadersArray[i].Name, pszSectionName) ) { sectionHeadersArray[i].Misc.VirtualSize += addsize; sectionHeadersArray[i].Characteristics &= ~IMAGE_SCN_MEM_DISCARDABLE; @@ -352,7 +352,7 @@ bool CResourceEditor::AddExtraVirtualSize2PESection(const char* pszSectionName, return true; } - } + } return false; } @@ -364,175 +364,175 @@ bool CResourceEditor::AddExtraVirtualSize2PESection(const char* pszSectionName, // This function scans a give resource directory and return a CResourceDirectory object // rdRoot must point to the root directory of the resource section CResourceDirectory* CResourceEditor::ScanDirectory(PRESOURCE_DIRECTORY rdRoot, PRESOURCE_DIRECTORY rdToScan) { - // Create CResourceDirectory from rdToScan - CResourceDirectory* rdc = new CResourceDirectory(PIMAGE_RESOURCE_DIRECTORY(rdToScan)); - char* szName; - PIMAGE_RESOURCE_DATA_ENTRY rde; + // Create CResourceDirectory from rdToScan + CResourceDirectory* rdc = new CResourceDirectory(PIMAGE_RESOURCE_DIRECTORY(rdToScan)); + char* szName; + PIMAGE_RESOURCE_DATA_ENTRY rde; - // Go through all entries of this resource directory - for (int i = 0; i < rdToScan->Header.NumberOfNamedEntries + rdToScan->Header.NumberOfIdEntries; i++) { - // If this entry points to data entry get a pointer to it - if (!rdToScan->Entries[i].DataIsDirectory) - rde = PIMAGE_RESOURCE_DATA_ENTRY(rdToScan->Entries[i].OffsetToData + (BYTE*)rdRoot); + // Go through all entries of this resource directory + for (int i = 0; i < rdToScan->Header.NumberOfNamedEntries + rdToScan->Header.NumberOfIdEntries; i++) { + // If this entry points to data entry get a pointer to it + if (!rdToScan->Entries[i].DataIsDirectory) + rde = PIMAGE_RESOURCE_DATA_ENTRY(rdToScan->Entries[i].OffsetToData + (BYTE*)rdRoot); - // If this entry has a name, translate it from Unicode - if (rdToScan->Entries[i].NameIsString) { - PIMAGE_RESOURCE_DIR_STRING_U rds = PIMAGE_RESOURCE_DIR_STRING_U(rdToScan->Entries[i].NameOffset + (char*)rdRoot); + // If this entry has a name, translate it from Unicode + if (rdToScan->Entries[i].NameIsString) { + PIMAGE_RESOURCE_DIR_STRING_U rds = PIMAGE_RESOURCE_DIR_STRING_U(rdToScan->Entries[i].NameOffset + (char*)rdRoot); - int mbsSize = WideCharToMultiByte(CP_ACP, 0, rds->NameString, rds->Length, 0, 0, 0, 0); - szName = new char[mbsSize+1]; - WideCharToMultiByte(CP_ACP, 0, rds->NameString, rds->Length, szName, mbsSize, 0, 0); - szName[mbsSize] = 0; - } - // Else, set the name to this entry's id - else - szName = MAKEINTRESOURCE(rdToScan->Entries[i].Id); + int mbsSize = WideCharToMultiByte(CP_ACP, 0, rds->NameString, rds->Length, 0, 0, 0, 0); + szName = new char[mbsSize+1]; + WideCharToMultiByte(CP_ACP, 0, rds->NameString, rds->Length, szName, mbsSize, 0, 0); + szName[mbsSize] = 0; + } + // Else, set the name to this entry's id + else + szName = MAKEINTRESOURCE(rdToScan->Entries[i].Id); - if (rdToScan->Entries[i].DataIsDirectory) - rdc->AddEntry( - new CResourceDirectoryEntry( - szName, - ScanDirectory( - rdRoot, - PRESOURCE_DIRECTORY(rdToScan->Entries[i].OffsetToDirectory + (BYTE*)rdRoot) - ) - ) - ); - else - rdc->AddEntry( - new CResourceDirectoryEntry( - szName, - new CResourceDataEntry( - (BYTE*)rdRoot + rde->OffsetToData - m_dwResourceSectionVA, - rde->Size, - rde->CodePage - ) - ) - ); + if (rdToScan->Entries[i].DataIsDirectory) + rdc->AddEntry( + new CResourceDirectoryEntry( + szName, + ScanDirectory( + rdRoot, + PRESOURCE_DIRECTORY(rdToScan->Entries[i].OffsetToDirectory + (BYTE*)rdRoot) + ) + ) + ); + else + rdc->AddEntry( + new CResourceDirectoryEntry( + szName, + new CResourceDataEntry( + (BYTE*)rdRoot + rde->OffsetToData - m_dwResourceSectionVA, + rde->Size, + rde->CodePage + ) + ) + ); - // Delete the dynamicly allocated name if it is a name and not an id - if (!IS_INTRESOURCE(szName)) - delete [] szName; - } + // Delete the dynamicly allocated name if it is a name and not an id + if (!IS_INTRESOURCE(szName)) + delete [] szName; + } - return rdc; + return rdc; } // This function writes into a given place in memory (pbRsrcSec) the edited resource section void CResourceEditor::WriteRsrcSec(BYTE* pbRsrcSec) { - BYTE* seeker = pbRsrcSec; + BYTE* seeker = pbRsrcSec; - queue qDirs; // Used to scan the tree by level - queue qDataEntries; // Used for writing the data entries - queue qDataEntries2; // Used for writing raw resources data - queue qStrings; // Used for writing resources' names + queue qDirs; // Used to scan the tree by level + queue qDataEntries; // Used for writing the data entries + queue qDataEntries2; // Used for writing raw resources data + queue qStrings; // Used for writing resources' names - qDirs.push(m_cResDir); + qDirs.push(m_cResDir); - while (!qDirs.empty()) { - CResourceDirectory* crd = qDirs.front(); + while (!qDirs.empty()) { + CResourceDirectory* crd = qDirs.front(); - CopyMemory(seeker, &crd->GetInfo(), sizeof(IMAGE_RESOURCE_DIRECTORY)); - crd->m_dwWrittenAt = DWORD(seeker); - seeker += sizeof(IMAGE_RESOURCE_DIRECTORY); + CopyMemory(seeker, &crd->GetInfo(), sizeof(IMAGE_RESOURCE_DIRECTORY)); + crd->m_dwWrittenAt = DWORD(seeker); + seeker += sizeof(IMAGE_RESOURCE_DIRECTORY); - for (int i = 0; i < crd->CountEntries(); i++) { - if (crd->GetEntry(i)->HasName()) - qStrings.push(crd->GetEntry(i)); - if (crd->GetEntry(i)->IsDataDirectory()) - qDirs.push(crd->GetEntry(i)->GetSubDirectory()); - else { - qDataEntries.push(crd->GetEntry(i)->GetDataEntry()); - qDataEntries2.push(crd->GetEntry(i)->GetDataEntry()); - } + for (int i = 0; i < crd->CountEntries(); i++) { + if (crd->GetEntry(i)->HasName()) + qStrings.push(crd->GetEntry(i)); + if (crd->GetEntry(i)->IsDataDirectory()) + qDirs.push(crd->GetEntry(i)->GetSubDirectory()); + else { + qDataEntries.push(crd->GetEntry(i)->GetDataEntry()); + qDataEntries2.push(crd->GetEntry(i)->GetDataEntry()); + } - IMAGE_RESOURCE_DIRECTORY_ENTRY rDirE = {0,}; - rDirE.DataIsDirectory = crd->GetEntry(i)->IsDataDirectory(); - rDirE.Id = (crd->GetEntry(i)->HasName()) ? 0 : crd->GetEntry(i)->GetId(); - rDirE.NameIsString = (crd->GetEntry(i)->HasName()) ? 1 : 0; + IMAGE_RESOURCE_DIRECTORY_ENTRY rDirE = {0,}; + rDirE.DataIsDirectory = crd->GetEntry(i)->IsDataDirectory(); + rDirE.Id = (crd->GetEntry(i)->HasName()) ? 0 : crd->GetEntry(i)->GetId(); + rDirE.NameIsString = (crd->GetEntry(i)->HasName()) ? 1 : 0; - CopyMemory(seeker, &rDirE, sizeof(IMAGE_RESOURCE_DIRECTORY_ENTRY)); - crd->GetEntry(i)->m_dwWrittenAt = DWORD(seeker); - seeker += sizeof(IMAGE_RESOURCE_DIRECTORY_ENTRY); - } - qDirs.pop(); - } + CopyMemory(seeker, &rDirE, sizeof(IMAGE_RESOURCE_DIRECTORY_ENTRY)); + crd->GetEntry(i)->m_dwWrittenAt = DWORD(seeker); + seeker += sizeof(IMAGE_RESOURCE_DIRECTORY_ENTRY); + } + qDirs.pop(); + } - /* - * Write IMAGE_RESOURCE_DATA_ENTRYs. - */ - while (!qDataEntries.empty()) { - CResourceDataEntry* cRDataE = qDataEntries.front(); - IMAGE_RESOURCE_DATA_ENTRY rDataE = {0,}; - rDataE.CodePage = cRDataE->GetCodePage(); - rDataE.Size = cRDataE->GetSize(); + /* + * Write IMAGE_RESOURCE_DATA_ENTRYs. + */ + while (!qDataEntries.empty()) { + CResourceDataEntry* cRDataE = qDataEntries.front(); + IMAGE_RESOURCE_DATA_ENTRY rDataE = {0,}; + rDataE.CodePage = cRDataE->GetCodePage(); + rDataE.Size = cRDataE->GetSize(); - CopyMemory(seeker, &rDataE, sizeof(IMAGE_RESOURCE_DATA_ENTRY)); - cRDataE->m_dwWrittenAt = DWORD(seeker); - seeker += sizeof(IMAGE_RESOURCE_DATA_ENTRY); + CopyMemory(seeker, &rDataE, sizeof(IMAGE_RESOURCE_DATA_ENTRY)); + cRDataE->m_dwWrittenAt = DWORD(seeker); + seeker += sizeof(IMAGE_RESOURCE_DATA_ENTRY); - qDataEntries.pop(); - } + qDataEntries.pop(); + } - /* - * Write strings - */ - while (!qStrings.empty()) { - CResourceDirectoryEntry* cRDirE = qStrings.front(); + /* + * Write strings + */ + while (!qStrings.empty()) { + CResourceDirectoryEntry* cRDirE = qStrings.front(); - PIMAGE_RESOURCE_DIRECTORY_ENTRY(cRDirE->m_dwWrittenAt)->NameOffset = DWORD(seeker) - DWORD(pbRsrcSec); + PIMAGE_RESOURCE_DIRECTORY_ENTRY(cRDirE->m_dwWrittenAt)->NameOffset = DWORD(seeker) - DWORD(pbRsrcSec); - char* szName = cRDirE->GetName(); - WORD iLen = lstrlen(szName); - WCHAR* szwName = new WCHAR[iLen]; - // MultiByteToWideChar return value includes the null char, so -1 - iLen = MultiByteToWideChar(CP_ACP, 0, szName, iLen, szwName, iLen) - 1; - *(WORD*)seeker = iLen; - seeker += sizeof(WORD); - CopyMemory(seeker, szwName, iLen*sizeof(WCHAR)); - seeker += iLen*sizeof(WCHAR); + char* szName = cRDirE->GetName(); + WORD iLen = lstrlen(szName); + WCHAR* szwName = new WCHAR[iLen]; + // MultiByteToWideChar return value includes the null char, so -1 + iLen = MultiByteToWideChar(CP_ACP, 0, szName, iLen, szwName, iLen) - 1; + *(WORD*)seeker = iLen; + seeker += sizeof(WORD); + CopyMemory(seeker, szwName, iLen*sizeof(WCHAR)); + seeker += iLen*sizeof(WCHAR); - // Even though the number of chars is predefined a null termination is required - *(WORD*)seeker = 0; - seeker += sizeof(WORD); + // Even though the number of chars is predefined a null termination is required + *(WORD*)seeker = 0; + seeker += sizeof(WORD); - delete [] szName; - delete [] szwName; + delete [] szName; + delete [] szwName; - qStrings.pop(); - } + qStrings.pop(); + } - /* - * Write raw resource data and set offsets in IMAGE_RESOURCE_DATA_ENTRYs. - */ - while (!qDataEntries2.empty()) { - CResourceDataEntry* cRDataE = qDataEntries2.front(); - CopyMemory(seeker, cRDataE->GetData(), cRDataE->GetSize()); - PIMAGE_RESOURCE_DATA_ENTRY(cRDataE->m_dwWrittenAt)->OffsetToData = seeker - pbRsrcSec + m_dwResourceSectionVA; + /* + * Write raw resource data and set offsets in IMAGE_RESOURCE_DATA_ENTRYs. + */ + while (!qDataEntries2.empty()) { + CResourceDataEntry* cRDataE = qDataEntries2.front(); + CopyMemory(seeker, cRDataE->GetData(), cRDataE->GetSize()); + PIMAGE_RESOURCE_DATA_ENTRY(cRDataE->m_dwWrittenAt)->OffsetToData = seeker - pbRsrcSec + m_dwResourceSectionVA; - seeker += RALIGN(cRDataE->GetSize(), 8); + seeker += RALIGN(cRDataE->GetSize(), 8); - qDataEntries2.pop(); - } + qDataEntries2.pop(); + } - /* - * Set all of the directory entries offsets. - */ - SetOffsets(m_cResDir, DWORD(pbRsrcSec)); + /* + * Set all of the directory entries offsets. + */ + SetOffsets(m_cResDir, DWORD(pbRsrcSec)); } // Sets the offsets in directory entries void CResourceEditor::SetOffsets(CResourceDirectory* resDir, DWORD newResDirAt) { - for (int i = 0; i < resDir->CountEntries(); i++) { - if (resDir->GetEntry(i)->IsDataDirectory()) { - PIMAGE_RESOURCE_DIRECTORY_ENTRY(resDir->GetEntry(i)->m_dwWrittenAt)->DataIsDirectory = 1; - PIMAGE_RESOURCE_DIRECTORY_ENTRY(resDir->GetEntry(i)->m_dwWrittenAt)->OffsetToDirectory = resDir->GetEntry(i)->GetSubDirectory()->m_dwWrittenAt - newResDirAt; - SetOffsets(resDir->GetEntry(i)->GetSubDirectory(), newResDirAt); - } - else { - PIMAGE_RESOURCE_DIRECTORY_ENTRY(resDir->GetEntry(i)->m_dwWrittenAt)->OffsetToData = resDir->GetEntry(i)->GetDataEntry()->m_dwWrittenAt - newResDirAt; - } - } + for (int i = 0; i < resDir->CountEntries(); i++) { + if (resDir->GetEntry(i)->IsDataDirectory()) { + PIMAGE_RESOURCE_DIRECTORY_ENTRY(resDir->GetEntry(i)->m_dwWrittenAt)->DataIsDirectory = 1; + PIMAGE_RESOURCE_DIRECTORY_ENTRY(resDir->GetEntry(i)->m_dwWrittenAt)->OffsetToDirectory = resDir->GetEntry(i)->GetSubDirectory()->m_dwWrittenAt - newResDirAt; + SetOffsets(resDir->GetEntry(i)->GetSubDirectory(), newResDirAt); + } + else { + PIMAGE_RESOURCE_DIRECTORY_ENTRY(resDir->GetEntry(i)->m_dwWrittenAt)->OffsetToData = resDir->GetEntry(i)->GetDataEntry()->m_dwWrittenAt - newResDirAt; + } + } } ////////////////////////////////////////////////////////////////////// @@ -546,9 +546,9 @@ void CResourceEditor::SetOffsets(CResourceDirectory* resDir, DWORD newResDirAt) ////////////////////////////////////////////////////////////////////// CResourceDirectory::CResourceDirectory(PIMAGE_RESOURCE_DIRECTORY prd) { - m_rdDir = *prd; - m_rdDir.NumberOfIdEntries = 0; - m_rdDir.NumberOfNamedEntries = 0; + m_rdDir = *prd; + m_rdDir.NumberOfIdEntries = 0; + m_rdDir.NumberOfNamedEntries = 0; } CResourceDirectory::~CResourceDirectory() { @@ -559,127 +559,127 @@ CResourceDirectory::~CResourceDirectory() { ////////////////////////////////////////////////////////////////////// IMAGE_RESOURCE_DIRECTORY CResourceDirectory::GetInfo() { - return m_rdDir; + return m_rdDir; } CResourceDirectoryEntry* CResourceDirectory::GetEntry(unsigned int i) { - if (m_vEntries.size() < i) - return 0; - return m_vEntries[i]; + if (m_vEntries.size() < i) + return 0; + return m_vEntries[i]; } // This function inserts a new directory entry // It also keeps the directory entries sorted void CResourceDirectory::AddEntry(CResourceDirectoryEntry* entry) { - int i = 0; - if (entry->HasName()) { - char* szEntName = entry->GetName(); - for (i = 0; i < m_rdDir.NumberOfIdEntries; i++) { - char* szName = m_vEntries[i]->GetName(); - int cmp = lstrcmp(szName, szEntName); - delete [] szName; - if (cmp == 0) { - delete [] szEntName; - return; - } - if (cmp > 0) - break; - } - delete [] szEntName; - m_rdDir.NumberOfNamedEntries++; - } - else { - for (i = m_rdDir.NumberOfNamedEntries; i < m_rdDir.NumberOfNamedEntries+m_rdDir.NumberOfIdEntries; i++) { - if (m_vEntries[i]->GetId() == entry->GetId()) - return; - if (m_vEntries[i]->GetId() > entry->GetId()) - break; - } - m_rdDir.NumberOfIdEntries++; - } - m_vEntries.insert(m_vEntries.begin() + i, entry); + int i = 0; + if (entry->HasName()) { + char* szEntName = entry->GetName(); + for (i = 0; i < m_rdDir.NumberOfIdEntries; i++) { + char* szName = m_vEntries[i]->GetName(); + int cmp = lstrcmp(szName, szEntName); + delete [] szName; + if (cmp == 0) { + delete [] szEntName; + return; + } + if (cmp > 0) + break; + } + delete [] szEntName; + m_rdDir.NumberOfNamedEntries++; + } + else { + for (i = m_rdDir.NumberOfNamedEntries; i < m_rdDir.NumberOfNamedEntries+m_rdDir.NumberOfIdEntries; i++) { + if (m_vEntries[i]->GetId() == entry->GetId()) + return; + if (m_vEntries[i]->GetId() > entry->GetId()) + break; + } + m_rdDir.NumberOfIdEntries++; + } + m_vEntries.insert(m_vEntries.begin() + i, entry); } void CResourceDirectory::RemoveEntry(int i) { - if (m_vEntries[i]->HasName()) - m_rdDir.NumberOfNamedEntries--; - else - m_rdDir.NumberOfIdEntries--; - delete m_vEntries[i]; - m_vEntries.erase(m_vEntries.begin() + i); + if (m_vEntries[i]->HasName()) + m_rdDir.NumberOfNamedEntries--; + else + m_rdDir.NumberOfIdEntries--; + delete m_vEntries[i]; + m_vEntries.erase(m_vEntries.begin() + i); } int CResourceDirectory::CountEntries() { - return m_vEntries.size(); + return m_vEntries.size(); } // Returns the index of a directory entry with the specified name // Name can be a string or an id // Returns -1 if can not be found int CResourceDirectory::Find(char* szName) { - if (IS_INTRESOURCE(szName)) - return Find(WORD(szName)); - else - if (szName[0] == '#') - return Find(WORD(atoi(szName+1))); + if (IS_INTRESOURCE(szName)) + return Find(WORD(szName)); + else + if (szName[0] == '#') + return Find(WORD(atoi(szName+1))); - for (unsigned int i = 0; i < m_vEntries.size(); i++) { - if (!m_vEntries[i]->HasName()) - continue; + for (unsigned int i = 0; i < m_vEntries.size(); i++) { + if (!m_vEntries[i]->HasName()) + continue; - char* szEntName = m_vEntries[i]->GetName(); - int cmp = lstrcmp(szName, szEntName); - delete [] szEntName; + char* szEntName = m_vEntries[i]->GetName(); + int cmp = lstrcmp(szName, szEntName); + delete [] szEntName; - if (!cmp) - return i; - } + if (!cmp) + return i; + } - return -1; + return -1; } // Returns the index of a directory entry with the specified id // Returns -1 if can not be found int CResourceDirectory::Find(WORD wId) { - for (unsigned int i = 0; i < m_vEntries.size(); i++) { - if (m_vEntries[i]->HasName()) - continue; + for (unsigned int i = 0; i < m_vEntries.size(); i++) { + if (m_vEntries[i]->HasName()) + continue; - if (wId == m_vEntries[i]->GetId()) - return i; - } + if (wId == m_vEntries[i]->GetId()) + return i; + } - return -1; + return -1; } // Get the size of this resource directory (including all of its children) DWORD CResourceDirectory::GetSize() { - DWORD dwSize = sizeof(IMAGE_RESOURCE_DIRECTORY); - for (unsigned int i = 0; i < m_vEntries.size(); i++) { - dwSize += sizeof(IMAGE_RESOURCE_DIRECTORY_ENTRY); - if (m_vEntries[i]->HasName()) - dwSize += sizeof(IMAGE_RESOURCE_DIR_STRING_U) + m_vEntries[i]->GetNameLength()*sizeof(WCHAR); - if (m_vEntries[i]->IsDataDirectory()) - dwSize += m_vEntries[i]->GetSubDirectory()->GetSize(); - else { - DWORD dwAligned = m_vEntries[i]->GetDataEntry()->GetSize(); - ALIGN(dwAligned, 8); - dwSize += sizeof(IMAGE_RESOURCE_DATA_ENTRY) + dwAligned; - } - } - return dwSize; + DWORD dwSize = sizeof(IMAGE_RESOURCE_DIRECTORY); + for (unsigned int i = 0; i < m_vEntries.size(); i++) { + dwSize += sizeof(IMAGE_RESOURCE_DIRECTORY_ENTRY); + if (m_vEntries[i]->HasName()) + dwSize += sizeof(IMAGE_RESOURCE_DIR_STRING_U) + m_vEntries[i]->GetNameLength()*sizeof(WCHAR); + if (m_vEntries[i]->IsDataDirectory()) + dwSize += m_vEntries[i]->GetSubDirectory()->GetSize(); + else { + DWORD dwAligned = m_vEntries[i]->GetDataEntry()->GetSize(); + ALIGN(dwAligned, 8); + dwSize += sizeof(IMAGE_RESOURCE_DATA_ENTRY) + dwAligned; + } + } + return dwSize; } // Destroys this directory and all of its children void CResourceDirectory::Destroy() { - for (unsigned int i = 0; i < m_vEntries.size(); i++) { - if (m_vEntries[i]->IsDataDirectory()) { - m_vEntries[i]->GetSubDirectory()->Destroy(); - delete m_vEntries[i]->GetSubDirectory(); - } - else - delete m_vEntries[i]->GetDataEntry(); - } + for (unsigned int i = 0; i < m_vEntries.size(); i++) { + if (m_vEntries[i]->IsDataDirectory()) { + m_vEntries[i]->GetSubDirectory()->Destroy(); + delete m_vEntries[i]->GetSubDirectory(); + } + else + delete m_vEntries[i]->GetDataEntry(); + } } ////////////////////////////////////////////////////////////////////// @@ -693,38 +693,38 @@ void CResourceDirectory::Destroy() { ////////////////////////////////////////////////////////////////////// CResourceDirectoryEntry::CResourceDirectoryEntry(char* szName, CResourceDirectory* rdSubDir) { - if (IS_INTRESOURCE(szName)) { - m_bHasName = false; - m_szName = 0; - m_wId = WORD(szName); - } - else { - m_bHasName = true; - m_szName = new char[lstrlen(szName)]; - lstrcpy(m_szName, szName); - } - m_bIsDataDirectory = true; - m_rdSubDir = rdSubDir; + if (IS_INTRESOURCE(szName)) { + m_bHasName = false; + m_szName = 0; + m_wId = WORD(szName); + } + else { + m_bHasName = true; + m_szName = new char[lstrlen(szName)]; + lstrcpy(m_szName, szName); + } + m_bIsDataDirectory = true; + m_rdSubDir = rdSubDir; } CResourceDirectoryEntry::CResourceDirectoryEntry(char* szName, CResourceDataEntry* rdeData) { - if (IS_INTRESOURCE(szName)) { - m_bHasName = false; - m_szName = 0; - m_wId = WORD(szName); - } - else { - m_bHasName = true; - m_szName = new char[lstrlen(szName)]; - lstrcpy(m_szName, szName); - } - m_bIsDataDirectory = false; - m_rdeData = rdeData; + if (IS_INTRESOURCE(szName)) { + m_bHasName = false; + m_szName = 0; + m_wId = WORD(szName); + } + else { + m_bHasName = true; + m_szName = new char[lstrlen(szName)]; + lstrcpy(m_szName, szName); + } + m_bIsDataDirectory = false; + m_rdeData = rdeData; } CResourceDirectoryEntry::~CResourceDirectoryEntry() { - if (m_szName && m_bHasName) - delete [] m_szName; + if (m_szName && m_bHasName) + delete [] m_szName; } ////////////////////////////////////////////////////////////////////// @@ -732,43 +732,43 @@ CResourceDirectoryEntry::~CResourceDirectoryEntry() { ////////////////////////////////////////////////////////////////////// bool CResourceDirectoryEntry::HasName() { - return m_bHasName; + return m_bHasName; } // Don't forget to free the memory used by the string after usage! char* CResourceDirectoryEntry::GetName() { - if (!m_bHasName) - return 0; - char* szName = 0; - szName = new char[lstrlen(m_szName)]; - lstrcpy(szName, m_szName); - return szName; + if (!m_bHasName) + return 0; + char* szName = 0; + szName = new char[lstrlen(m_szName)]; + lstrcpy(szName, m_szName); + return szName; } int CResourceDirectoryEntry::GetNameLength() { - return lstrlen(m_szName); + return lstrlen(m_szName); } WORD CResourceDirectoryEntry::GetId() { - if (m_bHasName) - return 0; - return m_wId; + if (m_bHasName) + return 0; + return m_wId; } bool CResourceDirectoryEntry::IsDataDirectory() { - return m_bIsDataDirectory; + return m_bIsDataDirectory; } CResourceDirectory* CResourceDirectoryEntry::GetSubDirectory() { - if (!m_bIsDataDirectory) - return NULL; - return m_rdSubDir; + if (!m_bIsDataDirectory) + return NULL; + return m_rdSubDir; } CResourceDataEntry* CResourceDirectoryEntry::GetDataEntry() { - if (m_bIsDataDirectory) - return NULL; - return m_rdeData; + if (m_bIsDataDirectory) + return NULL; + return m_rdeData; } ////////////////////////////////////////////////////////////////////// @@ -782,13 +782,13 @@ CResourceDataEntry* CResourceDirectoryEntry::GetDataEntry() { ////////////////////////////////////////////////////////////////////// CResourceDataEntry::CResourceDataEntry(BYTE* pbData, DWORD dwSize, DWORD dwCodePage) { - m_pbData = 0; - SetData(pbData, dwSize, dwCodePage); + m_pbData = 0; + SetData(pbData, dwSize, dwCodePage); } CResourceDataEntry::~CResourceDataEntry() { - if (m_pbData) - delete [] m_pbData; + if (m_pbData) + delete [] m_pbData; } ////////////////////////////////////////////////////////////////////// @@ -798,25 +798,25 @@ CResourceDataEntry::~CResourceDataEntry() { // To save memory this function doesn't give you a copy of the data // Don't mess with the data returned from this function! BYTE* CResourceDataEntry::GetData() { - return m_pbData; + return m_pbData; } void CResourceDataEntry::SetData(BYTE* pbData, DWORD dwSize) { - SetData(pbData, dwSize, m_dwCodePage); + SetData(pbData, dwSize, m_dwCodePage); } void CResourceDataEntry::SetData(BYTE* pbData, DWORD dwSize, DWORD dwCodePage) { - if (m_pbData) delete [] m_pbData; - m_pbData = new BYTE[dwSize]; - CopyMemory(m_pbData, pbData, dwSize); - m_dwSize = dwSize; - m_dwCodePage = dwCodePage; + if (m_pbData) delete [] m_pbData; + m_pbData = new BYTE[dwSize]; + CopyMemory(m_pbData, pbData, dwSize); + m_dwSize = dwSize; + m_dwCodePage = dwCodePage; } DWORD CResourceDataEntry::GetSize() { - return m_dwSize; + return m_dwSize; } DWORD CResourceDataEntry::GetCodePage() { - return m_dwCodePage; + return m_dwCodePage; } \ No newline at end of file diff --git a/Source/ResourceEditor.h b/Source/ResourceEditor.h index 65b84d71..89f3e9a2 100644 --- a/Source/ResourceEditor.h +++ b/Source/ResourceEditor.h @@ -1,23 +1,23 @@ /* - Copyright (C) 2002 Amir Szekely + Copyright (C) 2002 Amir Szekely - This software is provided 'as-is', without any express or implied - warranty. In no event will the authors be held liable for any damages - arising from the use of this software. + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. - Permission is granted to anyone to use this software for any purpose, - including commercial applications, and to alter it and redistribute it - freely, subject to the following restrictions: + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: - 1. The origin of this software must not be misrepresented; you must not - claim that you wrote the original software. If you use this software - in a product, an acknowledgment in the product documentation would be - appreciated but is not required. + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. - 2. Altered source versions must be plainly marked as such, and must not be - misrepresented as being the original software. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. - 3. This notice may not be removed or altered from any source distribution. + 3. This notice may not be removed or altered from any source distribution. */ #if !defined(AFX_RESOURCEEDITOR_H__683BF710_E805_4093_975B_D5729186A89A__INCLUDED_) @@ -53,121 +53,121 @@ class CResourceDataEntry; // Resource directory with entries typedef struct RESOURCE_DIRECTORY { - IMAGE_RESOURCE_DIRECTORY Header; - IMAGE_RESOURCE_DIRECTORY_ENTRY Entries[1]; + IMAGE_RESOURCE_DIRECTORY Header; + IMAGE_RESOURCE_DIRECTORY_ENTRY Entries[1]; } *PRESOURCE_DIRECTORY; class CResourceEditor { public: - CResourceEditor(BYTE* pbPE, int iSize); - virtual ~CResourceEditor(); + CResourceEditor(BYTE* pbPE, int iSize); + virtual ~CResourceEditor(); - bool UpdateResource(char* szType, char* szName, LANGID wLanguage, BYTE* lpData, DWORD dwSize); - bool UpdateResource(WORD szType, char* szName, LANGID wLanguage, BYTE* lpData, DWORD dwSize); - bool UpdateResource(char* szType, WORD szName, LANGID wLanguage, BYTE* lpData, DWORD dwSize); - bool UpdateResource(WORD szType, WORD szName, LANGID wLanguage, BYTE* lpData, DWORD dwSize); - BYTE* GetResource(char* szType, char* szName, LANGID wLanguage); + bool UpdateResource(char* szType, char* szName, LANGID wLanguage, BYTE* lpData, DWORD dwSize); + bool UpdateResource(WORD szType, char* szName, LANGID wLanguage, BYTE* lpData, DWORD dwSize); + bool UpdateResource(char* szType, WORD szName, LANGID wLanguage, BYTE* lpData, DWORD dwSize); + bool UpdateResource(WORD szType, WORD szName, LANGID wLanguage, BYTE* lpData, DWORD dwSize); + BYTE* GetResource(char* szType, char* szName, LANGID wLanguage); - bool AddExtraVirtualSize2PESection(const char* pszSectionName, int addsize); - BYTE* Save(DWORD &dwSize); + bool AddExtraVirtualSize2PESection(const char* pszSectionName, int addsize); + BYTE* Save(DWORD &dwSize); private: - BYTE* m_pbPE; - int m_iSize; + BYTE* m_pbPE; + int m_iSize; - PIMAGE_DOS_HEADER m_dosHeader; - PIMAGE_NT_HEADERS m_ntHeaders; + PIMAGE_DOS_HEADER m_dosHeader; + PIMAGE_NT_HEADERS m_ntHeaders; - DWORD m_dwResourceSectionIndex; - DWORD m_dwResourceSectionVA; + DWORD m_dwResourceSectionIndex; + DWORD m_dwResourceSectionVA; - CResourceDirectory* m_cResDir; + CResourceDirectory* m_cResDir; - CResourceDirectory* ScanDirectory(PRESOURCE_DIRECTORY rdRoot, PRESOURCE_DIRECTORY rdToScan); + CResourceDirectory* ScanDirectory(PRESOURCE_DIRECTORY rdRoot, PRESOURCE_DIRECTORY rdToScan); - void WriteRsrcSec(BYTE* pbRsrcSec); - void SetOffsets(CResourceDirectory* resDir, DWORD newResDirAt); + void WriteRsrcSec(BYTE* pbRsrcSec); + void SetOffsets(CResourceDirectory* resDir, DWORD newResDirAt); }; #ifdef RESOURCE_EDITOR_NO_API class CResourceDirectory { public: - CResourceDirectory(PIMAGE_RESOURCE_DIRECTORY prd); - virtual ~CResourceDirectory(); + CResourceDirectory(PIMAGE_RESOURCE_DIRECTORY prd); + virtual ~CResourceDirectory(); - IMAGE_RESOURCE_DIRECTORY GetInfo(); + IMAGE_RESOURCE_DIRECTORY GetInfo(); - CResourceDirectoryEntry* GetEntry(unsigned int i); - void AddEntry(CResourceDirectoryEntry* entry); - void RemoveEntry(int i); - int CountEntries(); - int Find(char* szName); - int Find(WORD wId); + CResourceDirectoryEntry* GetEntry(unsigned int i); + void AddEntry(CResourceDirectoryEntry* entry); + void RemoveEntry(int i); + int CountEntries(); + int Find(char* szName); + int Find(WORD wId); - DWORD GetSize(); + DWORD GetSize(); - void Destroy(); + void Destroy(); - DWORD m_dwWrittenAt; + DWORD m_dwWrittenAt; private: - IMAGE_RESOURCE_DIRECTORY m_rdDir; - vector m_vEntries; + IMAGE_RESOURCE_DIRECTORY m_rdDir; + vector m_vEntries; }; class CResourceDirectoryEntry { public: - CResourceDirectoryEntry(char* szName, CResourceDirectory* rdSubDir); - CResourceDirectoryEntry(char* szName, CResourceDataEntry* rdeData); - virtual ~CResourceDirectoryEntry(); + CResourceDirectoryEntry(char* szName, CResourceDirectory* rdSubDir); + CResourceDirectoryEntry(char* szName, CResourceDataEntry* rdeData); + virtual ~CResourceDirectoryEntry(); - bool HasName(); - char* GetName(); - int GetNameLength(); + bool HasName(); + char* GetName(); + int GetNameLength(); - WORD GetId(); + WORD GetId(); - bool IsDataDirectory(); - CResourceDirectory* GetSubDirectory(); + bool IsDataDirectory(); + CResourceDirectory* GetSubDirectory(); - CResourceDataEntry* GetDataEntry(); + CResourceDataEntry* GetDataEntry(); - DWORD m_dwWrittenAt; + DWORD m_dwWrittenAt; private: - bool m_bHasName; - union { - char* m_szName; - WORD m_wId; - }; + bool m_bHasName; + union { + char* m_szName; + WORD m_wId; + }; - bool m_bIsDataDirectory; - union { - CResourceDirectory* m_rdSubDir; - CResourceDataEntry* m_rdeData; - }; + bool m_bIsDataDirectory; + union { + CResourceDirectory* m_rdSubDir; + CResourceDataEntry* m_rdeData; + }; }; class CResourceDataEntry { public: - CResourceDataEntry(BYTE* pbData, DWORD dwSize, DWORD dwCodePage = 0); - ~CResourceDataEntry(); + CResourceDataEntry(BYTE* pbData, DWORD dwSize, DWORD dwCodePage = 0); + ~CResourceDataEntry(); - BYTE* GetData(); + BYTE* GetData(); - void SetData(BYTE* pbData, DWORD dwSize); - void SetData(BYTE* pbData, DWORD dwSize, DWORD dwCodePage); + void SetData(BYTE* pbData, DWORD dwSize); + void SetData(BYTE* pbData, DWORD dwSize, DWORD dwCodePage); - DWORD GetSize(); - DWORD GetCodePage(); + DWORD GetSize(); + DWORD GetCodePage(); - DWORD m_dwWrittenAt; + DWORD m_dwWrittenAt; private: - BYTE* m_pbData; - DWORD m_dwSize; - DWORD m_dwCodePage; + BYTE* m_pbData; + DWORD m_dwSize; + DWORD m_dwCodePage; }; #endif // #ifdef RESOURCE_EDITOR_NO_API