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
diff --git a/RepositoryExternal.mk b/RepositoryExternal.mk
index 3ae0eb6..c8f093e 100644
--- a/RepositoryExternal.mk
+++ b/RepositoryExternal.mk
@@ -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),\
diff --git a/bridges/Jar_java_uno.mk b/bridges/Jar_java_uno.mk
index da80f84..ecaad3c4 100644
--- a/bridges/Jar_java_uno.mk
+++ b/bridges/Jar_java_uno.mk
@@ -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,\
diff --git a/cli_ure/source/native/native_bootstrap.cxx b/cli_ure/source/native/native_bootstrap.cxx
index 1e2140278..2ff980f 100644
--- a/cli_ure/source/native/native_bootstrap.cxx
+++ b/cli_ure/source/native/native_bootstrap.cxx
@@ -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
diff --git a/cli_ure/source/native/path.cxx b/cli_ure/source/native/path.cxx
index 97c7f36..006c70f 100644
--- a/cli_ure/source/native/path.cxx
+++ b/cli_ure/source/native/path.cxx
@@ -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
diff --git a/configure.ac b/configure.ac
index 7740f0a..e2b7e4b 100644
--- a/configure.ac
+++ b/configure.ac
@@ -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
diff --git a/desktop/Executable_soffice.mk b/desktop/Executable_soffice.mk
index 15b93f6..0a6a0cc 100644
--- a/desktop/Executable_soffice.mk
+++ b/desktop/Executable_soffice.mk
@@ -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,\
diff --git a/desktop/Executable_soffice_bin.mk b/desktop/Executable_soffice_bin.mk
index cc66af5b..f18e9dc 100644
--- a/desktop/Executable_soffice_bin.mk
+++ b/desktop/Executable_soffice_bin.mk
@@ -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))
diff --git a/desktop/Executable_unopkg.mk b/desktop/Executable_unopkg.mk
index bc7b17b..674edfd 100644
--- a/desktop/Executable_unopkg.mk
+++ b/desktop/Executable_unopkg.mk
@@ -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,\
diff --git a/desktop/Module_desktop.mk b/desktop/Module_desktop.mk
index 76c55b3..4d6f5e8 100644
--- a/desktop/Module_desktop.mk
+++ b/desktop/Module_desktop.mk
@@ -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 \
diff --git a/desktop/StaticLibrary_winextendloaderenv.mk b/desktop/StaticLibrary_winloader.mk
similarity index 64%
rename from desktop/StaticLibrary_winextendloaderenv.mk
rename to desktop/StaticLibrary_winloader.mk
index fc1c071..ca4fe69 100644
--- a/desktop/StaticLibrary_winextendloaderenv.mk
+++ b/desktop/StaticLibrary_winloader.mk
@@ -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:
diff --git a/desktop/win32/source/extendloaderenvironment.cxx b/desktop/win32/source/extendloaderenvironment.cxx
deleted file mode 100644
index 5046763..0000000
--- a/desktop/win32/source/extendloaderenvironment.cxx
+++ /dev/null
@@ -1,142 +0,0 @@
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */

#include <sal/config.h>

#include <cstddef>

#define WIN32_LEAN_AND_MEAN
#if defined _MSC_VER
#pragma warning(push, 1)
#endif
#include <windows.h>
#include <shlwapi.h>
#if defined _MSC_VER
#pragma warning(pop)
#endif

#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;
        }
    }
}

}

namespace desktop_win32 {

void extendLoaderEnvironment(WCHAR * binPath, WCHAR * iniDirectory) {
    if (!GetModuleFileNameW(NULL, iniDirectory, MAX_PATH)) {
        fail();
    }
    WCHAR * iniDirEnd = tools::filename(iniDirectory);
    WCHAR name[MAX_PATH + MY_LENGTH(L".bin")];
        // hopefully std::size_t is large enough to not overflow
    WCHAR * nameEnd = name;
    for (WCHAR * p = iniDirEnd; *p != L'\0'; ++p) {
        *nameEnd++ = *p;
    }
    if (!(nameEnd - name >= 4 && nameEnd[-4] == L'.' &&
          (nameEnd[-3] == L'E' || nameEnd[-3] == L'e') &&
          (nameEnd[-2] == L'X' || nameEnd[-2] == L'x') &&
          (nameEnd[-1] == L'E' || nameEnd[-1] == L'e')))
    {
        *nameEnd = L'.';
        nameEnd += 4;
    }
    nameEnd[-3] = 'b';
    nameEnd[-2] = 'i';
    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();
        }
    }
}

}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/desktop/win32/source/guiloader/genericloader.cxx b/desktop/win32/source/guiloader/genericloader.cxx
index 97cc4e9..4c3d948 100644
--- a/desktop/win32/source/guiloader/genericloader.cxx
+++ b/desktop/win32/source/guiloader/genericloader.cxx
@@ -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);
diff --git a/desktop/win32/source/loader.cxx b/desktop/win32/source/loader.cxx
new file mode 100644
index 0000000..95327fc
--- /dev/null
+++ b/desktop/win32/source/loader.cxx
@@ -0,0 +1,72 @@
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */

#include <sal/config.h>

#define WIN32_LEAN_AND_MEAN
#if defined _MSC_VER
#pragma warning(push, 1)
#endif
#include <windows.h>
#include <shlwapi.h>
#if defined _MSC_VER
#pragma warning(pop)
#endif

#include <tools/pathutils.hxx>

#include "loader.hxx"

namespace desktop_win32 {

void getPaths(WCHAR * binPath, WCHAR * iniDirectory) {
    if (!GetModuleFileNameW(NULL, iniDirectory, MAX_PATH)) {
        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")];
        // hopefully std::size_t is large enough to not overflow
    WCHAR * nameEnd = name;
    for (WCHAR * p = iniDirEnd; *p != L'\0'; ++p) {
        *nameEnd++ = *p;
    }
    if (!(nameEnd - name >= 4 && nameEnd[-4] == L'.' &&
          (nameEnd[-3] == L'E' || nameEnd[-3] == L'e') &&
          (nameEnd[-2] == L'X' || nameEnd[-2] == L'x') &&
          (nameEnd[-1] == L'E' || nameEnd[-1] == L'e')))
    {
        *nameEnd = L'.';
        nameEnd += 4;
    }
    nameEnd[-3] = 'b';
    nameEnd[-2] = 'i';
    nameEnd[-1] = 'n';
    tools::buildPath(binPath, iniDirectory, iniDirEnd, name, nameEnd - name);
    *iniDirEnd = L'\0';
}

}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/desktop/win32/source/extendloaderenvironment.hxx b/desktop/win32/source/loader.hxx
similarity index 88%
rename from desktop/win32/source/extendloaderenvironment.hxx
rename to desktop/win32/source/loader.hxx
index 386d7ef..e8a9876 100644
--- a/desktop/win32/source/extendloaderenvironment.hxx
+++ b/desktop/win32/source/loader.hxx
@@ -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, WCHAR * iniDirectory);
void getPaths(WCHAR * binPath, WCHAR * iniDirectory);

}

diff --git a/desktop/win32/source/officeloader/officeloader.cxx b/desktop/win32/source/officeloader/officeloader.cxx
index 7f820c2..7eb1427 100644
--- a/desktop/win32/source/officeloader/officeloader.cxx
+++ b/desktop/win32/source/officeloader/officeloader.cxx
@@ -44,7 +44,7 @@
#include <sal/macros.h>

#include "../../../source/inc/exithelper.h"
#include "../extendloaderenvironment.hxx"
#include "../loader.hxx"

#include <config_version.h>

@@ -177,7 +177,7 @@ int WINAPI _tWinMain( HINSTANCE, HINSTANCE, LPTSTR, int )
    TCHAR               szPerfTuneIniFile[MAX_PATH] = TEXT("");
    STARTUPINFO         aStartupInfo;

    desktop_win32::extendLoaderEnvironment(szTargetFileName, szIniDirectory);
    desktop_win32::getPaths(szTargetFileName, szIniDirectory);

    ZeroMemory( &aStartupInfo, sizeof(aStartupInfo) );
    aStartupInfo.cb = sizeof(aStartupInfo);
diff --git a/desktop/win32/source/unoinfo.cxx b/desktop/win32/source/unoinfo.cxx
index 92143f6..5af29df 100644
--- a/desktop/win32/source/unoinfo.cxx
+++ b/desktop/win32/source/unoinfo.cxx
@@ -79,16 +79,7 @@ int wmain(int argc, wchar_t ** argv, wchar_t **) {
#endif
        wchar_t path[MAX_PATH];
        wchar_t * pathEnd = getBrandPath(path);
        if (tools::buildPath(path, path, pathEnd, MY_STRING(L"..\\ure-link")) ==
            NULL)
        {
            exit(EXIT_FAILURE);
        }
        pathEnd = tools::resolveLink(path);
        if (pathEnd == NULL) {
            exit(EXIT_FAILURE);
        }
        writePath(path, pathEnd, MY_STRING(L"\\bin"));
        writePath(path, pathEnd, MY_STRING(L""));
#ifdef __MINGW32__
    } else if (argc == 2 && strcmp(argv[1], "java") == 0) {
#else
@@ -99,24 +90,15 @@ int wmain(int argc, wchar_t ** argv, wchar_t **) {
        }
        wchar_t path[MAX_PATH];
        wchar_t * pathEnd = getBrandPath(path);
        writePath(path, pathEnd, MY_STRING(L""));
        writePath(path, pathEnd, MY_STRING(L"classes\\ridl.jar"));
        writeNull();
        writePath(path, pathEnd, MY_STRING(L"classes\\jurt.jar"));
        writeNull();
        writePath(path, pathEnd, MY_STRING(L"classes\\juh.jar"));
        writeNull();
        writePath(path, pathEnd, MY_STRING(L"classes\\unoil.jar"));
        if (tools::buildPath(path, path, pathEnd, MY_STRING(L"..\\ure-link")) ==
            NULL)
        {
            exit(EXIT_FAILURE);
        }
        pathEnd = tools::resolveLink(path);
        if (pathEnd == NULL) {
            exit(EXIT_FAILURE);
        }
        writeNull();
        writePath(path, pathEnd, MY_STRING(L"\\java\\ridl.jar"));
        writeNull();
        writePath(path, pathEnd, MY_STRING(L"\\java\\jurt.jar"));
        writeNull();
        writePath(path, pathEnd, MY_STRING(L"\\java\\juh.jar"));
        writePath(path, pathEnd, MY_STRING(L""));
    } else {
        exit(EXIT_FAILURE);
    }
diff --git a/external/libxml2/ExternalPackage_xml2_win32.mk b/external/libxml2/ExternalPackage_xml2_win32.mk
deleted file mode 100644
index c97fd89..0000000
--- a/external/libxml2/ExternalPackage_xml2_win32.mk
+++ /dev/null
@@ -1,26 +0,0 @@
# -*- Mode: makefile-gmake; tab-width: 4; indent-tabs-mode: t -*-
#
# This file is part of the LibreOffice project.
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#

$(eval $(call gb_ExternalPackage_ExternalPackage,xml2_win32,xml2))

$(eval $(call gb_ExternalPackage_use_external_project,xml2_win32,xml2))

# duplicate copy next to executables due to stupid Win32 DLL search order
# where a libxml2.dll in C:\Windows is loaded before one in URE lib dir
# see 5a5dab5167d136c315e81448ea8eca59e60067da

ifeq ($(OS),WNT)
ifeq ($(COM),GCC)
$(eval $(call gb_ExternalPackage_add_file,xml2_win32,$(LIBO_LIB_FOLDER)/libxml2.dll,.libs/libxml2.dll))
else # COM=MSC
$(eval $(call gb_ExternalPackage_add_file,xml2_win32,$(LIBO_LIB_FOLDER)/libxml2.dll,win32/bin.msvc/libxml2.dll))
endif
endif # OS=WNT

# vim: set noet sw=4 ts=4:
diff --git a/external/libxml2/Module_libxml2.mk b/external/libxml2/Module_libxml2.mk
index 9efb17c..f6bca3f 100644
--- a/external/libxml2/Module_libxml2.mk
+++ b/external/libxml2/Module_libxml2.mk
@@ -11,7 +11,6 @@ $(eval $(call gb_Module_Module,libxml2))

$(eval $(call gb_Module_add_targets,libxml2,\
	ExternalPackage_xml2 \
	ExternalPackage_xml2_win32 \
	ExternalProject_xml2 \
	UnpackedTarball_xml2 \
))
diff --git a/include/tools/pathutils.hxx b/include/tools/pathutils.hxx
index 7649114b..6e61a29 100644
--- a/include/tools/pathutils.hxx
+++ b/include/tools/pathutils.hxx
@@ -66,19 +66,6 @@ WCHAR * buildPath(
    WCHAR * path, WCHAR const * frontBegin, WCHAR const * frontEnd,
    WCHAR const * backBegin, std::size_t backLength);

/** Resolve a link file.

    @param path
        An input/output parameter taking the path; must point at a valid range of
        memory of size at least MAX_PATH.  On input, contains the null-terminated
        full path of the link file.  On output, contains the null-terminated full
        path of the resolved link; if NULL is returned, the content is unspecified.
    @return
        A pointer to the terminating null character of path, or NULL if a failure
        occurred.
*/
WCHAR * resolveLink(WCHAR * path);

}

#endif
diff --git a/instsetoo_native/CustomTarget_setup.mk b/instsetoo_native/CustomTarget_setup.mk
index 616f351..0d646ae 100644
--- a/instsetoo_native/CustomTarget_setup.mk
+++ b/instsetoo_native/CustomTarget_setup.mk
@@ -58,7 +58,7 @@ $(call gb_CustomTarget_get_workdir,instsetoo_native/setup)/$(call gb_Helper_get_
		&& echo 'UNO_SHARED_PACKAGES_CACHE=$${$$ORIGIN/$(call gb_Helper_get_rcfile,louno):UNO_SHARED_PACKAGES_CACHE}' \
		&& echo 'TMP_EXTENSIONS=$${$$ORIGIN/$(call gb_Helper_get_rcfile,louno):TMP_EXTENSIONS}' \
		&& echo 'UNO_USER_PACKAGES_CACHE=$${$$ORIGIN/$(call gb_Helper_get_rcfile,louno):UNO_USER_PACKAGES_CACHE}' \
		&& echo 'URE_BIN_DIR=$(if $(filter WNT,$(OS)),$${.link:$${BRAND_BASE_DIR}/ure-link}/bin,$${BRAND_BASE_DIR}/$(LIBO_URE_BIN_FOLDER))' \
		&& echo 'URE_BIN_DIR=$${BRAND_BASE_DIR}/$(LIBO_URE_BIN_FOLDER)' \
		&& echo 'URE_MORE_JAVA_CLASSPATH_URLS=$(if $(SYSTEM_HSQLDB),$(HSQLDB_JAR))' \
		&& echo 'URE_OVERRIDE_JAVA_JFW_SHARED_DATA=$${BRAND_BASE_DIR}/$(LIBO_SHARE_FOLDER)/config/javasettings_$${_OS}_$${_ARCH}.xml' \
		&& echo 'URE_OVERRIDE_JAVA_JFW_USER_DATA=$${$${BRAND_BASE_DIR}/$(LIBO_ETC_FOLDER)/$(call gb_Helper_get_rcfile,bootstrap):UserInstallation}/user/config/javasettings_$${_OS}_$${_ARCH}.xml' \
@@ -120,10 +120,10 @@ $(call gb_CustomTarget_get_workdir,instsetoo_native/setup)/$(call gb_Helper_get_
	( \
		echo '[Bootstrap]' \
		&& echo 'URE_INTERNAL_LIB_DIR=$${ORIGIN}$(if $(filter MACOSX,$(OS)),/../../../Frameworks)' \
		&& echo 'URE_INTERNAL_JAVA_DIR=$(if $(filter MACOSX,$(OS)),$${ORIGIN}/../../java,$(if $(filter WNT,$(OS)),$${ORIGIN}/../java,$${ORIGIN}/classes))' \
		&& echo 'URE_INTERNAL_JAVA_DIR=$(if $(filter MACOSX,$(OS)),$${ORIGIN}/../../java,$${ORIGIN}/classes)' \
		&& echo 'URE_INTERNAL_JAVA_CLASSPATH=$${URE_MORE_JAVA_TYPES}' \
		&& echo 'UNO_TYPES=$(if $(filter MACOSX,$(OS)),$${ORIGIN}/../share/misc/,$(if $(filter WNT,$(OS)),$${ORIGIN}/../misc/,$${ORIGIN}/))types.rdb $${URE_MORE_TYPES}' \
		&& echo 'UNO_SERVICES=$(if $(filter MACOSX,$(OS)),$${ORIGIN}/../share/misc/,$(if $(filter WNT,$(OS)),$${ORIGIN}/../misc/,$${ORIGIN}/))services.rdb $${URE_MORE_SERVICES}' \
		&& echo 'UNO_TYPES=$(if $(filter MACOSX,$(OS)),$${ORIGIN}/../share/misc/,$${ORIGIN}/)types.rdb $${URE_MORE_TYPES}' \
		&& echo 'UNO_SERVICES=$(if $(filter MACOSX,$(OS)),$${ORIGIN}/../share/misc/,$${ORIGIN}/)services.rdb $${URE_MORE_SERVICES}' \
	) > $@

.PHONY: $(call gb_CustomTarget_get_workdir,instsetoo_native/setup)/$(call gb_Helper_get_rcfile,version)
diff --git a/javaunohelper/Jar_juh.mk b/javaunohelper/Jar_juh.mk
index cfd0ec8..633a2d8 100644
--- a/javaunohelper/Jar_juh.mk
+++ b/javaunohelper/Jar_juh.mk
@@ -19,8 +19,7 @@ $(eval $(call gb_Jar_set_packageroot,juh,com))
$(eval $(call gb_Jar_add_manifest_classpath,juh, \
    jurt.jar \
    ridl.jar \
    $(if $(filter MACOSX,$(OS)),../../Frameworks/, \
        $(if $(filter WNT,$(OS)),../bin/,../)) \
    $(if $(filter MACOSX,$(OS)),../../Frameworks/,../) \
))

$(eval $(call gb_Jar_set_manifest,juh,$(SRCDIR)/javaunohelper/util/manifest))
diff --git a/jurt/Jar_jurt.mk b/jurt/Jar_jurt.mk
index 710e818..70dae3bc 100644
--- a/jurt/Jar_jurt.mk
+++ b/jurt/Jar_jurt.mk
@@ -21,8 +21,7 @@ $(eval $(call gb_Jar_set_manifest,jurt,$(SRCDIR)/jurt/util/manifest))
$(eval $(call gb_Jar_add_manifest_classpath,jurt, \
    ridl.jar \
    unoloader.jar \
	$(if $(filter MACOSX,$(OS)),../../Frameworks/, \
		$(if $(filter WNT,$(OS)),../bin/,../)) \
	$(if $(filter MACOSX,$(OS)),../../Frameworks/,../) \
))

$(eval $(call gb_Jar_add_sourcefiles,jurt,\
diff --git a/jvmfwk/CustomTarget_jvmfwk_jvmfwk3_ini.mk b/jvmfwk/CustomTarget_jvmfwk_jvmfwk3_ini.mk
index bda983d..7dd4f19 100644
--- a/jvmfwk/CustomTarget_jvmfwk_jvmfwk3_ini.mk
+++ b/jvmfwk/CustomTarget_jvmfwk_jvmfwk3_ini.mk
@@ -27,7 +27,7 @@ $(call gb_CustomTarget_get_workdir,jvmfwk/jvmfwk3_ini)/$(call gb_Helper_get_rcfi
            $(SRCDIR)/jvmfwk/CustomTarget_jvmfwk_jvmfwk3_ini.mk
	$(call gb_Output_announce,$(subst $(WORKDIR)/,,$@),$(true),ECH,1)
	(   printf '[Bootstrap]\n' && \
            printf 'UNO_JAVA_JFW_VENDOR_SETTINGS=$(if $(filter MACOSX,$(OS)),$${ORIGIN}/../share/misc/,$(if $(filter WNT,$(OS)),$${ORIGIN}/../misc/,$${ORIGIN}/))javavendors.xml\n' && \
            printf 'UNO_JAVA_JFW_VENDOR_SETTINGS=$(if $(filter MACOSX,$(OS)),$${ORIGIN}/../share/misc/,$${ORIGIN}/)javavendors.xml\n' && \
            printf 'UNO_JAVA_JFW_SHARED_DATA=$${URE_OVERRIDE_JAVA_JFW_SHARED_DATA}\n' && \
            printf 'UNO_JAVA_JFW_USER_DATA=$${URE_OVERRIDE_JAVA_JFW_USER_DATA}\n' && \
            printf 'UNO_JAVA_JFW_CLASSPATH_URLS=$${URE_MORE_JAVA_CLASSPATH_URLS}\n' \
diff --git a/odk/config/cfgWin.js b/odk/config/cfgWin.js
index 5157eba..9c71c43 100644
--- a/odk/config/cfgWin.js
+++ b/odk/config/cfgWin.js
@@ -53,7 +53,6 @@ var oo_user_sdk_dir=WshSysEnv("APPDATA") + "\\" + oo_sdk_name;
var oo_user_sdk_env_script=oo_user_sdk_dir + "\\setsdkenv_windows.bat";

var office_home=getOfficeHome();
var oo_sdk_ure_home=office_home + "\\URE";

var oo_sdk_make_home=getMakeHome();
var oo_sdk_zip_home=getZipHome();
@@ -854,10 +853,6 @@ function writeBatFile(fdir, file)
        "REM Example: set OFFICE_HOME=C:\\Program Files\\LibreOffice 3\n" +
        "set OFFICE_HOME=" + office_home +
        "\n\n" +
        "REM URE installation directory.\n" +
        "REM Example: set OO_SDK_URE_HOME=C:\\Program Files\\LibreOffice 3\\URE\n" +
        "set OO_SDK_URE_HOME=" + oo_sdk_ure_home +
        "\n\n" +
        "REM Directory of the make command.\n" +
        "REM Example: set OO_SDK_MAKE_HOME=D:\\NextGenerationMake\\make\n" +
        "set OO_SDK_MAKE_HOME=" + oo_sdk_make_home +
@@ -904,12 +899,9 @@ function writeBatFile(fdir, file)
        "\n" +
        "REM Check installation path for the office.\n" +
        "REM if not defined OFFICE_HOME (\n" +
        "REM if not defined OO_SDK_URE_HOME (\n" +
        "REM    echo Error: either of the variables OFFICE_HOME and\n" +
        "REM    echo OO_SDK_URE_HOME is missing!\n" +
        "REM    echo Error: the variable OFFICE_HOME is missing!\n" +
        "REM    goto :error\n" +
        "REM  )\n" +
        "REM  )\n" +
        "\n" +
        "REM Check installation path for GNU make.\n" +
        "if not defined OO_SDK_MAKE_HOME (\n" +
@@ -953,15 +945,12 @@ function writeBatFile(fdir, file)
		"   set UNO_PATH=%OFFICE_PROGRAM_PATH%\n" +
		" )\n" +
        "\n" +
        "REM if defined OO_SDK_URE_HOME (\n" +
        "set OO_SDK_URE_BIN_DIR=%OO_SDK_URE_HOME%\\bin\n" +
        "set OO_SDK_URE_LIB_DIR=%OO_SDK_URE_HOME%\\bin\n" +
        "set OO_SDK_URE_JAVA_DIR=%OO_SDK_URE_HOME%\\java\n" +
        "REM ) else (\n" +
        "set OO_SDK_URE_BIN_DIR=%OFFICE_PROGRAM_PATH%\n" +
        "set OO_SDK_URE_LIB_DIR=%OFFICE_PROGRAM_PATH%\n" +
        "set OO_SDK_URE_JAVA_DIR=%OFFICE_PROGRAM_PATH%\\classes\n" +
        "set OO_SDK_OFFICE_BIN_DIR=%OFFICE_PROGRAM_PATH%\n" +
        "set OO_SDK_OFFICE_LIB_DIR=%OFFICE_PROGRAM_PATH%\n" +
        "set OO_SDK_OFFICE_JAVA_DIR=%OFFICE_PROGRAM_PATH%\\classes\n" +
        "REM )\n" +
        "\n" +
        "REM Set classpath\n" +
        "set CLASSPATH=%OO_SDK_URE_JAVA_DIR%\\juh.jar;%OO_SDK_URE_JAVA_DIR%\\jurt.jar;%OO_SDK_URE_JAVA_DIR%\\ridl.jar;%OO_SDK_URE_JAVA_DIR%\\unoloader.jar;%OO_SDK_OFFICE_JAVA_DIR%\\unoil.jar\n" +
@@ -1013,7 +1002,6 @@ function writeBatFile(fdir, file)
        "echo  *\n" +
        "echo  * SDK = %OO_SDK_HOME%\n" +
        "echo  * Office = %OFFICE_HOME%\n" +
        "echo  * URE = %OO_SDK_URE_HOME%\n" +
        "echo  * Make = %OO_SDK_MAKE_HOME%\n" +
        "echo  * Zip = %OO_SDK_ZIP_HOME%\n" +
        "echo  * cat = %OO_SDK_CAT_HOME%\n" +
diff --git a/odk/settings/settings.mk b/odk/settings/settings.mk
index 2169af4..554ac74 100644
--- a/odk/settings/settings.mk
+++ b/odk/settings/settings.mk
@@ -129,9 +129,7 @@ endif

LINK_JAVA_LIBS=/LIBPATH:"$(OO_SDK_JAVA_HOME)/lib"

ifneq "$(OO_SDK_URE_HOME)" ""
URE_MISC=$(OO_SDK_URE_HOME)\misc
endif
URE_MISC=${OFFICE_PROGRAM_PATH}

# use this for release version
#EXE_LINK_FLAGS=/MAP /OPT:NOREF /SUBSYSTEM:CONSOLE /BASE:0x1100000
diff --git a/pyuno/zipcore/python.cxx b/pyuno/zipcore/python.cxx
index 517a6ae..0e8bb6c 100644
--- a/pyuno/zipcore/python.cxx
+++ b/pyuno/zipcore/python.cxx
@@ -131,21 +131,6 @@ int wmain(int argc, wchar_t ** argv, wchar_t **) {
    if (pythonexeEnd == NULL) {
        exit(EXIT_FAILURE);
    }
    wchar_t urepath[MAX_PATH];
    if (tools::buildPath(urepath, path, pathEnd, MY_STRING(L"..\\ure-link"))
        == NULL)
    {
        exit(EXIT_FAILURE);
    }
    wchar_t * urepathEnd = tools::resolveLink(urepath);
    if (urepathEnd == NULL) {
        exit(EXIT_FAILURE);
    }
    urepathEnd = tools::buildPath(
        urepath, urepath, urepathEnd, MY_STRING(L"\\bin"));
    if (urepathEnd == NULL) {
        exit(EXIT_FAILURE);
    }
    std::size_t clSize = MY_LENGTH(L"\"") + 4 * (pythonexeEnd - pythonexe) +
        MY_LENGTH(L"\"\0"); //TODO: overflow
        // 4 * len: each char preceded by backslash, each trailing backslash
@@ -192,12 +177,12 @@ int wmain(int argc, wchar_t ** argv, wchar_t **) {
            exit(EXIT_FAILURE);
        }
    }
    std::size_t len = (urepathEnd - urepath) + MY_LENGTH(L";") +
        (pathEnd - path) + (n == 0 ? 0 : MY_LENGTH(L";") + (n - 1)) + 1;
    std::size_t len = (pathEnd - path) + (n == 0 ? 0 : MY_LENGTH(L";") +
        (n - 1)) + 1;
        //TODO: overflow
    wchar_t * value = new wchar_t[len];
    _snwprintf(
        value, len, L"%s;%s%s%s", urepath, path, n == 0 ? L"" : L";", orig);
        value, len, L"%s%s%s", path, n == 0 ? L"" : L";", orig);
    if (!SetEnvironmentVariableW(L"PATH", value)) {
        exit(EXIT_FAILURE);
    }
diff --git a/scp2/inc/macros.inc b/scp2/inc/macros.inc
index cccf0d4..6f8e6ab 100644
--- a/scp2/inc/macros.inc
+++ b/scp2/inc/macros.inc
@@ -65,19 +65,10 @@
#ifdef MACOSX
    #define GID_BRAND_DIR_ETC gid_Brand_Dir_Share
    #define GID_DIR_PY gid_Brand_Dir_Share
    #define GID_DIR_URE_BIN gid_Brand_Dir_Program
    #define GID_DIR_URE_JAVA gid_Dir_Classes
    #define GID_DIR_URE_LIB gid_Dir_Frameworks
#else
    #define GID_BRAND_DIR_ETC gid_Brand_Dir_Program
    #define GID_DIR_PY gid_Brand_Dir_Program
#if defined UNX
    #define GID_DIR_URE_BIN gid_Brand_Dir_Program
    #define GID_DIR_URE_JAVA gid_Dir_Classes
#else
    #define GID_DIR_URE_BIN gid_Dir_Ure_Bin
    #define GID_DIR_URE_JAVA gid_Dir_Ure_Java
#endif
    #define GID_DIR_URE_LIB gid_Brand_Dir_Program
#endif

@@ -246,7 +237,7 @@ End
    File id \
        TXT_FILE_BODY; \
        Name = name; \
        Dir = GID_DIR_URE_JAVA; \
        Dir = gid_Dir_Classes; \
        Styles = (PACKED); \
    End

@@ -463,9 +454,8 @@ End
#endif



#define SCP2_URE_DL_DIR GID_DIR_URE_LIB
#if defined WNT
#define SCP2_URE_DL_DIR gid_Dir_Ure_Bin
#define SCP2_URE_DL_NORMAL(n) n ".dll"
#define SCP2_URE_DL_BARE(n) n ".dll"
#define SCP2_URE_DL_VER(n, v) n v ".dll"
@@ -474,7 +464,6 @@ End
#define SCP2_URE_DL_UNO_VER(n, v) n v ".dll"
#define SCP2_URE_DL_UNO_COMID_VER(n, v) n v STRING(COMID) ".dll"
#else
#define SCP2_URE_DL_DIR GID_DIR_URE_LIB
#define SCP2_URE_DL_NORMAL(n) "lib" n STRING(UNXSUFFIX)
#define SCP2_URE_DL_BARE(n) n STRING(UNXSUFFIX)
#define SCP2_URE_DL_VER(n, v) "lib" n STRING(UNXSUFFIX) "." v
@@ -536,7 +525,7 @@ End
#define URE_EXECUTABLE(id,name) \
    File id \
        BIN_FILE_BODY; \
        Dir = GID_DIR_URE_BIN; \
        Dir = gid_Brand_Dir_Program; \
        Name = name; \
        Styles = (PACKED); \
    End
diff --git a/scp2/source/ooo/common_brand.scp b/scp2/source/ooo/common_brand.scp
index a6075e7..88d8166 100644
--- a/scp2/source/ooo/common_brand.scp
+++ b/scp2/source/ooo/common_brand.scp
@@ -1126,11 +1126,7 @@ ProfileItem gid_Brand_Profileitem_Fundamental_Ure_Bin_Dir
    ProfileID = gid_Brand_Profile_Fundamental_Ini;
    Section = "Bootstrap";
    Key = "URE_BIN_DIR";
#if defined WNT
    Value = "${.link:${BRAND_BASE_DIR}/ure-link}/bin";
#else
    Value = "${BRAND_BASE_DIR}/" LIBO_URE_BIN_FOLDER;
#endif
End

ProfileItem gid_Brand_Profileitem_Fundamental_Configuration_Layers
diff --git a/scp2/source/ooo/ure.scp b/scp2/source/ooo/ure.scp
index 9bb8ebe..61af96a 100644
--- a/scp2/source/ooo/ure.scp
+++ b/scp2/source/ooo/ure.scp
@@ -20,32 +20,6 @@

#include "AutoInstall/ure"

#if !defined MACOSX

#if defined WNT
Directory gid_Dir_Common_Ure
    ParentID = gid_Dir_Brand_Root;
    HostName = "URE";
    Styles = (UREDIRECTORY);
End
#endif

#if defined WNT
Directory gid_Dir_Ure_Bin
    ParentID = gid_Dir_Common_Ure;
    DosName = "bin";
End
#endif

#if defined WNT && defined ENABLE_JAVA
Directory gid_Dir_Ure_Java
    ParentID = gid_Dir_Common_Ure;
    DosName = "java";
End
#endif

#endif // !MACOSX

#if defined MACOSX
Directory gid_Dir_Ure_Share_Misc
    ParentID = gid_Brand_Dir_Share_Ure_Share;
@@ -53,13 +27,6 @@ Directory gid_Dir_Ure_Share_Misc
End
#endif

#ifdef WNT
Directory gid_Dir_Ure_Misc
    ParentID = gid_Dir_Common_Ure;
    DosName = "misc";
End
#endif

// Public Dynamic Libraries:

#ifdef AIX
@@ -293,9 +260,7 @@ End

File gid_File_Misc_TypesRdb
    TXT_FILE_BODY;
#ifdef WNT
    Dir = gid_Dir_Ure_Misc;
#elif defined MACOSX
#if defined MACOSX
    Dir = gid_Dir_Ure_Share_Misc;
#else
    Dir = gid_Brand_Dir_Program;
@@ -306,9 +271,7 @@ End

File gid_File_Misc_ServicesRdb
    TXT_FILE_BODY;
#ifdef WNT
    Dir = gid_Dir_Ure_Misc;
#elif defined MACOSX
#if defined MACOSX
    Dir = gid_Dir_Ure_Share_Misc;
#else
    Dir = gid_Brand_Dir_Program;
@@ -328,10 +291,7 @@ Module gid_Module_Root_Ure_Hidden
	Sortkey = "2200";
	Default = YES;
	Styles = (HIDDEN_ROOT);
    Dirs = (gid_Dir_Ure_Bin,
            gid_Dir_Ure_Misc,
            gid_Dir_Ure_Java,
            gid_Dir_Ure_Share_Misc);
    Dirs = (gid_Dir_Ure_Share_Misc);
    Files = (auto_ure_ALL,
            gid_File_ThirdpartylicensereadmeHtml_Ure,
            gid_File_Readme_Ure,
diff --git a/tools/source/misc/pathutils.cxx b/tools/source/misc/pathutils.cxx
index f4507d8..0b3f172 100644
--- a/tools/source/misc/pathutils.cxx
+++ b/tools/source/misc/pathutils.cxx
@@ -102,107 +102,6 @@ WCHAR * buildPath(
    }
}

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
diff --git a/ure/Package_install.mk b/ure/Package_install.mk
index c02cf29..99801c7 100644
--- a/ure/Package_install.mk
+++ b/ure/Package_install.mk
@@ -15,8 +15,6 @@ endif

ifeq (MACOSX,$(OS))
$(eval $(call gb_Package_add_symbolic_link,ure_install,MacOS/urelibs,../Frameworks))
else ifeq (WNT,$(OS))
$(eval $(call gb_Package_add_file,ure_install,ure-link,ure-link))
endif

# vim:set noet sw=4 ts=4:
diff --git a/ure/source/ure-link b/ure/source/ure-link
deleted file mode 100644
index 122de34..0000000
--- a/ure/source/ure-link
+++ /dev/null
@@ -1 +0,0 @@
URE
\ No newline at end of file