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 
30*cdf0e10cSrcweir #define  UNICODE    1
31*cdf0e10cSrcweir #define _UNICODE    1
32*cdf0e10cSrcweir 
33*cdf0e10cSrcweir #ifndef _WINDOWS_
34*cdf0e10cSrcweir #	define WIN32_LEAN_AND_MEAN
35*cdf0e10cSrcweir #if defined _MSC_VER
36*cdf0e10cSrcweir #pragma warning(push, 1)
37*cdf0e10cSrcweir #endif
38*cdf0e10cSrcweir #	include <windows.h>
39*cdf0e10cSrcweir #	include <shellapi.h>
40*cdf0e10cSrcweir #	include <wchar.h>
41*cdf0e10cSrcweir #if defined _MSC_VER
42*cdf0e10cSrcweir #pragma warning(pop)
43*cdf0e10cSrcweir #endif
44*cdf0e10cSrcweir #endif
45*cdf0e10cSrcweir 
46*cdf0e10cSrcweir #include "Resource.h"
47*cdf0e10cSrcweir #include <time.h>
48*cdf0e10cSrcweir #include "sal/config.h"
49*cdf0e10cSrcweir #include "tools/pathutils.hxx"
50*cdf0e10cSrcweir 
51*cdf0e10cSrcweir const DWORD PE_Signature = 0x00004550;
52*cdf0e10cSrcweir 
53*cdf0e10cSrcweir #define MY_LENGTH(s)		(sizeof (s) / sizeof *(s) - 1)
54*cdf0e10cSrcweir #define MY_STRING(s)		(s), MY_LENGTH(s)
55*cdf0e10cSrcweir #define MAX_STR_CAPTION		256
56*cdf0e10cSrcweir #define MAX_TEXT_LENGTH     1024
57*cdf0e10cSrcweir 
58*cdf0e10cSrcweir static void failPath(wchar_t* pszAppTitle, wchar_t* pszMsg)
59*cdf0e10cSrcweir {
60*cdf0e10cSrcweir     MessageBoxW(NULL, pszMsg, pszAppTitle, MB_OK | MB_ICONERROR);
61*cdf0e10cSrcweir     TerminateProcess(GetCurrentProcess(), 255);
62*cdf0e10cSrcweir }
63*cdf0e10cSrcweir 
64*cdf0e10cSrcweir static void fail()
65*cdf0e10cSrcweir {
66*cdf0e10cSrcweir     LPWSTR buf = NULL;
67*cdf0e10cSrcweir     FormatMessageW(
68*cdf0e10cSrcweir         FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, NULL,
69*cdf0e10cSrcweir         GetLastError(), 0, reinterpret_cast< LPWSTR >(&buf), 0, NULL);
70*cdf0e10cSrcweir     MessageBoxW(NULL, buf, NULL, MB_OK | MB_ICONERROR);
71*cdf0e10cSrcweir     LocalFree(buf);
72*cdf0e10cSrcweir     TerminateProcess(GetCurrentProcess(), 255);
73*cdf0e10cSrcweir }
74*cdf0e10cSrcweir 
75*cdf0e10cSrcweir static LPVOID getVirtualBaseAddress( wchar_t* pszFilePath )
76*cdf0e10cSrcweir {
77*cdf0e10cSrcweir     HANDLE                hFile;
78*cdf0e10cSrcweir     HANDLE                hFileMapping;
79*cdf0e10cSrcweir     LPVOID				  lpFileBase = 0;
80*cdf0e10cSrcweir     PIMAGE_DOS_HEADER     lpDosHeader;
81*cdf0e10cSrcweir 	PIMAGE_NT_HEADERS     lpNTHeader;
82*cdf0e10cSrcweir 
83*cdf0e10cSrcweir     hFile = CreateFile(pszFilePath,
84*cdf0e10cSrcweir 					   GENERIC_READ, FILE_SHARE_READ, NULL,
85*cdf0e10cSrcweir                        OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,
86*cdf0e10cSrcweir 					   0);
87*cdf0e10cSrcweir 
88*cdf0e10cSrcweir     if ( hFile == INVALID_HANDLE_VALUE )
89*cdf0e10cSrcweir     {
90*cdf0e10cSrcweir         return NULL;
91*cdf0e10cSrcweir 	}
92*cdf0e10cSrcweir 
93*cdf0e10cSrcweir     hFileMapping = CreateFileMapping(hFile, NULL, PAGE_READONLY, 0, 0, NULL);
94*cdf0e10cSrcweir     if ( hFileMapping == 0 )
95*cdf0e10cSrcweir     {
96*cdf0e10cSrcweir 		CloseHandle(hFile);
97*cdf0e10cSrcweir         return NULL;
98*cdf0e10cSrcweir 	}
99*cdf0e10cSrcweir 
100*cdf0e10cSrcweir     lpFileBase = MapViewOfFile(hFileMapping, FILE_MAP_READ, 0, 0, 0);
101*cdf0e10cSrcweir     if ( lpFileBase == 0 )
102*cdf0e10cSrcweir     {
103*cdf0e10cSrcweir         CloseHandle(hFileMapping);
104*cdf0e10cSrcweir         CloseHandle(hFile);
105*cdf0e10cSrcweir         return NULL;
106*cdf0e10cSrcweir     }
107*cdf0e10cSrcweir 
108*cdf0e10cSrcweir     lpDosHeader = (PIMAGE_DOS_HEADER)lpFileBase;
109*cdf0e10cSrcweir     if ( lpDosHeader->e_magic == IMAGE_DOS_SIGNATURE )
110*cdf0e10cSrcweir     {
111*cdf0e10cSrcweir 		lpNTHeader = (PIMAGE_NT_HEADERS)((char*)lpDosHeader + lpDosHeader->e_lfanew);
112*cdf0e10cSrcweir 		if (lpNTHeader->Signature == PE_Signature )
113*cdf0e10cSrcweir 			lpFileBase = reinterpret_cast<LPVOID>( lpNTHeader->OptionalHeader.ImageBase );
114*cdf0e10cSrcweir 	}
115*cdf0e10cSrcweir 
116*cdf0e10cSrcweir 	UnmapViewOfFile(lpFileBase);
117*cdf0e10cSrcweir     CloseHandle(hFileMapping);
118*cdf0e10cSrcweir     CloseHandle(hFile);
119*cdf0e10cSrcweir 
120*cdf0e10cSrcweir 	return lpFileBase;
121*cdf0e10cSrcweir }
122*cdf0e10cSrcweir 
123*cdf0e10cSrcweir static bool checkImageVirtualBaseAddress(wchar_t* pszFilePath, LPVOID lpVBA)
124*cdf0e10cSrcweir {
125*cdf0e10cSrcweir 	LPVOID lpImageVBA = getVirtualBaseAddress(pszFilePath);
126*cdf0e10cSrcweir 	if ( lpImageVBA == lpVBA )
127*cdf0e10cSrcweir 		return true;
128*cdf0e10cSrcweir 	else
129*cdf0e10cSrcweir 		return false;
130*cdf0e10cSrcweir }
131*cdf0e10cSrcweir 
132*cdf0e10cSrcweir static wchar_t* getBrandPath(wchar_t * pszPath)
133*cdf0e10cSrcweir {
134*cdf0e10cSrcweir     DWORD n = GetModuleFileNameW(NULL, pszPath, MAX_PATH);
135*cdf0e10cSrcweir     if (n == 0 || n >= MAX_PATH) {
136*cdf0e10cSrcweir         exit(EXIT_FAILURE);
137*cdf0e10cSrcweir     }
138*cdf0e10cSrcweir     return tools::filename(pszPath);
139*cdf0e10cSrcweir }
140*cdf0e10cSrcweir 
141*cdf0e10cSrcweir extern "C" int APIENTRY WinMain( HINSTANCE hInst, HINSTANCE, LPSTR, int )
142*cdf0e10cSrcweir {
143*cdf0e10cSrcweir     wchar_t* pAppTitle = new wchar_t[ MAX_STR_CAPTION ];
144*cdf0e10cSrcweir 			 pAppTitle[0]  = '\0';
145*cdf0e10cSrcweir 	LoadString( hInst, IDS_APP_TITLE, pAppTitle, MAX_STR_CAPTION );
146*cdf0e10cSrcweir 
147*cdf0e10cSrcweir     wchar_t* pTextServer = new wchar_t[ MAX_TEXT_LENGTH ];
148*cdf0e10cSrcweir 			 pTextServer[0]  = '\0';
149*cdf0e10cSrcweir 	LoadString( hInst, IDS_MSG_OPTIMIZED_FOR_SERVER, pTextServer, MAX_TEXT_LENGTH );
150*cdf0e10cSrcweir 
151*cdf0e10cSrcweir     wchar_t* pTextClient = new wchar_t[ MAX_TEXT_LENGTH ];
152*cdf0e10cSrcweir 			 pTextClient[0]  = '\0';
153*cdf0e10cSrcweir 	LoadString( hInst, IDS_MSG_OPTIMIZED_FOR_CLIENT, pTextClient, MAX_TEXT_LENGTH );
154*cdf0e10cSrcweir 
155*cdf0e10cSrcweir     wchar_t* pTextNoInstallation = new wchar_t[ MAX_TEXT_LENGTH ];
156*cdf0e10cSrcweir 			 pTextNoInstallation[0]  = '\0';
157*cdf0e10cSrcweir 	LoadString( hInst, IDS_MSG_NO_INSTALLATION_FOUND, pTextNoInstallation, MAX_TEXT_LENGTH );
158*cdf0e10cSrcweir 
159*cdf0e10cSrcweir 	LPVOID  VBA = (void*)0x10000000;
160*cdf0e10cSrcweir 	wchar_t path[MAX_PATH];
161*cdf0e10cSrcweir 
162*cdf0e10cSrcweir 	wchar_t * pathEnd = getBrandPath(path);
163*cdf0e10cSrcweir 
164*cdf0e10cSrcweir 	if (tools::buildPath(path, path, pathEnd, MY_STRING(L"libxml2.dll")) == NULL)
165*cdf0e10cSrcweir 		fail();
166*cdf0e10cSrcweir 	bool bFast = checkImageVirtualBaseAddress(path, VBA);
167*cdf0e10cSrcweir 
168*cdf0e10cSrcweir 	if (tools::buildPath(path, path, pathEnd, MY_STRING(L"..\\basis-link")) == NULL)
169*cdf0e10cSrcweir 		fail();
170*cdf0e10cSrcweir 	pathEnd = tools::resolveLink(path);
171*cdf0e10cSrcweir 
172*cdf0e10cSrcweir 	if (pathEnd == NULL)
173*cdf0e10cSrcweir 		failPath(pAppTitle, pTextNoInstallation);
174*cdf0e10cSrcweir 
175*cdf0e10cSrcweir 	if (tools::buildPath(path, path, pathEnd, MY_STRING(L"\\program\\vclmi.dll")) == NULL)
176*cdf0e10cSrcweir 		fail();
177*cdf0e10cSrcweir 	bFast &= checkImageVirtualBaseAddress(path, VBA);
178*cdf0e10cSrcweir 
179*cdf0e10cSrcweir 	if (tools::buildPath(path, path, pathEnd, MY_STRING(L"\\ure-link")) == NULL)
180*cdf0e10cSrcweir 		fail();
181*cdf0e10cSrcweir 	pathEnd = tools::resolveLink(path);
182*cdf0e10cSrcweir 
183*cdf0e10cSrcweir 	if (pathEnd == NULL)
184*cdf0e10cSrcweir 		failPath(pAppTitle, pTextNoInstallation);
185*cdf0e10cSrcweir 
186*cdf0e10cSrcweir 	if (tools::buildPath(path, path, pathEnd, MY_STRING(L"\\bin\\sal3.dll")) == NULL)
187*cdf0e10cSrcweir 		fail();
188*cdf0e10cSrcweir 	bFast &= checkImageVirtualBaseAddress(path, VBA);
189*cdf0e10cSrcweir 
190*cdf0e10cSrcweir 	const wchar_t* pOutput = pTextClient;
191*cdf0e10cSrcweir 	if (!bFast)
192*cdf0e10cSrcweir 		pOutput = pTextServer;
193*cdf0e10cSrcweir 
194*cdf0e10cSrcweir 	MessageBoxW( NULL, pOutput, pAppTitle, MB_OK );
195*cdf0e10cSrcweir 
196*cdf0e10cSrcweir 	return 0;
197*cdf0e10cSrcweir }
198