xref: /trunk/main/desktop/win32/source/rebase/rebase.cxx (revision cf6516809c57e1bb0a940545cca99cdad54d4ce2)
1*2722ceddSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*2722ceddSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*2722ceddSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*2722ceddSAndrew Rist  * distributed with this work for additional information
6*2722ceddSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*2722ceddSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*2722ceddSAndrew Rist  * "License"); you may not use this file except in compliance
9*2722ceddSAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
11*2722ceddSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
13*2722ceddSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*2722ceddSAndrew Rist  * software distributed under the License is distributed on an
15*2722ceddSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*2722ceddSAndrew Rist  * KIND, either express or implied.  See the License for the
17*2722ceddSAndrew Rist  * specific language governing permissions and limitations
18*2722ceddSAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
20*2722ceddSAndrew Rist  *************************************************************/
21*2722ceddSAndrew Rist 
22*2722ceddSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir #include "precompiled_desktop.hxx"
25cdf0e10cSrcweir #define UNICODE
26cdf0e10cSrcweir #define _UNICODE
27cdf0e10cSrcweir 
28cdf0e10cSrcweir #define WIN32_LEAN_AND_MEAN
29cdf0e10cSrcweir #if defined _MSC_VER
30cdf0e10cSrcweir #pragma warning(push, 1)
31cdf0e10cSrcweir #endif
32cdf0e10cSrcweir #include <windows.h>
33cdf0e10cSrcweir #include <shellapi.h>
34cdf0e10cSrcweir #include <imagehlp.h>
35cdf0e10cSrcweir #include <wchar.h>
36cdf0e10cSrcweir #if defined _MSC_VER
37cdf0e10cSrcweir #pragma warning(pop)
38cdf0e10cSrcweir #endif
39cdf0e10cSrcweir 
40cdf0e10cSrcweir #include <time.h>
41cdf0e10cSrcweir #include "sal/config.h"
42cdf0e10cSrcweir #include "tools/pathutils.hxx"
43cdf0e10cSrcweir 
44cdf0e10cSrcweir #define MY_LENGTH(s) (sizeof (s) / sizeof *(s) - 1)
45cdf0e10cSrcweir #define MY_STRING(s) (s), MY_LENGTH(s)
46cdf0e10cSrcweir 
47cdf0e10cSrcweir const int   FORMAT_MESSAGE_SIZE = 4096;
48cdf0e10cSrcweir const DWORD PE_Signature        = 0x00004550;
49cdf0e10cSrcweir const DWORD BASEVIRTUALADDRESS  = 0x10000000;
50cdf0e10cSrcweir 
51cdf0e10cSrcweir namespace
52cdf0e10cSrcweir {
53cdf0e10cSrcweir 
IsValidHandle(HANDLE handle)54cdf0e10cSrcweir bool IsValidHandle( HANDLE handle )
55cdf0e10cSrcweir {
56cdf0e10cSrcweir     return ((NULL != handle) && (INVALID_HANDLE_VALUE != handle));
57cdf0e10cSrcweir }
58cdf0e10cSrcweir 
fail()59cdf0e10cSrcweir void fail()
60cdf0e10cSrcweir {
61cdf0e10cSrcweir     LPWSTR buf = NULL;
62cdf0e10cSrcweir     FormatMessageW(
63cdf0e10cSrcweir         FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, NULL,
64cdf0e10cSrcweir         GetLastError(), 0, reinterpret_cast< LPWSTR >(&buf), 0, NULL);
65cdf0e10cSrcweir     MessageBoxW(NULL, buf, NULL, MB_OK | MB_ICONERROR);
66cdf0e10cSrcweir     LocalFree(buf);
67cdf0e10cSrcweir     TerminateProcess(GetCurrentProcess(), 255);
68cdf0e10cSrcweir }
69cdf0e10cSrcweir 
rebaseImage(wchar_t * pszFilePath,ULONG nNewImageBase)70cdf0e10cSrcweir bool rebaseImage( wchar_t* pszFilePath, ULONG nNewImageBase)
71cdf0e10cSrcweir {
72cdf0e10cSrcweir     ULONG ulOldImageSize;
73cdf0e10cSrcweir     ULONG_PTR lpOldImageBase;
74cdf0e10cSrcweir     ULONG ulNewImageSize;
75cdf0e10cSrcweir     ULONG_PTR lpNewImageBase  = nNewImageBase;
76cdf0e10cSrcweir     ULONG     ulDateTimeStamp = 0;
77cdf0e10cSrcweir     bool      bResult(false);
78cdf0e10cSrcweir 
79cdf0e10cSrcweir     char cszFilePath[_MAX_PATH+1] = {0};
80cdf0e10cSrcweir     int nResult = WideCharToMultiByte(CP_ACP, 0, pszFilePath, -1, cszFilePath, _MAX_PATH, NULL, NULL);
81cdf0e10cSrcweir 
82cdf0e10cSrcweir     if (nResult != 0)
83cdf0e10cSrcweir     {
84cdf0e10cSrcweir         BOOL bResult = ReBaseImage(
85cdf0e10cSrcweir             cszFilePath,
86cdf0e10cSrcweir             "",
87cdf0e10cSrcweir             TRUE,
88cdf0e10cSrcweir             FALSE,
89cdf0e10cSrcweir             FALSE,
90cdf0e10cSrcweir             0,
91cdf0e10cSrcweir             &ulOldImageSize,
92cdf0e10cSrcweir             &lpOldImageBase,
93cdf0e10cSrcweir             &ulNewImageSize,
94cdf0e10cSrcweir             &lpNewImageBase,
95cdf0e10cSrcweir             ulDateTimeStamp );
96cdf0e10cSrcweir     }
97cdf0e10cSrcweir 
98cdf0e10cSrcweir     return bResult;
99cdf0e10cSrcweir }
100cdf0e10cSrcweir 
getBrandPath(wchar_t * path)101cdf0e10cSrcweir wchar_t* getBrandPath(wchar_t * path)
102cdf0e10cSrcweir {
103cdf0e10cSrcweir     DWORD n = GetModuleFileNameW(NULL, path, MAX_PATH);
104cdf0e10cSrcweir     if (n == 0 || n >= MAX_PATH) {
105cdf0e10cSrcweir         exit(EXIT_FAILURE);
106cdf0e10cSrcweir     }
107cdf0e10cSrcweir     return tools::filename(path);
108cdf0e10cSrcweir }
109cdf0e10cSrcweir 
rebaseImagesInFolder(wchar_t * pszFolder,DWORD nNewImageBase)110cdf0e10cSrcweir void rebaseImagesInFolder( wchar_t* pszFolder, DWORD nNewImageBase )
111cdf0e10cSrcweir {
112cdf0e10cSrcweir     wchar_t szPattern[MAX_PATH];
113cdf0e10cSrcweir     wchar_t *lpLastSlash = wcsrchr( pszFolder, '\\' );
114cdf0e10cSrcweir     if ( lpLastSlash )
115cdf0e10cSrcweir     {
116cdf0e10cSrcweir         size_t len = lpLastSlash - pszFolder + 1;
117cdf0e10cSrcweir         wcsncpy( szPattern, pszFolder, len );
118cdf0e10cSrcweir         wcsncpy( szPattern + len, TEXT("*.dll"), sizeof(szPattern)/sizeof(szPattern[0]) - len );
119cdf0e10cSrcweir     }
120cdf0e10cSrcweir 
121cdf0e10cSrcweir     WIN32_FIND_DATA aFindFileData;
122cdf0e10cSrcweir     HANDLE  hFind = FindFirstFile( szPattern, &aFindFileData );
123cdf0e10cSrcweir 
124cdf0e10cSrcweir     if ( IsValidHandle(hFind) )
125cdf0e10cSrcweir     {
126cdf0e10cSrcweir         BOOL fSuccess = false;
127cdf0e10cSrcweir 
128cdf0e10cSrcweir         do
129cdf0e10cSrcweir         {
130cdf0e10cSrcweir             wchar_t szLibFilePath[MAX_PATH];
131cdf0e10cSrcweir             wchar_t *lpLastSlash = wcsrchr( pszFolder, '\\' );
132cdf0e10cSrcweir             if ( lpLastSlash )
133cdf0e10cSrcweir             {
134cdf0e10cSrcweir                 size_t len = lpLastSlash - pszFolder + 1;
135cdf0e10cSrcweir                 wcsncpy( szLibFilePath, pszFolder, len );
136cdf0e10cSrcweir                 wcsncpy( szLibFilePath + len, aFindFileData.cFileName, sizeof(szLibFilePath)/sizeof(szLibFilePath[0]) - len );
137cdf0e10cSrcweir             }
138cdf0e10cSrcweir 
139cdf0e10cSrcweir             rebaseImage( szLibFilePath, nNewImageBase );
140cdf0e10cSrcweir             fSuccess = FindNextFile( hFind, &aFindFileData );
141cdf0e10cSrcweir         }
142cdf0e10cSrcweir         while ( fSuccess );
143cdf0e10cSrcweir 
144cdf0e10cSrcweir         FindClose( hFind );
145cdf0e10cSrcweir     }
146cdf0e10cSrcweir }
147cdf0e10cSrcweir 
148cdf0e10cSrcweir }
149cdf0e10cSrcweir 
WinMain(HINSTANCE,HINSTANCE,LPSTR,int)150cdf0e10cSrcweir extern "C" int APIENTRY WinMain( HINSTANCE, HINSTANCE, LPSTR, int )
151cdf0e10cSrcweir {
152cdf0e10cSrcweir     wchar_t path[MAX_PATH];
153cdf0e10cSrcweir 
154cdf0e10cSrcweir     wchar_t * pathEnd = getBrandPath(path);
155cdf0e10cSrcweir 
156cdf0e10cSrcweir     if (tools::buildPath(path, path, pathEnd, MY_STRING(L"")) == NULL)
157cdf0e10cSrcweir         fail();
158cdf0e10cSrcweir     rebaseImagesInFolder(path, BASEVIRTUALADDRESS);
159cdf0e10cSrcweir 
160cdf0e10cSrcweir     if (tools::buildPath(path, path, pathEnd, MY_STRING(L"..\\basis-link")) == NULL)
161cdf0e10cSrcweir         fail();
162cdf0e10cSrcweir     pathEnd = tools::resolveLink(path);
163cdf0e10cSrcweir 
164cdf0e10cSrcweir     if ( pathEnd == NULL )
165cdf0e10cSrcweir         return 0;
166cdf0e10cSrcweir 
167cdf0e10cSrcweir     if (tools::buildPath(path, path, pathEnd, MY_STRING(L"\\program\\")) == NULL)
168cdf0e10cSrcweir         fail();
169cdf0e10cSrcweir     rebaseImagesInFolder(path, BASEVIRTUALADDRESS);
170cdf0e10cSrcweir 
171cdf0e10cSrcweir     if (tools::buildPath(path, path, pathEnd, MY_STRING(L"\\ure-link")) == NULL)
172cdf0e10cSrcweir         fail();
173cdf0e10cSrcweir     pathEnd = tools::resolveLink(path);
174cdf0e10cSrcweir 
175cdf0e10cSrcweir     if ( pathEnd == NULL )
176cdf0e10cSrcweir         return 0;
177cdf0e10cSrcweir 
178cdf0e10cSrcweir     if (tools::buildPath(path, path, pathEnd, MY_STRING(L"\\bin\\")) == NULL)
179cdf0e10cSrcweir         fail();
180cdf0e10cSrcweir     rebaseImagesInFolder(path, BASEVIRTUALADDRESS);
181cdf0e10cSrcweir 
182cdf0e10cSrcweir     return 0;
183cdf0e10cSrcweir }
184