Kaydet (Commit) 827430c8 authored tarafından Stephan Bergmann's avatar Stephan Bergmann

Fold URE: Windows

...assuming the delayLoadHook in cli_ure/source/native/native_bootstrap.cxx is
no longer necessary and loading of cppuhelper from the program dir cannot fail
regardless in whatever scenario the cli_cppuhelper library itself is loaded.

Change-Id: I13f32b327bca4cce9780864f5e57cdad3860afe5
üst 1924d05e
......@@ -932,10 +932,6 @@ $(eval $(call gb_Helper_register_packages_for_install,ure,\
xml2 \
))
$(eval $(call gb_Helper_register_packages_for_install,ooo,\
xml2_win32 \
))
define gb_LinkTarget__use_libxml2
$(call gb_LinkTarget_use_package,$(1),xml2)
$(call gb_LinkTarget_set_include,$(1),\
......
......@@ -21,8 +21,7 @@ $(eval $(call gb_Jar_set_packageroot,java_uno,com))
$(eval $(call gb_Jar_add_manifest_classpath,java_uno,\
ridl.jar \
jurt.jar \
$(if $(filter MACOSX,$(OS)),../../Frameworks/, \
$(if $(filter WNT,$(OS)),../bin/,../)) \
$(if $(filter MACOSX,$(OS)),../../Frameworks/,../) \
))
$(eval $(call gb_Jar_add_sourcefiles,java_uno,\
......
......@@ -25,7 +25,6 @@
#pragma warning(push, 1)
#endif
#include <windows.h>
#include "uno/environment.hxx"
#ifdef _MSC_VER
#pragma warning(pop)
#endif
......@@ -37,271 +36,11 @@
#include "rtl/bootstrap.hxx"
#include "com/sun/star/uno/XComponentContext.hpp"
#include "cppuhelper/bootstrap.hxx"
#include <delayimp.h>
#include <stdio.h>
using namespace ::com::sun::star;
using namespace ::com::sun::star::uno;
namespace cli_ure {
WCHAR * resolveLink(WCHAR * path);
}
#define INSTALL_PATH L"Software\\LibreOffice\\UNO\\InstallPath"
#define URE_LINK L"\\ure-link"
#define URE_BIN L"\\bin"
#define UNO_PATH L"UNO_PATH"
namespace
{
/*
* Gets the installation path from the Windows Registry for the specified
* registry key.
*
* @param hroot open handle to predefined root registry key
* @param subKeyName name of the subkey to open
*
* @return the installation path or NULL, if no installation was found or
* if an error occurred
*/
WCHAR* getPathFromRegistryKey( HKEY hroot, LPCWSTR subKeyName )
{
HKEY hkey;
DWORD type;
TCHAR* data = NULL;
DWORD size;
/* open the specified registry key */
if ( RegOpenKeyEx( hroot, subKeyName, 0, KEY_READ, &hkey ) != ERROR_SUCCESS )
{
return NULL;
}
/* find the type and size of the default value */
if ( RegQueryValueEx( hkey, NULL, NULL, &type, NULL, &size) != ERROR_SUCCESS )
{
RegCloseKey( hkey );
return NULL;
}
/* get memory to hold the default value */
data = new WCHAR[size];
/* read the default value */
if ( RegQueryValueEx( hkey, NULL, NULL, &type, (LPBYTE) data, &size ) != ERROR_SUCCESS )
{
RegCloseKey( hkey );
delete[] data;
return NULL;
}
/* release registry key handle */
RegCloseKey( hkey );
return data;
}
/* If the path does not end with '\' the las segment will be removed.
path: C:\a\b
-> C:\a
@param io_path
in/out parameter. The string is not reallocated. Simply a '\0'
will be inserted to shorten the string.
*/
void oneDirUp(LPTSTR io_path)
{
WCHAR * pEnd = io_path + lstrlen(io_path) - 1;
while (pEnd > io_path //prevent crashing if provided string does not contain a backslash
&& *pEnd != L'\\')
pEnd --;
*pEnd = L'\0';
}
/* Returns the path to the program folder of the brand layer,
for example c:/LibreOffice 3/program
This path is either obtained from the environment variable UNO_PATH
or the registry item
"Software\\LibreOffice\\UNO\\InstallPath"
either in HKEY_CURRENT_USER or HKEY_LOCAL_MACHINE
The return value must be freed with delete[]
*/
WCHAR * getInstallPath()
{
WCHAR * szInstallPath = NULL;
DWORD cChars = GetEnvironmentVariable(UNO_PATH, NULL, 0);
if (cChars > 0)
{
szInstallPath = new WCHAR[cChars];
cChars = GetEnvironmentVariable(UNO_PATH, szInstallPath, cChars);
//If PATH is not set then it is no error
if (cChars == 0)
{
delete[] szInstallPath;
return NULL;
}
}
if (! szInstallPath)
{
szInstallPath = getPathFromRegistryKey( HKEY_CURRENT_USER, INSTALL_PATH );
if ( szInstallPath == NULL )
{
/* read the key's default value from HKEY_LOCAL_MACHINE */
szInstallPath = getPathFromRegistryKey( HKEY_LOCAL_MACHINE, INSTALL_PATH );
}
}
return szInstallPath;
}
/* Returns the path to the URE/bin path, where cppuhelper lib resides.
The returned string must be freed with delete[]
*/
WCHAR* getUnoPath()
{
WCHAR * szLinkPath = NULL;
WCHAR * szUrePath = NULL;
WCHAR * szUreBin = NULL; //the return value
WCHAR * szInstallPath = getInstallPath();
if (szInstallPath)
{
oneDirUp(szInstallPath);
//build the path to the ure-link file
szUrePath = new WCHAR[MAX_PATH];
szUrePath[0] = L'\0';
lstrcat(szUrePath, szInstallPath);
lstrcat(szUrePath, URE_LINK);
//get the path to the actual Ure folder
if (cli_ure::resolveLink(szUrePath))
{
//build the path to the URE/bin directory
szUreBin = new WCHAR[lstrlen(szUrePath) + lstrlen(URE_BIN) + 1];
szUreBin[0] = L'\0';
lstrcat(szUreBin, szUrePath);
lstrcat(szUreBin, URE_BIN);
}
}
#if OSL_DEBUG_LEVEL >=2
if (szUreBin)
{
fwprintf(stdout,L"[cli_cppuhelper]: Path to URE libraries:\n %s \n", szUreBin);
}
else
{
fwprintf(stdout,L"[cli_cppuhelper]: Failed to determine location of URE.\n");
}
#endif
delete[] szInstallPath;
delete[] szLinkPath;
delete[] szUrePath;
return szUreBin;
}
/*We extend the path to contain the Ure/bin folder,
so that components can use osl_loadModule with arguments, such as
"reg3.dll". That is, the arguments are only the library names.
*/
void extendPath(LPCWSTR szUreBinPath)
{
if (!szUreBinPath)
return;
WCHAR * sEnvPath = NULL;
DWORD cChars = GetEnvironmentVariable(L"PATH", sEnvPath, 0);
if (cChars > 0)
{
sEnvPath = new WCHAR[cChars];
cChars = GetEnvironmentVariable(L"PATH", sEnvPath, cChars);
//If PATH is not set then it is no error
if (cChars == 0 && GetLastError() != ERROR_ENVVAR_NOT_FOUND)
{
delete[] sEnvPath;
return;
}
}
//prepare the new PATH. Add the Ure/bin directory at the front.
//note also adding ';'
WCHAR * sNewPath = new WCHAR[lstrlen(sEnvPath) + lstrlen(szUreBinPath) + 2];
sNewPath[0] = L'\0';
lstrcat(sNewPath, szUreBinPath);
if (lstrlen(sEnvPath))
{
lstrcat(sNewPath, L";");
lstrcat(sNewPath, sEnvPath);
}
SetEnvironmentVariable(L"PATH", sNewPath);
delete[] sEnvPath;
delete[] sNewPath;
}
HMODULE loadFromPath(LPCWSTR sLibName)
{
if (sLibName == NULL)
return NULL;
WCHAR * szUreBinPath = getUnoPath();
if (!szUreBinPath)
return NULL;
extendPath(szUreBinPath);
WCHAR* szFullPath = new WCHAR[lstrlen(sLibName) + lstrlen(szUreBinPath) + 2];
szFullPath[0] = L'\0';
lstrcat(szFullPath, szUreBinPath);
lstrcat(szFullPath, L"\\");
lstrcat(szFullPath, sLibName);
HMODULE handle = LoadLibraryEx(szFullPath, NULL,
LOAD_WITH_ALTERED_SEARCH_PATH);
delete[] szFullPath;
delete[] szUreBinPath;
return handle;
}
/*Hook for delayed loading of libraries which this library is linked with.
This is a failure hook. That is, it is only called when the loading of
a library failed. It will be called when loading of cppuhelper failed.
Because we extend the PATH to the URE/bin folder while this function is
executed (see extendPath), all other libraries are found.
*/
extern "C" FARPROC WINAPI delayLoadHook(
unsigned dliNotify,
PDelayLoadInfo pdli
)
{
if (dliNotify == dliFailLoadLib)
{
LPWSTR szLibName = NULL;
//Convert the ansi file name to wchar_t*
int size = MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, pdli->szDll, -1, NULL, 0);
if (size > 0)
{
szLibName = new WCHAR[size];
if (! MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, pdli->szDll, -1, szLibName, size))
{
delete[] szLibName;
return 0;
}
}
HANDLE h = loadFromPath(szLibName);
delete[] szLibName;
return (FARPROC) h;
}
return 0;
}
}
ExternC
PfnDliHook __pfnDliFailureHook2 = delayLoadHook;
namespace uno
{
namespace util
......
......@@ -21,8 +21,6 @@
#if defined WNT
#include <cstddef>
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
......@@ -43,167 +41,6 @@ SAL_DLLPUBLIC_EXPORT WCHAR * filename(WCHAR * path) {
}
}
WCHAR * buildPath(
WCHAR * path, WCHAR const * frontBegin, WCHAR const * frontEnd,
WCHAR const * backBegin, std::size_t backLength)
{
// Remove leading ".." segments in the second path together with matching
// segments in the first path that are neither empty nor "." nor ".." nor
// end in ":" (which is not foolprove, as it can erroneously erase the start
// of a UNC path, but only if the input is bad data):
while (backLength >= 2 && backBegin[0] == L'.' && backBegin[1] == L'.' &&
(backLength == 2 || backBegin[2] == L'\\'))
{
if (frontEnd - frontBegin < 2 || frontEnd[-1] != L'\\' ||
frontEnd[-2] == L'\\' || frontEnd[-2] == L':' ||
(frontEnd[-2] == L'.' &&
(frontEnd - frontBegin < 3 || frontEnd[-3] == L'\\' ||
(frontEnd[-3] == L'.' &&
(frontEnd - frontBegin < 4 || frontEnd[-4] == L'\\')))))
{
break;
}
WCHAR const * p = frontEnd - 1;
while (p != frontBegin && p[-1] != L'\\') {
--p;
}
if (p == frontBegin) {
break;
}
frontEnd = p;
if (backLength == 2) {
backBegin += 2;
backLength -= 2;
} else {
backBegin += 3;
backLength -= 3;
}
}
if (backLength <
static_cast< std::size_t >(MAX_PATH - (frontEnd - frontBegin)))
// hopefully std::size_t is large enough
{
WCHAR * p;
if (frontBegin == path) {
p = const_cast< WCHAR * >(frontEnd);
} else {
p = path;
while (frontBegin != frontEnd) {
*p++ = *frontBegin++;
}
}
for (; backLength > 0; --backLength) {
*p++ = *backBegin++;
}
*p = L'\0';
return p;
} else {
SetLastError(ERROR_FILENAME_EXCED_RANGE);
return NULL;
}
}
WCHAR * resolveLink(WCHAR * path) {
HANDLE h = CreateFileW(
path, FILE_READ_DATA, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
if (h == INVALID_HANDLE_VALUE) {
return NULL;
}
char p1[MAX_PATH];
DWORD n;
BOOL ok = ReadFile(h, p1, MAX_PATH, &n, NULL);
CloseHandle(h);
if (!ok) {
return NULL;
}
WCHAR p2[MAX_PATH];
std::size_t n2 = 0;
bool colon = false;
for (DWORD i = 0; i < n;) {
unsigned char c = static_cast< unsigned char >(p1[i++]);
switch (c) {
case '\0':
SetLastError(ERROR_BAD_PATHNAME);
return NULL;
case '\x0A':
case '\x0D':
if (n2 == MAX_PATH) {
SetLastError(ERROR_FILENAME_EXCED_RANGE);
return NULL;
}
p2[n2] = L'\0';
break;
case ':':
colon = true;
// fall through
default:
// Convert from UTF-8 to UTF-16:
if (c <= 0x7F) {
p2[n2++] = c;
} else if (c >= 0xC2 && c <= 0xDF && i < n &&
static_cast< unsigned char >(p1[i]) >= 0x80 &&
static_cast< unsigned char >(p1[i]) <= 0xBF)
{
p2[n2++] = ((c & 0x1F) << 6) |
(static_cast< unsigned char >(p1[i++]) & 0x3F);
} else if (n - i > 1 &&
((c == 0xE0 &&
static_cast< unsigned char >(p1[i]) >= 0xA0 &&
static_cast< unsigned char >(p1[i]) <= 0xBF) ||
((c >= 0xE1 && c <= 0xEC || c >= 0xEE && c <= 0xEF) &&
static_cast< unsigned char >(p1[i]) >= 0x80 &&
static_cast< unsigned char >(p1[i]) <= 0xBF) ||
(c == 0xED &&
static_cast< unsigned char >(p1[i]) >= 0x80 &&
static_cast< unsigned char >(p1[i]) <= 0x9F)) &&
static_cast< unsigned char >(p1[i + 1]) >= 0x80 &&
static_cast< unsigned char >(p1[i + 1]) <= 0xBF)
{
p2[n2++] = ((c & 0x0F) << 12) |
((static_cast< unsigned char >(p1[i]) & 0x3F) << 6) |
(static_cast< unsigned char >(p1[i + 1]) & 0x3F);
i += 2;
} else if (n - 2 > 1 &&
((c == 0xF0 &&
static_cast< unsigned char >(p1[i]) >= 0x90 &&
static_cast< unsigned char >(p1[i]) <= 0xBF) ||
(c >= 0xF1 && c <= 0xF3 &&
static_cast< unsigned char >(p1[i]) >= 0x80 &&
static_cast< unsigned char >(p1[i]) <= 0xBF) ||
(c == 0xF4 &&
static_cast< unsigned char >(p1[i]) >= 0x80 &&
static_cast< unsigned char >(p1[i]) <= 0x8F)) &&
static_cast< unsigned char >(p1[i + 1]) >= 0x80 &&
static_cast< unsigned char >(p1[i + 1]) <= 0xBF &&
static_cast< unsigned char >(p1[i + 2]) >= 0x80 &&
static_cast< unsigned char >(p1[i + 2]) <= 0xBF)
{
sal_Int32 u = ((c & 0x07) << 18) |
((static_cast< unsigned char >(p1[i]) & 0x3F) << 12) |
((static_cast< unsigned char >(p1[i + 1]) & 0x3F) << 6) |
(static_cast< unsigned char >(p1[i + 2]) & 0x3F);
i += 3;
p2[n2++] = static_cast< WCHAR >(((u - 0x10000) >> 10) | 0xD800);
p2[n2++] = static_cast< WCHAR >(
((u - 0x10000) & 0x3FF) | 0xDC00);
} else {
SetLastError(ERROR_BAD_PATHNAME);
return NULL;
}
break;
}
}
WCHAR * end;
if (colon || p2[0] == L'\\') {
// Interpret p2 as an absolute path:
end = path;
} else {
// Interpret p2 as a relative path:
end = filename(path);
}
return buildPath(path, path, end, p2, n2);
}
}
#endif
......
......@@ -3039,11 +3039,11 @@ elif test $_os = WINNT; then
LIBO_SHARE_PRESETS_FOLDER=presets
LIBO_SHARE_RESOURCE_FOLDER=program/resource
LIBO_SHARE_SHELL_FOLDER=program/shell
LIBO_URE_BIN_FOLDER=URE/bin
LIBO_URE_ETC_FOLDER=URE/bin
LIBO_URE_LIB_FOLDER=URE/bin
LIBO_URE_MISC_FOLDER=URE/misc
LIBO_URE_SHARE_JAVA_FOLDER=URE/java
LIBO_URE_BIN_FOLDER=program
LIBO_URE_ETC_FOLDER=program
LIBO_URE_LIB_FOLDER=program
LIBO_URE_MISC_FOLDER=program
LIBO_URE_SHARE_JAVA_FOLDER=program/classes
else
LIBO_BIN_FOLDER=program
LIBO_ETC_FOLDER=program
......
......@@ -23,7 +23,7 @@ $(eval $(call gb_Executable_use_system_win32_libs,soffice,\
$(eval $(call gb_Executable_use_static_libraries,soffice,\
ooopathutils \
winextendloaderenv \
winloader \
))
$(eval $(call gb_Executable_add_exception_objects,soffice,\
......
......@@ -36,11 +36,6 @@ endif
ifeq ($(OS),WNT)
$(eval $(call gb_Executable_use_static_libraries,soffice_bin,\
ooopathutils \
winextendloaderenv \
))
$(eval $(call gb_Executable_set_targettype_gui,soffice_bin,YES))
$(eval $(call gb_Executable_add_nativeres,soffice_bin,sofficebin/officeloader))
......
......@@ -13,7 +13,7 @@ $(eval $(call gb_Executable_set_targettype_gui,unopkg,YES))
$(eval $(call gb_Executable_use_static_libraries,unopkg,\
ooopathutils \
winextendloaderenv \
winloader \
))
$(eval $(call gb_Executable_add_exception_objects,unopkg,\
......
......@@ -69,7 +69,7 @@ $(eval $(call gb_Module_add_targets,desktop,\
endif
$(eval $(call gb_Module_add_targets,desktop,\
StaticLibrary_winextendloaderenv \
StaticLibrary_winloader \
StaticLibrary_winlauncher \
Executable_quickstart \
Executable_sbase \
......
......@@ -8,10 +8,10 @@
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
$(eval $(call gb_StaticLibrary_StaticLibrary,winextendloaderenv))
$(eval $(call gb_StaticLibrary_StaticLibrary,winloader))
$(eval $(call gb_StaticLibrary_add_exception_objects,winextendloaderenv,\
desktop/win32/source/extendloaderenvironment \
$(eval $(call gb_StaticLibrary_add_exception_objects,winloader,\
desktop/win32/source/loader \
))
# vim:set noet sw=4 ts=4:
......@@ -38,7 +38,7 @@
#include <systools/win32/uwinapi.h>
#include <tools/pathutils.hxx>
#include "../extendloaderenvironment.hxx"
#include "../loader.hxx"
......@@ -48,7 +48,7 @@ static int GenericMain()
TCHAR szIniDirectory[MAX_PATH];
STARTUPINFO aStartupInfo;
desktop_win32::extendLoaderEnvironment(szTargetFileName, szIniDirectory);
desktop_win32::getPaths(szTargetFileName, szIniDirectory);
ZeroMemory( &aStartupInfo, sizeof(aStartupInfo) );
aStartupInfo.cb = sizeof(aStartupInfo);
......
......@@ -19,8 +19,6 @@
#include <sal/config.h>
#include <cstddef>
#define WIN32_LEAN_AND_MEAN
#if defined _MSC_VER
#pragma warning(push, 1)
......@@ -33,53 +31,19 @@
#include <tools/pathutils.hxx>
#include "extendloaderenvironment.hxx"
namespace {
void fail() {
LPWSTR buf = NULL;
FormatMessageW(
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, NULL,
GetLastError(), 0, reinterpret_cast< LPWSTR >(&buf), 0, NULL);
MessageBoxW(NULL, buf, NULL, MB_OK | MB_ICONERROR);
LocalFree(buf);
TerminateProcess(GetCurrentProcess(), 255);
}
bool contains(WCHAR const * paths, WCHAR const * path, WCHAR const * pathEnd) {
WCHAR const * q = path;
for (WCHAR const * p = paths;; ++p) {
WCHAR c = *p;
switch (c) {
case L'\0':
return q == pathEnd;
case L';':
if (q == pathEnd) {
return true;
}
q = path;
break;
default:
if (q != NULL) {
if (q != pathEnd && *q == c) {
++q;
} else {
q = NULL;
}
}
break;
}
}
}
}
#include "loader.hxx"
namespace desktop_win32 {
void extendLoaderEnvironment(WCHAR * binPath, WCHAR * iniDirectory) {
void getPaths(WCHAR * binPath, WCHAR * iniDirectory) {
if (!GetModuleFileNameW(NULL, iniDirectory, MAX_PATH)) {
fail();
LPWSTR buf = NULL;
FormatMessageW(
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, NULL,
GetLastError(), 0, reinterpret_cast< LPWSTR >(&buf), 0, NULL);
MessageBoxW(NULL, buf, NULL, MB_OK | MB_ICONERROR);
LocalFree(buf);
TerminateProcess(GetCurrentProcess(), 255);
}
WCHAR * iniDirEnd = tools::filename(iniDirectory);
WCHAR name[MAX_PATH + MY_LENGTH(L".bin")];
......@@ -101,40 +65,6 @@ void extendLoaderEnvironment(WCHAR * binPath, WCHAR * iniDirectory) {
nameEnd[-1] = 'n';
tools::buildPath(binPath, iniDirectory, iniDirEnd, name, nameEnd - name);
*iniDirEnd = L'\0';
std::size_t const maxEnv = 32767;
WCHAR pad[MAX_PATH + maxEnv];
// hopefully std::size_t is large enough to not overflow
WCHAR * pathEnd = tools::buildPath(
pad, iniDirectory, iniDirEnd, MY_STRING(L"..\\ure-link"));
if (pathEnd == NULL) {
fail();
}
pathEnd = tools::resolveLink(pad);
if (pathEnd == NULL) {
fail();
}
pathEnd = tools::buildPath(pad, pad, pathEnd, MY_STRING(L"\\bin"));
if (pathEnd == NULL) {
fail();
}
WCHAR env[maxEnv];
DWORD n = GetEnvironmentVariableW(L"PATH", env, maxEnv);
if ((n >= maxEnv || n == 0) && GetLastError() != ERROR_ENVVAR_NOT_FOUND) {
fail();
}
env[n] = L'\0';
if (!contains(env, pad, pathEnd)) {
WCHAR * p = pathEnd;
if (n != 0) {
*p++ = L';';
}
for (DWORD i = 0; i <= n; ++i) {
*p++ = env[i];
}
if (!SetEnvironmentVariableW(L"PATH", pad)) {
fail();
}
}
}
}
......
......@@ -17,8 +17,8 @@
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
#ifndef INCLUDED_DESKTOP_WIN32_SOURCE_EXTENDLOADERENVIRONMENT_HXX
#define INCLUDED_DESKTOP_WIN32_SOURCE_EXTENDLOADERENVIRONMENT_HXX
#ifndef INCLUDED_DESKTOP_WIN32_SOURCE_LOADER_HXX
#define INCLUDED_DESKTOP_WIN32_SOURCE_LOADER_HXX
#include <sal/config.h>
......@@ -68,8 +68,6 @@ inline WCHAR * commandLineAppendEncoded(WCHAR * buffer, WCHAR const * text) {
return buffer;
}
// Set the PATH environment variable in the current (loader) process, so that a
// following CreateProcess has the necessary environment:
// @param binPath
// Must point to an array of size at least MAX_PATH. Is filled with the null
// terminated full path to the "bin" file corresponding to the current
......@@ -78,7 +76,7 @@ inline WCHAR * commandLineAppendEncoded(WCHAR * buffer, WCHAR const * text) {
// Must point to an array of size at least MAX_PATH. Is filled with the null
// terminated full directory path (ending in "\") to the "ini" file
// corresponding to the current executable.
void extendLoaderEnvironment(WCHAR * binPath,