1*cdf0e10cSrcweir /************************************************************************* 2*cdf0e10cSrcweir * 3*cdf0e10cSrcweir * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4*cdf0e10cSrcweir * 5*cdf0e10cSrcweir * Copyright 2000, 2010 Oracle and/or its affiliates. 6*cdf0e10cSrcweir * 7*cdf0e10cSrcweir * OpenOffice.org - a multi-platform office productivity suite 8*cdf0e10cSrcweir * 9*cdf0e10cSrcweir * This file is part of OpenOffice.org. 10*cdf0e10cSrcweir * 11*cdf0e10cSrcweir * OpenOffice.org is free software: you can redistribute it and/or modify 12*cdf0e10cSrcweir * it under the terms of the GNU Lesser General Public License version 3 13*cdf0e10cSrcweir * only, as published by the Free Software Foundation. 14*cdf0e10cSrcweir * 15*cdf0e10cSrcweir * OpenOffice.org is distributed in the hope that it will be useful, 16*cdf0e10cSrcweir * but WITHOUT ANY WARRANTY; without even the implied warranty of 17*cdf0e10cSrcweir * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18*cdf0e10cSrcweir * GNU Lesser General Public License version 3 for more details 19*cdf0e10cSrcweir * (a copy is included in the LICENSE file that accompanied this code). 20*cdf0e10cSrcweir * 21*cdf0e10cSrcweir * You should have received a copy of the GNU Lesser General Public License 22*cdf0e10cSrcweir * version 3 along with OpenOffice.org. If not, see 23*cdf0e10cSrcweir * <http://www.openoffice.org/license.html> 24*cdf0e10cSrcweir * for a copy of the LGPLv3 License. 25*cdf0e10cSrcweir * 26*cdf0e10cSrcweir ************************************************************************/ 27*cdf0e10cSrcweir 28*cdf0e10cSrcweir #include "precompiled_desktop.hxx" 29*cdf0e10cSrcweir #define UNICODE 30*cdf0e10cSrcweir #define _UNICODE 31*cdf0e10cSrcweir 32*cdf0e10cSrcweir #define WIN32_LEAN_AND_MEAN 33*cdf0e10cSrcweir #if defined _MSC_VER 34*cdf0e10cSrcweir #pragma warning(push, 1) 35*cdf0e10cSrcweir #endif 36*cdf0e10cSrcweir #include <windows.h> 37*cdf0e10cSrcweir #include <shellapi.h> 38*cdf0e10cSrcweir #include <imagehlp.h> 39*cdf0e10cSrcweir #include <wchar.h> 40*cdf0e10cSrcweir #if defined _MSC_VER 41*cdf0e10cSrcweir #pragma warning(pop) 42*cdf0e10cSrcweir #endif 43*cdf0e10cSrcweir 44*cdf0e10cSrcweir #include <time.h> 45*cdf0e10cSrcweir #include "sal/config.h" 46*cdf0e10cSrcweir #include "tools/pathutils.hxx" 47*cdf0e10cSrcweir 48*cdf0e10cSrcweir #define MY_LENGTH(s) (sizeof (s) / sizeof *(s) - 1) 49*cdf0e10cSrcweir #define MY_STRING(s) (s), MY_LENGTH(s) 50*cdf0e10cSrcweir 51*cdf0e10cSrcweir const int FORMAT_MESSAGE_SIZE = 4096; 52*cdf0e10cSrcweir const DWORD PE_Signature = 0x00004550; 53*cdf0e10cSrcweir const DWORD BASEVIRTUALADDRESS = 0x10000000; 54*cdf0e10cSrcweir 55*cdf0e10cSrcweir namespace 56*cdf0e10cSrcweir { 57*cdf0e10cSrcweir 58*cdf0e10cSrcweir bool IsValidHandle( HANDLE handle ) 59*cdf0e10cSrcweir { 60*cdf0e10cSrcweir return ((NULL != handle) && (INVALID_HANDLE_VALUE != handle)); 61*cdf0e10cSrcweir } 62*cdf0e10cSrcweir 63*cdf0e10cSrcweir void fail() 64*cdf0e10cSrcweir { 65*cdf0e10cSrcweir LPWSTR buf = NULL; 66*cdf0e10cSrcweir FormatMessageW( 67*cdf0e10cSrcweir FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, NULL, 68*cdf0e10cSrcweir GetLastError(), 0, reinterpret_cast< LPWSTR >(&buf), 0, NULL); 69*cdf0e10cSrcweir MessageBoxW(NULL, buf, NULL, MB_OK | MB_ICONERROR); 70*cdf0e10cSrcweir LocalFree(buf); 71*cdf0e10cSrcweir TerminateProcess(GetCurrentProcess(), 255); 72*cdf0e10cSrcweir } 73*cdf0e10cSrcweir 74*cdf0e10cSrcweir bool rebaseImage( wchar_t* pszFilePath, ULONG nNewImageBase) 75*cdf0e10cSrcweir { 76*cdf0e10cSrcweir ULONG ulOldImageSize; 77*cdf0e10cSrcweir ULONG_PTR lpOldImageBase; 78*cdf0e10cSrcweir ULONG ulNewImageSize; 79*cdf0e10cSrcweir ULONG_PTR lpNewImageBase = nNewImageBase; 80*cdf0e10cSrcweir ULONG ulDateTimeStamp = 0; 81*cdf0e10cSrcweir bool bResult(false); 82*cdf0e10cSrcweir 83*cdf0e10cSrcweir char cszFilePath[_MAX_PATH+1] = {0}; 84*cdf0e10cSrcweir int nResult = WideCharToMultiByte(CP_ACP, 0, pszFilePath, -1, cszFilePath, _MAX_PATH, NULL, NULL); 85*cdf0e10cSrcweir 86*cdf0e10cSrcweir if (nResult != 0) 87*cdf0e10cSrcweir { 88*cdf0e10cSrcweir BOOL bResult = ReBaseImage( 89*cdf0e10cSrcweir cszFilePath, 90*cdf0e10cSrcweir "", 91*cdf0e10cSrcweir TRUE, 92*cdf0e10cSrcweir FALSE, 93*cdf0e10cSrcweir FALSE, 94*cdf0e10cSrcweir 0, 95*cdf0e10cSrcweir &ulOldImageSize, 96*cdf0e10cSrcweir &lpOldImageBase, 97*cdf0e10cSrcweir &ulNewImageSize, 98*cdf0e10cSrcweir &lpNewImageBase, 99*cdf0e10cSrcweir ulDateTimeStamp ); 100*cdf0e10cSrcweir } 101*cdf0e10cSrcweir 102*cdf0e10cSrcweir return bResult; 103*cdf0e10cSrcweir } 104*cdf0e10cSrcweir 105*cdf0e10cSrcweir wchar_t* getBrandPath(wchar_t * path) 106*cdf0e10cSrcweir { 107*cdf0e10cSrcweir DWORD n = GetModuleFileNameW(NULL, path, MAX_PATH); 108*cdf0e10cSrcweir if (n == 0 || n >= MAX_PATH) { 109*cdf0e10cSrcweir exit(EXIT_FAILURE); 110*cdf0e10cSrcweir } 111*cdf0e10cSrcweir return tools::filename(path); 112*cdf0e10cSrcweir } 113*cdf0e10cSrcweir 114*cdf0e10cSrcweir void rebaseImagesInFolder( wchar_t* pszFolder, DWORD nNewImageBase ) 115*cdf0e10cSrcweir { 116*cdf0e10cSrcweir wchar_t szPattern[MAX_PATH]; 117*cdf0e10cSrcweir wchar_t *lpLastSlash = wcsrchr( pszFolder, '\\' ); 118*cdf0e10cSrcweir if ( lpLastSlash ) 119*cdf0e10cSrcweir { 120*cdf0e10cSrcweir size_t len = lpLastSlash - pszFolder + 1; 121*cdf0e10cSrcweir wcsncpy( szPattern, pszFolder, len ); 122*cdf0e10cSrcweir wcsncpy( szPattern + len, TEXT("*.dll"), sizeof(szPattern)/sizeof(szPattern[0]) - len ); 123*cdf0e10cSrcweir } 124*cdf0e10cSrcweir 125*cdf0e10cSrcweir WIN32_FIND_DATA aFindFileData; 126*cdf0e10cSrcweir HANDLE hFind = FindFirstFile( szPattern, &aFindFileData ); 127*cdf0e10cSrcweir 128*cdf0e10cSrcweir if ( IsValidHandle(hFind) ) 129*cdf0e10cSrcweir { 130*cdf0e10cSrcweir BOOL fSuccess = false; 131*cdf0e10cSrcweir 132*cdf0e10cSrcweir do 133*cdf0e10cSrcweir { 134*cdf0e10cSrcweir wchar_t szLibFilePath[MAX_PATH]; 135*cdf0e10cSrcweir wchar_t *lpLastSlash = wcsrchr( pszFolder, '\\' ); 136*cdf0e10cSrcweir if ( lpLastSlash ) 137*cdf0e10cSrcweir { 138*cdf0e10cSrcweir size_t len = lpLastSlash - pszFolder + 1; 139*cdf0e10cSrcweir wcsncpy( szLibFilePath, pszFolder, len ); 140*cdf0e10cSrcweir wcsncpy( szLibFilePath + len, aFindFileData.cFileName, sizeof(szLibFilePath)/sizeof(szLibFilePath[0]) - len ); 141*cdf0e10cSrcweir } 142*cdf0e10cSrcweir 143*cdf0e10cSrcweir rebaseImage( szLibFilePath, nNewImageBase ); 144*cdf0e10cSrcweir fSuccess = FindNextFile( hFind, &aFindFileData ); 145*cdf0e10cSrcweir } 146*cdf0e10cSrcweir while ( fSuccess ); 147*cdf0e10cSrcweir 148*cdf0e10cSrcweir FindClose( hFind ); 149*cdf0e10cSrcweir } 150*cdf0e10cSrcweir } 151*cdf0e10cSrcweir 152*cdf0e10cSrcweir } 153*cdf0e10cSrcweir 154*cdf0e10cSrcweir extern "C" int APIENTRY WinMain( HINSTANCE, HINSTANCE, LPSTR, int ) 155*cdf0e10cSrcweir { 156*cdf0e10cSrcweir wchar_t path[MAX_PATH]; 157*cdf0e10cSrcweir 158*cdf0e10cSrcweir wchar_t * pathEnd = getBrandPath(path); 159*cdf0e10cSrcweir 160*cdf0e10cSrcweir if (tools::buildPath(path, path, pathEnd, MY_STRING(L"")) == NULL) 161*cdf0e10cSrcweir fail(); 162*cdf0e10cSrcweir rebaseImagesInFolder(path, BASEVIRTUALADDRESS); 163*cdf0e10cSrcweir 164*cdf0e10cSrcweir if (tools::buildPath(path, path, pathEnd, MY_STRING(L"..\\basis-link")) == NULL) 165*cdf0e10cSrcweir fail(); 166*cdf0e10cSrcweir pathEnd = tools::resolveLink(path); 167*cdf0e10cSrcweir 168*cdf0e10cSrcweir if ( pathEnd == NULL ) 169*cdf0e10cSrcweir return 0; 170*cdf0e10cSrcweir 171*cdf0e10cSrcweir if (tools::buildPath(path, path, pathEnd, MY_STRING(L"\\program\\")) == NULL) 172*cdf0e10cSrcweir fail(); 173*cdf0e10cSrcweir rebaseImagesInFolder(path, BASEVIRTUALADDRESS); 174*cdf0e10cSrcweir 175*cdf0e10cSrcweir if (tools::buildPath(path, path, pathEnd, MY_STRING(L"\\ure-link")) == NULL) 176*cdf0e10cSrcweir fail(); 177*cdf0e10cSrcweir pathEnd = tools::resolveLink(path); 178*cdf0e10cSrcweir 179*cdf0e10cSrcweir if ( pathEnd == NULL ) 180*cdf0e10cSrcweir return 0; 181*cdf0e10cSrcweir 182*cdf0e10cSrcweir if (tools::buildPath(path, path, pathEnd, MY_STRING(L"\\bin\\")) == NULL) 183*cdf0e10cSrcweir fail(); 184*cdf0e10cSrcweir rebaseImagesInFolder(path, BASEVIRTUALADDRESS); 185*cdf0e10cSrcweir 186*cdf0e10cSrcweir return 0; 187*cdf0e10cSrcweir } 188