1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 #include "precompiled_desktop.hxx"
29 
30 #define  UNICODE    1
31 #define _UNICODE    1
32 
33 #ifndef _WINDOWS_
34 #	define WIN32_LEAN_AND_MEAN
35 #if defined _MSC_VER
36 #pragma warning(push, 1)
37 #endif
38 #	include <windows.h>
39 #	include <shellapi.h>
40 #	include <wchar.h>
41 #if defined _MSC_VER
42 #pragma warning(pop)
43 #endif
44 #endif
45 
46 #include "Resource.h"
47 #include <time.h>
48 #include "sal/config.h"
49 #include "tools/pathutils.hxx"
50 
51 const DWORD PE_Signature = 0x00004550;
52 
53 #define MY_LENGTH(s)		(sizeof (s) / sizeof *(s) - 1)
54 #define MY_STRING(s)		(s), MY_LENGTH(s)
55 #define MAX_STR_CAPTION		256
56 #define MAX_TEXT_LENGTH     1024
57 
58 static void failPath(wchar_t* pszAppTitle, wchar_t* pszMsg)
59 {
60     MessageBoxW(NULL, pszMsg, pszAppTitle, MB_OK | MB_ICONERROR);
61     TerminateProcess(GetCurrentProcess(), 255);
62 }
63 
64 static void fail()
65 {
66     LPWSTR buf = NULL;
67     FormatMessageW(
68         FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, NULL,
69         GetLastError(), 0, reinterpret_cast< LPWSTR >(&buf), 0, NULL);
70     MessageBoxW(NULL, buf, NULL, MB_OK | MB_ICONERROR);
71     LocalFree(buf);
72     TerminateProcess(GetCurrentProcess(), 255);
73 }
74 
75 static LPVOID getVirtualBaseAddress( wchar_t* pszFilePath )
76 {
77     HANDLE                hFile;
78     HANDLE                hFileMapping;
79     LPVOID				  lpFileBase = 0;
80     PIMAGE_DOS_HEADER     lpDosHeader;
81 	PIMAGE_NT_HEADERS     lpNTHeader;
82 
83     hFile = CreateFile(pszFilePath,
84 					   GENERIC_READ, FILE_SHARE_READ, NULL,
85                        OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,
86 					   0);
87 
88     if ( hFile == INVALID_HANDLE_VALUE )
89     {
90         return NULL;
91 	}
92 
93     hFileMapping = CreateFileMapping(hFile, NULL, PAGE_READONLY, 0, 0, NULL);
94     if ( hFileMapping == 0 )
95     {
96 		CloseHandle(hFile);
97         return NULL;
98 	}
99 
100     lpFileBase = MapViewOfFile(hFileMapping, FILE_MAP_READ, 0, 0, 0);
101     if ( lpFileBase == 0 )
102     {
103         CloseHandle(hFileMapping);
104         CloseHandle(hFile);
105         return NULL;
106     }
107 
108     lpDosHeader = (PIMAGE_DOS_HEADER)lpFileBase;
109     if ( lpDosHeader->e_magic == IMAGE_DOS_SIGNATURE )
110     {
111 		lpNTHeader = (PIMAGE_NT_HEADERS)((char*)lpDosHeader + lpDosHeader->e_lfanew);
112 		if (lpNTHeader->Signature == PE_Signature )
113 			lpFileBase = reinterpret_cast<LPVOID>( lpNTHeader->OptionalHeader.ImageBase );
114 	}
115 
116 	UnmapViewOfFile(lpFileBase);
117     CloseHandle(hFileMapping);
118     CloseHandle(hFile);
119 
120 	return lpFileBase;
121 }
122 
123 static bool checkImageVirtualBaseAddress(wchar_t* pszFilePath, LPVOID lpVBA)
124 {
125 	LPVOID lpImageVBA = getVirtualBaseAddress(pszFilePath);
126 	if ( lpImageVBA == lpVBA )
127 		return true;
128 	else
129 		return false;
130 }
131 
132 static wchar_t* getBrandPath(wchar_t * pszPath)
133 {
134     DWORD n = GetModuleFileNameW(NULL, pszPath, MAX_PATH);
135     if (n == 0 || n >= MAX_PATH) {
136         exit(EXIT_FAILURE);
137     }
138     return tools::filename(pszPath);
139 }
140 
141 extern "C" int APIENTRY WinMain( HINSTANCE hInst, HINSTANCE, LPSTR, int )
142 {
143     wchar_t* pAppTitle = new wchar_t[ MAX_STR_CAPTION ];
144 			 pAppTitle[0]  = '\0';
145 	LoadString( hInst, IDS_APP_TITLE, pAppTitle, MAX_STR_CAPTION );
146 
147     wchar_t* pTextServer = new wchar_t[ MAX_TEXT_LENGTH ];
148 			 pTextServer[0]  = '\0';
149 	LoadString( hInst, IDS_MSG_OPTIMIZED_FOR_SERVER, pTextServer, MAX_TEXT_LENGTH );
150 
151     wchar_t* pTextClient = new wchar_t[ MAX_TEXT_LENGTH ];
152 			 pTextClient[0]  = '\0';
153 	LoadString( hInst, IDS_MSG_OPTIMIZED_FOR_CLIENT, pTextClient, MAX_TEXT_LENGTH );
154 
155     wchar_t* pTextNoInstallation = new wchar_t[ MAX_TEXT_LENGTH ];
156 			 pTextNoInstallation[0]  = '\0';
157 	LoadString( hInst, IDS_MSG_NO_INSTALLATION_FOUND, pTextNoInstallation, MAX_TEXT_LENGTH );
158 
159 	LPVOID  VBA = (void*)0x10000000;
160 	wchar_t path[MAX_PATH];
161 
162 	wchar_t * pathEnd = getBrandPath(path);
163 
164 	if (tools::buildPath(path, path, pathEnd, MY_STRING(L"libxml2.dll")) == NULL)
165 		fail();
166 	bool bFast = checkImageVirtualBaseAddress(path, VBA);
167 
168 	if (tools::buildPath(path, path, pathEnd, MY_STRING(L"..\\basis-link")) == NULL)
169 		fail();
170 	pathEnd = tools::resolveLink(path);
171 
172 	if (pathEnd == NULL)
173 		failPath(pAppTitle, pTextNoInstallation);
174 
175 	if (tools::buildPath(path, path, pathEnd, MY_STRING(L"\\program\\vclmi.dll")) == NULL)
176 		fail();
177 	bFast &= checkImageVirtualBaseAddress(path, VBA);
178 
179 	if (tools::buildPath(path, path, pathEnd, MY_STRING(L"\\ure-link")) == NULL)
180 		fail();
181 	pathEnd = tools::resolveLink(path);
182 
183 	if (pathEnd == NULL)
184 		failPath(pAppTitle, pTextNoInstallation);
185 
186 	if (tools::buildPath(path, path, pathEnd, MY_STRING(L"\\bin\\sal3.dll")) == NULL)
187 		fail();
188 	bFast &= checkImageVirtualBaseAddress(path, VBA);
189 
190 	const wchar_t* pOutput = pTextClient;
191 	if (!bFast)
192 		pOutput = pTextServer;
193 
194 	MessageBoxW( NULL, pOutput, pAppTitle, MB_OK );
195 
196 	return 0;
197 }
198