diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-27 16:51:28 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-27 16:51:28 +0000 |
commit | 940b4d1848e8c70ab7642901a68594e8016caffc (patch) | |
tree | eb72f344ee6c3d9b80a7ecc079ea79e9fba8676d /odk/source | |
parent | Initial commit. (diff) | |
download | libreoffice-1ad18e38974bb28c3d98d0be8f7d8c18fc56de29.tar.xz libreoffice-1ad18e38974bb28c3d98d0be8f7d8c18fc56de29.zip |
Adding upstream version 1:7.0.4.upstream/1%7.0.4upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'odk/source')
-rw-r--r-- | odk/source/com/sun/star/lib/loader/InstallationFinder.java | 590 | ||||
-rw-r--r-- | odk/source/com/sun/star/lib/loader/Loader.java | 340 | ||||
-rw-r--r-- | odk/source/com/sun/star/lib/loader/WinRegKey.java | 204 | ||||
-rw-r--r-- | odk/source/com/sun/star/lib/loader/WinRegKeyException.java | 45 | ||||
-rw-r--r-- | odk/source/unoapploader/unx/unoapploader.c | 310 | ||||
-rw-r--r-- | odk/source/unoapploader/win/unoapploader.c | 410 | ||||
-rw-r--r-- | odk/source/unowinreg/win/unowinreg.cxx | 170 |
7 files changed, 2069 insertions, 0 deletions
diff --git a/odk/source/com/sun/star/lib/loader/InstallationFinder.java b/odk/source/com/sun/star/lib/loader/InstallationFinder.java new file mode 100644 index 000000000..5caef5445 --- /dev/null +++ b/odk/source/com/sun/star/lib/loader/InstallationFinder.java @@ -0,0 +1,590 @@ +/* -*- Mode: Java; 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 . + */ + +package com.sun.star.lib.loader; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileInputStream; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.IOException; +import java.io.UnsupportedEncodingException; +import java.net.MalformedURLException; +import java.net.URL; +import java.util.StringTokenizer; +import java.util.ArrayList; + +/** + * This class finds a UNO installation on the system. + * + * <p>A UNO installation can be specified by the user by either setting the + * com.sun.star.lib.loader.unopath system property or by setting the + * UNO_PATH environment variable to the program directory of a UNO + * installation. + * Note, that Java 1.3.1 and Java 1.4 don't support environment variables + * (System.getenv() throws java.lang.Error) and therefore setting the UNO_PATH + * environment variable won't work with those Java versions. + * If no UNO installation is specified by the user, the default installation + * on the system will be returned.</p> + * + * <p>On the Windows platform the default installation is read from the Windows + * Registry.</p> + * + * <p>On the Unix/Linux platforms the default installation is found from the + * PATH environment variable. Note, that for Java 1.3.1 and Java 1.4 the + * default installation is found by using the 'which' command, because + * environment variables are not supported with those Java versions. + * Both methods require that the 'soffice' executable or a symbolic + * link is in one of the directories listed in the PATH environment variable. + * For older versions than OOo 2.0 the above described methods may fail. + * In this case the default installation is taken from the .versionrc file in + * the user's home directory. Note, that the .sversionrc file will be omitted + * for OOo 2.0</p> + */ +final class InstallationFinder { + + private static final String SYSPROP_NAME = + "com.sun.star.lib.loader.unopath"; + private static final String ENVVAR_NAME = "UNO_PATH"; + private static final String SOFFICE = "libreoffice"; // Unix/Linux only + + private InstallationFinder() {} // do not instantiate + + /** + * Gets the path of a UNO installation. + * + * @return the installation path or <code>null</code>, if no installation + * was specified or found, or if an error occurred + */ + public static String getPath() { + + String path = null; + + // get the installation path from the Java system property + // com.sun.star.lib.loader.unopath + // (all platforms) + path = getPathFromProperty( SYSPROP_NAME ); + if ( path != null ) { + return path; + } + // get the installation path from the UNO_PATH environment variable + // (all platforms, not working for Java 1.3.1 and Java 1.4) + path = getPathFromEnvVar( ENVVAR_NAME ); + if ( path != null ) { + return path; + } + + String osname = null; + try { + osname = System.getProperty( "os.name" ); + } catch ( SecurityException e ) { + // if a SecurityException was thrown, + // return <code>null</code> + return null; + } + if ( osname == null ) { + return null; + } + + if ( osname.startsWith( "Windows" ) ) { + // get the installation path from the Windows Registry + // (Windows platform only) + path = getPathFromWindowsRegistry(); + } else { + // get the installation path from the PATH environment + // variable (Unix/Linux platforms only, not working for + // Java 1.3.1 and Java 1.4) + path = getPathFromPathEnvVar(); + if ( path == null ) { + // get the installation path from the 'which' + // command (Unix/Linux platforms only) + path = getPathFromWhich(); + if ( path == null ) { + // get the installation path from the + // .sversionrc file (Unix/Linux platforms only, + // for older versions than OOo 2.0) + path = getPathFromSVersionFile(); + } + } + } + + return path; + } + + /** + * Gets the installation path from a Java system property. + * + * <p>This method is called on all platforms. + * The Java system property can be passed into the application by using + * the -D flag, e.g. + * java -D<property name>=<installation path> -jar application.jar.</p> + * + * @return the installation path or <code>null</code>, if no installation + * was specified in the Java system property or if an error occurred + */ + private static String getPathFromProperty( String prop ) { + + String path = null; + + try { + path = System.getProperty( prop ); + } catch ( SecurityException e ) { + // if a SecurityException was thrown, return <code>null</code> + } + + return path; + } + + /** + * Gets the installation path from an environment variable. + * + * <p>This method is called on all platforms. + * Note, that in Java 1.3.1 and Java 1.4 System.getenv() throws + * java.lang.Error and therefore this method returns null for those + * Java versions.</p> + * + * @return the installation path or <code>null</code>, if no installation + * was specified in the environment variable or if an error occurred + */ + private static String getPathFromEnvVar( String var ) { + + String path = null; + + try { + path = System.getenv( var ); + } catch ( SecurityException e ) { + // if a SecurityException was thrown, return <code>null</code> + } catch ( java.lang.Error err ) { + // System.getenv() throws java.lang.Error in Java 1.3.1 and + // Java 1.4 + } + + return path; + } + + /** + * Gets the installation path from the Windows Registry. + * + * <p>This method is called on the Windows platform only.</p> + * + * @return the installation path or <code>null</code>, if no installation + * was found or if an error occurred + */ + private static String getPathFromWindowsRegistry() { + + final String SUBKEYNAME = "Software\\LibreOffice\\UNO\\InstallPath"; + + String path = null; + + try { + // read the key's default value from HKEY_CURRENT_USER + WinRegKey key = new WinRegKey( "HKEY_CURRENT_USER", SUBKEYNAME ); + path = key.getStringValue( "" ); // default + } catch ( WinRegKeyException e ) { + try { + // read the key's default value from HKEY_LOCAL_MACHINE + WinRegKey key = new WinRegKey( "HKEY_LOCAL_MACHINE", + SUBKEYNAME ); + path = key.getStringValue( "" ); // default + } catch ( WinRegKeyException we ) { + System.err.println( "com.sun.star.lib.loader." + + "InstallationFinder::getPathFromWindowsRegistry: " + + "reading key from Windows Registry failed: " + we ); + } + } + + return path; + } + + /** + * Gets the installation path from the PATH environment variable. + * + * <p>This method is called on Unix/Linux platforms only. + * An installation is found, if the executable 'soffice' or a symbolic link + * is in one of the directories listed in the PATH environment variable. + * Note, that in Java 1.3.1 and Java 1.4 System.getenv() throws + * java.lang.Error and therefore this method returns null for those + * Java versions.</p> + * + * @return the installation path or <code>null</code>, if no installation + * was found or if an error occurred + */ + private static String getPathFromPathEnvVar() { + + final String PATH_ENVVAR_NAME = "PATH"; + + String path = null; + String str = null; + + try { + str = System.getenv( PATH_ENVVAR_NAME ); + } catch ( SecurityException e ) { + // if a SecurityException was thrown, return <code>null</code> + return null; + } catch ( java.lang.Error err ) { + // System.getenv() throws java.lang.Error in Java 1.3.1 and + // Java 1.4 + return null; + } + + if ( str != null ) { + StringTokenizer tokens = new StringTokenizer( + str, File.pathSeparator ); + while ( tokens.hasMoreTokens() ) { + File file = new File( tokens.nextToken(), SOFFICE ); + try { + if ( file.exists() ) { + try { + // resolve symlink + path = file.getCanonicalFile().getParent(); + if ( path != null ) + break; + } catch ( IOException e ) { + // if an I/O exception is thrown, ignore this + // path entry and try the next one + System.err.println( "com.sun.star.lib.loader." + + "InstallationFinder::getPathFromEnvVar: " + + "bad path: " + e ); + } + } + } catch ( SecurityException e ) { + // if a SecurityException was thrown, ignore this path + // entry and try the next one + } + } + } + + return path; + } + + /** + * Gets the installation path from the 'which' command on Unix/Linux + * platforms. + * + * <p>This method is called on Unix/Linux platforms only. + * An installation is found, if the executable 'soffice' or a symbolic link + * is in one of the directories listed in the PATH environment variable.</p> + * + * @return the installation path or <code>null</code>, if no installation + * was found or if an error occurred + */ + private static String getPathFromWhich() { + + final String WHICH = "which"; + + String path = null; + + // start the which process + String[] cmdArray = new String[] { WHICH, SOFFICE }; + Process proc = null; + Runtime rt = Runtime.getRuntime(); + try { + proc = rt.exec( cmdArray ); + } catch ( SecurityException e ) { + return null; + } catch ( IOException e ) { + // if an I/O exception is thrown, return <code>null</null> + System.err.println( "com.sun.star.lib.loader." + + "InstallationFinder::getPathFromWhich: " + + "which command failed: " + e ); + return null; + } + + // empty standard error stream in a separate thread + StreamGobbler gobbler = new StreamGobbler( proc.getErrorStream() ); + gobbler.start(); + + try { + // read the which output from standard input stream + BufferedReader br = new BufferedReader( + new InputStreamReader( proc.getInputStream(), "UTF-8" ) ); + String line = null; + try { + while ( ( line = br.readLine() ) != null ) { + if ( path == null ) { + // get the path from the which output + int index = line.lastIndexOf( SOFFICE ); + if ( index != -1 ) { + int end = index + SOFFICE.length(); + for ( int i = 0; i <= index; i++ ) { + File file = new File( line.substring( i, end ) ); + try { + if ( file.exists() ) { + // resolve symlink + path = file.getCanonicalFile().getParent(); + if ( path != null ) + break; + } + } catch ( SecurityException e ) { + return null; + } + } + } + } + } + } catch ( IOException e ) { + // if an I/O exception is thrown, return <code>null</null> + System.err.println( "com.sun.star.lib.loader." + + "InstallationFinder::getPathFromWhich: " + + "reading which command output failed: " + e ); + return null; + } finally { + try { + br.close(); + } catch ( IOException e ) { + // closing standard input stream failed, ignore + } + } + } catch ( UnsupportedEncodingException e ) { + // if an Encoding exception is thrown, return <code>null</null> + System.err.println( "com.sun.star.lib.loader." + + "InstallationFinder::getPathFromWhich: " + + "encoding failed: " + e ); + return null; + } + + try { + // wait until the which process has terminated + proc.waitFor(); + } catch ( InterruptedException e ) { + // the current thread was interrupted by another thread, + // kill the which process + proc.destroy(); + // set the interrupted status + Thread.currentThread().interrupt(); + } + + return path; + } + + /** + * Gets the installation path from the .sverionrc file in the user's home + * directory. + * + * <p>This method is called on Unix/Linux platforms only. + * The .sversionrc file is written during setup and will be omitted for + * OOo 2.0.</p> + * + * @return the installation path or <code>null</code>, if no installation + * was found or if an error occurred + */ + private static String getPathFromSVersionFile() { + + final String SVERSION = ".sversionrc"; // Unix/Linux only + final String VERSIONS = "[Versions]"; + + String path = null; + + try { + File fSVersion = new File( + System.getProperty( "user.home" ) ,SVERSION ); + if ( fSVersion.exists() ) { + ArrayList<String> lines = new ArrayList<String>(); + BufferedReader br = null; + try { + br = new BufferedReader( new InputStreamReader( + new FileInputStream( fSVersion ), "UTF-8" ) ); + String line = null; + while ( ( line = br.readLine() ) != null && + !line.equals( VERSIONS ) ) { + // read lines until [Versions] is found + } + while ( ( line = br.readLine() ) != null && + line.length() != 0 ) { + if ( !line.startsWith( ";" ) ) + lines.add( line ); + } + } catch ( IOException e ) { + // if an I/O exception is thrown, try to analyze the lines + // read so far + System.err.println( "com.sun.star.lib.loader." + + "InstallationFinder::getPathFromSVersionFile: " + + "reading .sversionrc file failed: " + e ); + } finally { + if ( br != null ) { + try { + br.close(); + } catch ( IOException e ) { + // closing .sversionrc failed, ignore + } + } + } + for ( int i = lines.size() - 1; i >= 0; i-- ) { + StringTokenizer tokens = new StringTokenizer( + lines.get( i ), "=" ); + if ( tokens.countTokens() != 2 ) + continue; + tokens.nextToken(); // key + String url = tokens.nextToken(); + path = getCanonicalPathFromFileURL( url ); + if ( path != null ) + break; + } + } + } catch ( SecurityException e ) { + return null; + } + + return path; + } + + /** + * Translates an OOo-internal absolute file URL reference (encoded using + * UTF-8) into a Java canonical pathname. + * + * @param oooUrl any URL reference; any fragment part is ignored + * + * @return if the given URL is a valid absolute, local (that is, the host + * part is empty or equal to "localhost", ignoring case) file URL, it is + * converted into an absolute canonical pathname; otherwise, + * <code>null</code> is returned + */ + private static String getCanonicalPathFromFileURL( String oooUrl ) { + + String prefix = "file://"; + if (oooUrl.length() < prefix.length() + || !oooUrl.substring(0, prefix.length()).equalsIgnoreCase( + prefix)) + { + return null; + } + StringBuffer buf = new StringBuffer(prefix); + int n = oooUrl.indexOf('/', prefix.length()); + if (n < 0) { + n = oooUrl.length(); + } + String host = oooUrl.substring(prefix.length(), n); + if (host.length() != 0 && !host.equalsIgnoreCase("localhost")) { + return null; + } + buf.append(host); + if (n == oooUrl.length()) { + buf.append('/'); + } else { + loop: + while (n < oooUrl.length()) { + buf.append('/'); + ++n; + int n2 = oooUrl.indexOf('/', n); + if (n2 < 0) { + n2 = oooUrl.length(); + } + while (n < n2) { + char c = oooUrl.charAt(n); + switch (c) { + case '%': + byte[] bytes = new byte[(n2 - n) / 3]; + int len = 0; + while (oooUrl.length() - n > 2 + && oooUrl.charAt(n) == '%') + { + int d1 = Character.digit(oooUrl.charAt(n + 1), 16); + int d2 = Character.digit(oooUrl.charAt(n + 2), 16); + if (d1 < 0 || d2 < 0) { + break; + } + int d = 16 * d1 + d2; + if (d == '/') { + return null; + } + bytes[len++] = (byte) d; + n += 3; + } + String s; + try { + s = new String(bytes, 0, len, "UTF-8"); + } catch (UnsupportedEncodingException e) { + return null; + } + buf.append(s); + break; + + case '#': + break loop; + + default: + buf.append(c); + ++n; + break; + } + } + } + } + URL url; + try { + url = new URL(buf.toString()); + } catch (MalformedURLException e) { + return null; + } + String path = url.getFile(); + String fragment = url.getRef(); + if (fragment != null) { + path += '#' + fragment; + } + String ret = null; + File file = new File( path, SOFFICE ); + try { + if ( file.isAbsolute() && file.exists() ) { + try { + // resolve symlink + ret = file.getCanonicalFile().getParent(); + } catch ( IOException e ) { + return null; + } + } + } catch ( SecurityException e ) { + return null; + } + + return ret; + } + + /** + This class is used for emptying any stream which is passed into it in + a separate thread. + */ + private static final class StreamGobbler extends Thread { + + InputStream m_istream; + + StreamGobbler( InputStream istream ) { + m_istream = istream; + } + + @Override + public void run() { + try { + BufferedReader br = new BufferedReader( + new InputStreamReader( m_istream, "UTF-8" ) ); + // read from input stream + while ( br.readLine() != null ) { + // don't handle line content + } + br.close(); + } catch (UnsupportedEncodingException e) { + // cannot read from input stream + } catch ( IOException e ) { + // stop reading from input stream + } + } + } +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/odk/source/com/sun/star/lib/loader/Loader.java b/odk/source/com/sun/star/lib/loader/Loader.java new file mode 100644 index 000000000..bc1a3c3e7 --- /dev/null +++ b/odk/source/com/sun/star/lib/loader/Loader.java @@ -0,0 +1,340 @@ +/* -*- Mode: Java; 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 . + */ + +package com.sun.star.lib.loader; + +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.io.UnsupportedEncodingException; +import java.lang.reflect.Method; +import java.net.JarURLConnection; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLClassLoader; +import java.util.Enumeration; +import java.util.jar.Attributes; +import java.util.jar.Manifest; +import java.util.StringTokenizer; +import java.util.ArrayList; +import java.security.AccessController; +import java.security.PrivilegedAction; + +/** + * This class can be used as a loader for application classes which use UNO. + * + * <p>The Loader class detects a UNO installation on the system and adds the + * UNO jar files to the search path of a customized class loader, which is used + * for loading the application classes.</p> + */ +public final class Loader { + + private static ClassLoader m_Loader = null; + + /** + * do not instantiate + */ + private Loader() {} + + /** + * The main method instantiates a customized class loader with the + * UNO jar files added to the search path and loads the application class, + * which is specified in the Main-Class attribute of the + * com/sun/star/lib/Loader.class entry of the manifest file or + * as first parameter in the argument list. + */ + public static void main( String[] arguments ) throws Exception { + + // get the name of the class to be loaded from the manifest + String className = null; + Class clazz = Loader.class; + ClassLoader loader = clazz.getClassLoader(); + ArrayList<URL> res = new ArrayList<URL>(); + try { + Enumeration<URL> en = loader.getResources( "META-INF/MANIFEST.MF" ); + while ( en.hasMoreElements() ) { + res.add( en.nextElement() ); + } + // the jarfile with the com/sun/star/lib/loader/Loader.class + // per-entry attribute is most probably the last resource in the + // list, therefore search backwards + for ( int i = res.size() - 1; i >= 0; i-- ) { + URL jarurl = res.get( i ); + try { + JarURLConnection jarConnection = + (JarURLConnection) jarurl.openConnection(); + Manifest mf = jarConnection.getManifest(); + Attributes attrs = (mf != null) ? mf.getAttributes( + "com/sun/star/lib/loader/Loader.class") : null; + if ( attrs != null ) { + className = attrs.getValue( "Application-Class" ); + if ( className != null ) + break; + } + } catch ( IOException e ) { + // if an I/O error occurs when opening a new + // JarURLConnection, ignore this manifest file + System.err.println( "com.sun.star.lib.loader.Loader::" + + "main: bad manifest file: " + e ); + } + } + } catch ( IOException e ) { + // if an I/O error occurs when getting the manifest resources, + // try to get the name of the class to be loaded from the argument + // list + System.err.println( "com.sun.star.lib.loader.Loader::" + + "main: cannot get manifest resources: " + e ); + } + + // if no manifest entry was found, get the name of the class + // to be loaded from the argument list + String[] args; + if ( className == null ) { + if ( arguments.length > 0 ) { + className = arguments[0]; + args = new String[arguments.length - 1]; + System.arraycopy( arguments, 1, args, 0, args.length ); + } else { + throw new IllegalArgumentException( + "The name of the class to be loaded must be either " + + "specified in the Main-Class attribute of the " + + "com/sun/star/lib/loader/Loader.class entry " + + "of the manifest file or as a command line argument." ); + } + } else { + args = arguments; + } + + // load the class with the customized class loader and + // invoke the main method + if ( className != null ) { + ClassLoader cl = getCustomLoader(); + Thread.currentThread().setContextClassLoader(cl); + Class c = cl.loadClass( className ); + @SuppressWarnings("unchecked") + Method m = c.getMethod( "main", new Class[] { String[].class } ); + m.invoke( null, new Object[] { args } ); + } + } + + /** + * Gets the customized class loader with the UNO jar files added to the + * search path. + * + * @return the customized class loader + */ + public static synchronized ClassLoader getCustomLoader() { + if ( m_Loader == null ) { + + // get the urls from which to load classes and resources + // from the class path + ArrayList<URL> vec = new ArrayList<URL>(); + String classpath = null; + try { + classpath = System.getProperty( "java.class.path" ); + } catch ( SecurityException e ) { + // don't add the class path entries to the list of class + // loader URLs + System.err.println( "com.sun.star.lib.loader.Loader::" + + "getCustomLoader: cannot get system property " + + "java.class.path: " + e ); + } + if ( classpath != null ) { + addUrls(vec, classpath, File.pathSeparator); + } + + // get the urls from which to load classes and resources + // from the UNO installation + String path = InstallationFinder.getPath(); + if ( path != null ) { + callUnoinfo(path, vec); + } else { + System.err.println( "com.sun.star.lib.loader.Loader::" + + "getCustomLoader: no UNO installation found!" ); + } + + // copy urls to array + final URL[] urls = new URL[vec.size()]; + vec.toArray( urls ); + + // instantiate class loader + m_Loader = AccessController.doPrivileged( + new PrivilegedAction<ClassLoader>() { + public ClassLoader run() { + return new CustomURLClassLoader(urls); + } + }); + } + + return m_Loader; + } + + private static void addUrls(ArrayList<URL> urls, String data, String delimiter) { + StringTokenizer tokens = new StringTokenizer( data, delimiter ); + while ( tokens.hasMoreTokens() ) { + try { + urls.add( new File( tokens.nextToken() ).toURI().toURL() ); + } catch ( MalformedURLException e ) { + // don't add this class path entry to the list of class loader + // URLs + System.err.println( "com.sun.star.lib.loader.Loader::" + + "getCustomLoader: bad pathname: " + e ); + } + } + } + + private static void callUnoinfo(String path, ArrayList<URL> urls) { + Process p; + try { + p = Runtime.getRuntime().exec( + new String[] { new File(path, "unoinfo").getPath(), "java" }); + } catch (IOException e) { + System.err.println( + "com.sun.star.lib.loader.Loader::getCustomLoader: exec" + + " unoinfo: " + e); + return; + } + new Drain(p.getErrorStream()).start(); + int code; + byte[] buf = new byte[1000]; + int n = 0; + try { + InputStream s = p.getInputStream(); + code = s.read(); + for (;;) { + if (n == buf.length) { + if (n > Integer.MAX_VALUE / 2) { + System.err.println( + "com.sun.star.lib.loader.Loader::getCustomLoader:" + + " too much unoinfo output"); + return; + } + byte[] buf2 = new byte[2 * n]; + System.arraycopy(buf, 0, buf2, 0, n); + buf = buf2; + } + int k = s.read(buf, n, buf.length - n); + if (k == -1) { + break; + } + n += k; + } + } catch (IOException e) { + System.err.println( + "com.sun.star.lib.loader.Loader::getCustomLoader: reading" + + " unoinfo output: " + e); + return; + } + int ev; + try { + ev = p.waitFor(); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + System.err.println( + "com.sun.star.lib.loader.Loader::getCustomLoader: waiting for" + + " unoinfo: " + e); + return; + } + if (ev != 0) { + System.err.println( + "com.sun.star.lib.loader.Loader::getCustomLoader: unoinfo" + + " exit value " + n); + return; + } + String s; + if (code == '0') { + s = new String(buf); + } else if (code == '1') { + try { + s = new String(buf, "UTF-16LE"); + } catch (UnsupportedEncodingException e) { + System.err.println( + "com.sun.star.lib.loader.Loader::getCustomLoader:" + + " transforming unoinfo output: " + e); + return; + } + } else { + System.err.println( + "com.sun.star.lib.loader.Loader::getCustomLoader: bad unoinfo" + + " output"); + return; + } + addUrls(urls, s, "\0"); + } + + private static final class Drain extends Thread { + public Drain(InputStream stream) { + super("unoinfo stderr drain"); + this.stream = stream; + } + + @Override + public void run() { + try { + while (stream.read() != -1) {} + } catch (IOException e) { /* ignored */ } + } + + private final InputStream stream; + } + + /** + * A customized class loader which is used to load classes and resources + * from a search path of user-defined URLs. + */ + private static final class CustomURLClassLoader extends URLClassLoader { + + public CustomURLClassLoader( URL[] urls ) { + super( urls ); + } + + @Override + protected Class<?> findClass( String name ) throws ClassNotFoundException { + // This is only called via this.loadClass -> super.loadClass -> + // this.findClass, after this.loadClass has already called + // super.findClass, so no need to call super.findClass again: + throw new ClassNotFoundException( name ); + } + + @Override + protected synchronized Class<?> loadClass( String name, boolean resolve ) + throws ClassNotFoundException + { + Class c = findLoadedClass( name ); + if ( c == null ) { + try { + c = super.findClass( name ); + } catch ( ClassNotFoundException e ) { + return super.loadClass( name, resolve ); + } catch ( SecurityException e ) { + // A SecurityException "Prohibited package name: java.lang" + // may occur when the user added the JVM's rt.jar to the + // java.class.path: + return super.loadClass( name, resolve ); + } + } + if ( resolve ) { + resolveClass( c ); + } + return c; + } + } +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/odk/source/com/sun/star/lib/loader/WinRegKey.java b/odk/source/com/sun/star/lib/loader/WinRegKey.java new file mode 100644 index 000000000..12cfc45ba --- /dev/null +++ b/odk/source/com/sun/star/lib/loader/WinRegKey.java @@ -0,0 +1,204 @@ +/* -*- Mode: Java; 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 . + */ + +package com.sun.star.lib.loader; + +import java.io.BufferedInputStream; +import java.io.BufferedOutputStream; +import java.io.File; +import java.io.FileOutputStream; +import java.io.InputStream; + + +/** + * This class provides functionality for reading string values from the + * Windows Registry. It requires the native library unowinreg.dll. + */ +final class WinRegKey { + + private final String m_rootKeyName; + private final String m_subKeyName; + + // native methods to access the windows registry + private static native boolean winreg_RegOpenClassesRoot( long[] hkresult ); + private static native boolean winreg_RegOpenCurrentConfig( + long[] hkresult ); + private static native boolean winreg_RegOpenCurrentUser( long[] hkresult ); + private static native boolean winreg_RegOpenLocalMachine( long[] hkresult ); + private static native boolean winreg_RegOpenUsers( long[] hkresult ); + private static native boolean winreg_RegOpenKeyEx( long parent, String name, + long[] hkresult ); + private static native boolean winreg_RegCloseKey( long hkey ); + private static native boolean winreg_RegQueryValueEx( + long hkey, String value, long[] type, + byte[] data, long[] size ); + private static native boolean winreg_RegQueryInfoKey( + long hkey, long[] subkeys, long[] maxSubkeyLen, + long[] values, long[] maxValueNameLen, + long[] maxValueLen, long[] secDescriptor ); + + // load the native library unowinreg.dll + static { + try { + ClassLoader cl = WinRegKey.class.getClassLoader(); + InputStream is = cl.getResourceAsStream( "win/unowinreg.dll" ); + if ( is != null ) { + // generate a temporary name for lib file and write to temp + // location + File libfile; + BufferedInputStream istream = null; + BufferedOutputStream ostream = null; + try { + istream = new BufferedInputStream( is ); + libfile = File.createTempFile( "unowinreg", ".dll" ); + libfile.deleteOnExit(); // ensure deletion + ostream = new BufferedOutputStream( + new FileOutputStream( libfile ) ); + int bsize = 2048; int n = 0; + byte[] buffer = new byte[bsize]; + while ( ( n = istream.read( buffer, 0, bsize ) ) != -1 ) { + ostream.write( buffer, 0, n ); + } + } finally { + if (istream != null) { + istream.close(); + } + if (ostream != null) { + ostream.close(); + } + } + // load library + System.load( libfile.getPath() ); + } else { + // If the library cannot be found as a class loader resource, + // try the global System.loadLibrary(). The JVM will look for + // it in the java.library.path. + System.loadLibrary( "unowinreg" ); + } + } catch ( java.lang.Exception e ) { + System.err.println( "com.sun.star.lib.loader.WinRegKey: " + + "loading of native library failed!" + e ); + } + } + + /** + * Constructs a <code>WinRegKey</code>. + */ + public WinRegKey( String rootKeyName, String subKeyName ) { + m_rootKeyName = rootKeyName; + m_subKeyName = subKeyName; + } + + /** + * Reads a string value for the specified value name. + */ + public String getStringValue( String valueName ) throws WinRegKeyException { + byte[] data = getValue( valueName ); + // remove terminating null character + return new String( data, 0, data.length - 1 ); + } + + /** + * Reads a value for the specified value name. + */ + private byte[] getValue( String valueName ) throws WinRegKeyException { + + byte[] result = null; + long[] hkey = {0}; + + // open the specified registry key + boolean bRet = false; + long[] hroot = {0}; + if ( m_rootKeyName.equals( "HKEY_CLASSES_ROOT" ) ) { + bRet = winreg_RegOpenClassesRoot( hroot ); + } else if ( m_rootKeyName.equals( "HKEY_CURRENT_CONFIG" ) ) { + bRet = winreg_RegOpenCurrentConfig( hroot ); + } else if ( m_rootKeyName.equals( "HKEY_CURRENT_USER" ) ) { + bRet = winreg_RegOpenCurrentUser( hroot ); + } else if ( m_rootKeyName.equals( "HKEY_LOCAL_MACHINE" ) ) { + bRet = winreg_RegOpenLocalMachine( hroot ); + } else if ( m_rootKeyName.equals( "HKEY_USERS" ) ) { + bRet = winreg_RegOpenUsers( hroot ); + } else { + throw new WinRegKeyException( "unknown root registry key!"); + } + if ( !bRet ) { + throw new WinRegKeyException( "opening root registry key " + + "failed!" ); + } + if ( !winreg_RegOpenKeyEx( hroot[0], m_subKeyName, hkey ) ) { + if ( !winreg_RegCloseKey( hroot[0] ) ) { + throw new WinRegKeyException( "opening registry key and " + + "releasing root registry key handle failed!" ); + } + throw new WinRegKeyException( "opening registry key failed!" ); + } + + // get the size of the longest data component among the key's values + long[] subkeys = {0}; + long[] maxSubkeyLen = {0}; + long[] values = {0}; + long[] maxValueNameLen = {0}; + long[] maxValueLen = {0}; + long[] secDescriptor = {0}; + if ( !winreg_RegQueryInfoKey( hkey[0], subkeys, maxSubkeyLen, + values, maxValueNameLen, maxValueLen, secDescriptor ) ) { + if ( !winreg_RegCloseKey( hkey[0] ) || + !winreg_RegCloseKey( hroot[0] ) ) { + throw new WinRegKeyException( "retrieving information about " + + "the registry key and releasing registry key handles " + + "failed!" ); + } + throw new WinRegKeyException( "retrieving information about " + + "the registry key failed!" ); + } + + // get the data for the specified value name + byte[] buffer = new byte[ (int) maxValueLen[0] ]; + long[] size = new long[1]; + size[0] = buffer.length; + long[] type = new long[1]; + type[0] = 0; + if ( !winreg_RegQueryValueEx( hkey[0], valueName, type, buffer, + size ) ) { + if ( !winreg_RegCloseKey( hkey[0] ) || + !winreg_RegCloseKey( hroot[0] ) ) { + throw new WinRegKeyException( "retrieving data for the " + + "specified value name and releasing registry key handles " + + "failed!" ); + } + throw new WinRegKeyException( "retrieving data for the " + + "specified value name failed!" ); + } + + // release registry key handles + if ( !winreg_RegCloseKey( hkey[0] ) || + !winreg_RegCloseKey( hroot[0] ) ) { + throw new WinRegKeyException( "releasing registry key handles " + + "failed!" ); + } + + result = new byte[ (int) size[0] ]; + System.arraycopy( buffer, 0, result, 0, (int)size[0] ); + + return result; + } +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/odk/source/com/sun/star/lib/loader/WinRegKeyException.java b/odk/source/com/sun/star/lib/loader/WinRegKeyException.java new file mode 100644 index 000000000..aee93c06e --- /dev/null +++ b/odk/source/com/sun/star/lib/loader/WinRegKeyException.java @@ -0,0 +1,45 @@ +/* -*- Mode: Java; 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 . + */ + +package com.sun.star.lib.loader; + +/** + * WinRegKeyException is a checked exception. + */ +final class WinRegKeyException extends java.lang.Exception { + + /** + * Constructs a <code>WinRegKeyException</code>. + */ + public WinRegKeyException() { + super(); + } + + /** + * Constructs a <code>WinRegKeyException</code> with the specified + * detail message. + * + * @param message the detail message + */ + public WinRegKeyException( String message ) { + super( message ); + } +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/odk/source/unoapploader/unx/unoapploader.c b/odk/source/unoapploader/unx/unoapploader.c new file mode 100644 index 000000000..3a3559929 --- /dev/null +++ b/odk/source/unoapploader/unx/unoapploader.c @@ -0,0 +1,310 @@ +/* -*- 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 <stdlib.h> +#include <unistd.h> +#include <stdio.h> +#include <string.h> +#include <sys/stat.h> + +#ifdef LINUX +#define __USE_GNU +#endif +#include <dlfcn.h> + +#include <cppuhelper/findsofficepath.h> +#include <rtl/string.h> +#include <sal/types.h> + +static char* getPath(void); +static char* createCommandName( char* argv0 ); + +static const int SEPARATOR = '/'; +static const char* PATHSEPARATOR = ":"; + + +/* + * The main function implements a loader for applications which use UNO. + * + * <p>This code runs on the Unix/Linux platforms only.</p> + * + * <p>The main function detects a UNO installation on the system and adds the + * relevant directories of the installation to the LD_LIBRARY_PATH environment + * variable. After that, the application process is loaded and started, whereby + * the new process inherits the environment of the calling process, including + * the modified LD_LIBRARY_PATH environment variable. The application's + * executable name must be the same as the name of this executable, prefixed + * by '_'.</p> + * <p>On MACOSX DYLD_LIBRARY_PATH is used instead of LD_LIBRARY_PATH!<p> + * + * <p>A UNO installation can be specified by the user by setting the UNO_PATH + * environment variable to the program directory of the UNO installation. + * If no installation is specified by the user, the default installation on + * the system will be taken. The default installation is found from the + * PATH environment variable. This requires that the 'soffice' executable or + * a symbolic link is in one of the directories listed in the PATH environment + * variable.</p> + */ +int main( int argc, char *argv[] ) +{ + char* path; + char* cmdname; + + (void) argc; /* avoid warning about unused parameter */ + + /* get the path of the UNO installation */ + path = getPath(); + + if ( path != NULL ) + { +#if defined(MACOSX) + static const char* ENVVARNAME = "DYLD_LIBRARY_PATH"; +#elif defined(AIX) + static const char* ENVVARNAME = "LIBPATH"; +#else + static const char* ENVVARNAME = "LD_LIBRARY_PATH"; +#endif + char* libpath; + char* value; + char* envstr; + int size; + + size_t pathlen = strlen(path); + struct stat stats; + int ret; + + static char const unoinfoSuffix[] = "/unoinfo"; + char * unoinfo = malloc( + pathlen + RTL_CONSTASCII_LENGTH(unoinfoSuffix) + 1); + /*TODO: overflow */ + if (unoinfo == NULL) { + free(path); + fprintf(stderr, "Error: out of memory!\n"); + exit(EXIT_FAILURE); + } + strcpy(unoinfo, path); + strcpy( + unoinfo + pathlen, + unoinfoSuffix + (pathlen == 0 || path[pathlen - 1] != '/' ? 0 : 1)); + ret = lstat(unoinfo, &stats); + free(unoinfo); + + if (ret == 0) { + char * cmd = malloc( + 2 * pathlen + RTL_CONSTASCII_LENGTH("/unoinfo c++") + 1); + /*TODO: overflow */ + char const * p; + char * q; + FILE * f; + size_t n = 1000; + size_t old = 0; + if (cmd == NULL) { + fprintf(stderr, "Error: out of memory!\n"); + exit(EXIT_FAILURE); + } + p = path; + q = cmd; + while (*p != '\0') { + *q++ = '\\'; + *q++ = *p++; + } + if (p == path || p[-1] != '/') { + *q++ = '/'; + } + strcpy(q, "unoinfo c++"); + f = popen(cmd, "r"); + free(cmd); + if (f == NULL) + { + fprintf(stderr, "Error: calling unoinfo failed!\n"); + exit(EXIT_FAILURE); + } + libpath = NULL; + for (;;) { + size_t m; + libpath = realloc(libpath, n); + if (libpath == NULL) { + fprintf( + stderr, + "Error: out of memory reading unoinfo output!\n"); + exit(EXIT_FAILURE); + } + m = fread(libpath + old, 1, n - old - 1, f); + if (m != n - old - 1) { + if (ferror(f)) { + fprintf(stderr, "Error: cannot read unoinfo output!\n"); + exit(EXIT_FAILURE); + } + libpath[old + m] = '\0'; + break; + } + if (n >= SAL_MAX_SIZE / 2) { + fprintf( + stderr, + "Error: out of memory reading unoinfo output!\n"); + exit(EXIT_FAILURE); + } + old = n - 1; + n *= 2; + } + if (pclose(f) != 0) { + fprintf(stderr, "Error: executing unoinfo failed!\n"); + exit(EXIT_FAILURE); + } + free(path); + } + else + { + /* Assume an old OOo 2.x installation without unoinfo: */ + libpath = path; + } + + value = getenv( ENVVARNAME ); + + // workaround for finding wrong libsqlite3.dylib in the office installation + // For MacOS > 10.6 nss uses the system lib -> unresolved symbol _sqlite3_wal_checkpoint +#ifdef MACOSX + size = strlen( ENVVARNAME ) + strlen( "=/usr/lib:" ) + strlen( libpath ) + 1; +#else + size = strlen( ENVVARNAME ) + strlen( "=" ) + strlen( libpath ) + 1; +#endif + if ( value != NULL ) + size += strlen( PATHSEPARATOR ) + strlen( value ); + envstr = (char*) malloc( size ); + strcpy( envstr, ENVVARNAME ); +#ifdef MACOSX + strcat( envstr, "=/usr/lib:" ); +#else + strcat( envstr, "=" ); +#endif + strcat( envstr, libpath ); + free( libpath ); + if ( value != NULL ) + { + strcat( envstr, PATHSEPARATOR ); + strcat( envstr, value ); + } + /* coverity[tainted_data : FALSE] */ + putenv( envstr ); + } + else + { + fprintf( stderr, "Warning: no office installation found!\n" ); + fflush( stderr ); + } + + /* set the executable name for the application process */ + cmdname = createCommandName( argv[0] ); + argv[0] = cmdname; + + /* + * create the application process; + * if successful, execvp doesn't return to the calling process + */ + /* coverity[tainted_string] - createCommandName creates a safe string */ + execvp( cmdname, argv ); + fprintf( stderr, "Error: execvp failed!\n" ); + fflush( stderr ); + + return 0; +} + +/* + * Gets the path of a UNO installation. + * + * @return the installation path or NULL, if no installation was specified or + * found, or if an error occurred. + * Returned pointer must be released with free() + */ +char* getPath(void) +{ + char* path = cppuhelper_detail_findSofficePath(); + + if ( path == NULL ) + { + fprintf( stderr, "Warning: getting path from PATH environment " + "variable failed!\n" ); + fflush( stderr ); + } + + return path; +} + +/* + * Creates the application's executable file name. + * + * <p>The application's executable file name is the name of this executable + * prefixed by '_'.</p> + * + * @param argv0 specifies the argv[0] parameter of the main function + * + * @return the application's executable file name or NULL, if an error occurred + */ +char* createCommandName( char* argv0 ) +{ + const char* CMDPREFIX = "_"; + const char* prgname = NULL; + + char* cmdname = NULL; + char* sep = NULL; +#ifndef AIX + Dl_info dl_info; +#endif + + /* get the executable file name from argv0 */ + prgname = argv0; + +#ifndef AIX + /* + * if argv0 doesn't contain an absolute path name, try to get the absolute + * path name from dladdr; note that this only works for Solaris, not for + * Linux + */ + if ( argv0 != NULL && *argv0 != SEPARATOR && + dladdr( (void*) &createCommandName, &dl_info ) && + dl_info.dli_fname != NULL && *dl_info.dli_fname == SEPARATOR ) + { + prgname = dl_info.dli_fname; + } +#endif + + /* prefix the executable file name by '_' */ + if ( prgname != NULL ) + { + cmdname = (char*) malloc( strlen( prgname ) + strlen( CMDPREFIX ) + 1 ); + sep = strrchr( prgname, SEPARATOR ); + if ( sep != NULL ) + { + int pos = ++sep - prgname; + strncpy( cmdname, prgname, pos ); + cmdname[ pos ] = '\0'; + strcat( cmdname, CMDPREFIX ); + strcat( cmdname, sep ); + } + else + { + strcpy( cmdname, CMDPREFIX ); + strcat( cmdname, prgname ); + } + } + + return cmdname; +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/odk/source/unoapploader/win/unoapploader.c b/odk/source/unoapploader/win/unoapploader.c new file mode 100644 index 000000000..4baa3ddda --- /dev/null +++ b/odk/source/unoapploader/win/unoapploader.c @@ -0,0 +1,410 @@ +/* -*- 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 <assert.h> +#include <stdlib.h> +#include <stdio.h> +#include <string.h> +#include <process.h> + +#if !defined WIN32_LEAN_AND_MEAN +# define WIN32_LEAN_AND_MEAN +#endif +#include <windows.h> + +#include <cppuhelper/findsofficepath.h> +#include <sal/types.h> + +#define MY_SIZE(s) (sizeof (s) / sizeof *(s)) +#define MY_LENGTH(s) (MY_SIZE(s) - 1) + +static wchar_t* getPath(void); +static wchar_t* createCommandLine( wchar_t const * lpCmdLine ); +static FILE* getErrorFile( int create ); +static void writeError( const char* errstr ); +static void closeErrorFile(void); + +/* + * The main function implements a loader for applications which use UNO. + * + * <p>This code runs on the Windows platform only.</p> + * + * <p>The main function detects a UNO installation on the system and adds the + * program directory of the UNO installation to the PATH environment variable. + * After that, the application process is loaded and started, whereby the + * new process inherits the environment of the calling process, including + * the modified PATH environment variable. The application's executable name + * must be the same as the name of this executable, prefixed by '_'.</p> + * + * <p>A UNO installation can be specified by the user by setting the UNO_PATH + * environment variable to the program directory of the UNO installation. + * If no installation is specified by the user, the default installation on + * the system will be taken. The default installation is read from the + * default value of the key "Software\LibreOffice\UNO\InstallPath" from the + * root key HKEY_CURRENT_USER in the Windows Registry. If this key is missing, + * the key is read from the root key HKEY_LOCAL_MACHINE.</p> + */ +int WINAPI wWinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, + LPWSTR lpCmdLine, int nCmdShow ) +{ + (void) hInstance; /* unused */ + (void) hPrevInstance; /* unused */ + (void) nCmdShow; /* unused */ + + /* get the path of the UNO installation */ + wchar_t* path = getPath(); + + if ( path != NULL ) + { + wchar_t cmd[ + MY_LENGTH(L"\"") + MAX_PATH + + MY_LENGTH(L"\\unoinfo.exe\" c++")]; + /* hopefully does not overflow */ + cmd[0] = L'"'; + wcscpy(cmd + 1, path); + if (wcschr(cmd + 1, L'"') != NULL) { + free(path); + writeError("Error: bad characters in UNO installation path!\n"); + closeErrorFile(); + return 1; + } + size_t pathsize = wcslen(cmd); + wcscpy( + cmd + pathsize, + &L"\\unoinfo.exe\" c++"[ + pathsize == 1 || cmd[pathsize - 1] != L'\\' ? 0 : 1]); + SECURITY_ATTRIBUTES sec; + sec.nLength = sizeof (SECURITY_ATTRIBUTES); + sec.lpSecurityDescriptor = NULL; + sec.bInheritHandle = TRUE; + HANDLE stdoutRead; + HANDLE stdoutWrite; + HANDLE temp; + if (CreatePipe(&temp, &stdoutWrite, &sec, 0) == 0 || + DuplicateHandle( + GetCurrentProcess(), temp, GetCurrentProcess(), &stdoutRead, 0, + FALSE, DUPLICATE_CLOSE_SOURCE | DUPLICATE_SAME_ACCESS) == 0) + { + free(path); + writeError("Error: CreatePipe/DuplicateHandle failed!\n"); + closeErrorFile(); + return 1; + } + STARTUPINFOW startinfo; + PROCESS_INFORMATION procinfo; + memset(&startinfo, 0, sizeof(startinfo)); + startinfo.cb = sizeof(startinfo); + startinfo.lpDesktop = L""; + startinfo.dwFlags = STARTF_USESTDHANDLES; + startinfo.hStdOutput = stdoutWrite; + BOOL ret = CreateProcessW( + NULL, cmd, NULL, NULL, TRUE, 0, NULL, NULL, &startinfo, &procinfo); + if (ret != FALSE) { + // Release result of GetPath() + free(path); + + char * buf = NULL; + char * tmp; + DWORD n = 1000; + DWORD k = 0; + DWORD exitcode; + CloseHandle(stdoutWrite); + CloseHandle(procinfo.hThread); + for (;;) { + DWORD m; + tmp = realloc(buf, n); + if (tmp == NULL) { + free(buf); + writeError( + "Error: out of memory reading unoinfo output!\n"); + closeErrorFile(); + return 1; + } + buf = tmp; + if (!ReadFile(stdoutRead, buf + k, n - k, &m, NULL)) + { + DWORD err = GetLastError(); + if (err == ERROR_HANDLE_EOF || err == ERROR_BROKEN_PIPE) { + break; + } + writeError("Error: cannot read unoinfo output!\n"); + closeErrorFile(); + return 1; + } + if (m == 0) { + break; + } + k += m; + if (k >= n) { + if (n >= MAXDWORD / 2) { + writeError( + "Error: out of memory reading unoinfo output!\n"); + closeErrorFile(); + return 1; + } + n *= 2; + } + } + if ((k & 1) == 1) { + writeError("Error: bad unoinfo output!\n"); + closeErrorFile(); + return 1; + } + CloseHandle(stdoutRead); + if (!GetExitCodeProcess(procinfo.hProcess, &exitcode) || + exitcode != 0) + { + writeError("Error: executing unoinfo failed!\n"); + closeErrorFile(); + return 1; + } + path = (wchar_t*)realloc(buf, k + sizeof(wchar_t)); + if (path == NULL) + { + free(buf); + writeError( + "Error: out of memory zero-terminating unoinfo output!\n"); + closeErrorFile(); + return 1; + } + path[k / 2] = L'\0'; + } else { + if (GetLastError() != ERROR_FILE_NOT_FOUND) { + free(path); + writeError("Error: calling unoinfo failed!\n"); + closeErrorFile(); + return 1; + } + CloseHandle(stdoutRead); + CloseHandle(stdoutWrite); + } + + /* get the value of the PATH environment variable */ + const wchar_t* ENVVARNAME = L"PATH"; + const wchar_t* PATHSEPARATOR = L";"; + wchar_t* value = _wgetenv( ENVVARNAME ); + + /* + * add the UNO installation path to the PATH environment variable; + * note that this only affects the environment variable of the current + * process, the command processor's environment is not changed + */ + size_t size = wcslen( ENVVARNAME ) + wcslen( L"=" ) + wcslen( path ) + 1; + if ( value != NULL ) + size += wcslen( PATHSEPARATOR ) + wcslen( value ); + wchar_t* envstr = (wchar_t*) malloc( size*sizeof(wchar_t) ); + assert(envstr); + wcscpy( envstr, ENVVARNAME ); + wcscat( envstr, L"=" ); + wcscat( envstr, path ); + if ( value != NULL ) + { + wcscat( envstr, PATHSEPARATOR ); + wcscat( envstr, value ); + } + /* coverity[tainted_data : FALSE] */ + _wputenv( envstr ); + free( envstr ); + free( path ); + } + else + { + writeError( "Warning: no UNO installation found!\n" ); + } + + /* create the command line for the application process */ + wchar_t* cmdline = createCommandLine( lpCmdLine ); + if ( cmdline == NULL ) + { + writeError( "Error: cannot create command line!\n" ); + closeErrorFile(); + return 1; + } + + /* create the application process */ + STARTUPINFOW startup_info; + PROCESS_INFORMATION process_info; + memset( &startup_info, 0, sizeof(startup_info) ); + startup_info.cb = sizeof(startup_info); + BOOL bCreate = CreateProcessW( NULL, cmdline, NULL, NULL, FALSE, 0, NULL, NULL, + &startup_info, &process_info ); + free( cmdline ); + if ( !bCreate ) + { + writeError( "Error: cannot create process!\n" ); + closeErrorFile(); + return 1; + } + + /* close the error file */ + closeErrorFile(); + + return 0; +} + +/* + * Gets the path of a UNO installation. + * + * @return the installation path or NULL, if no installation was specified or + * found, or if an error occurred. + * Returned pointer must be released with free() + */ +wchar_t* getPath() +{ + wchar_t* path = cppuhelper_detail_findSofficePath(); + + if ( path == NULL ) + writeError( "Warning: getting path from Windows Registry failed!\n" ); + + return path; +} + +/* + * Creates the command line for the application process including the absolute + * path of the executable. + * + * <p>The application's executable file name is the name of this executable + * prefixed by '_'.</p> + * + * @param appendix specifies the command line for the application excluding + * the executable name + * + * @return the command line for the application process or NULL, if an error + * occurred + */ +wchar_t* createCommandLine( wchar_t const * appendix ) +{ + const wchar_t* CMDPREFIX = L"_"; + const wchar_t* DQUOTE = L"\""; + const wchar_t* SPACE = L" "; + + wchar_t* cmdline = NULL; + + wchar_t cmdname[ _MAX_PATH ]; + wchar_t drive[ _MAX_DRIVE ]; + wchar_t dir[ _MAX_PATH ]; + wchar_t base[ _MAX_FNAME ]; + wchar_t newbase[ _MAX_FNAME ]; + wchar_t ext[ _MAX_EXT ]; + + /* get the absolute path of the executable file */ + if ( GetModuleFileNameW( NULL, cmdname, MY_SIZE( cmdname ) ) ) + { + /* prefix the executable file name by '_' */ + _wsplitpath( cmdname, drive, dir, base, ext ); + wcscpy( newbase, CMDPREFIX ); + wcscat( newbase, base ); + _wmakepath( cmdname, drive, dir, newbase, ext ); + + /* create the command line */ + cmdline = (wchar_t*) malloc( (wcslen( DQUOTE ) + wcslen( cmdname ) + + wcslen ( DQUOTE ) + wcslen( SPACE ) + wcslen( appendix ) + 1) * sizeof(wchar_t) ); + assert(cmdline); + wcscpy( cmdline, DQUOTE ); + wcscat( cmdline, cmdname ); + wcscat( cmdline, DQUOTE ); + wcscat( cmdline, SPACE ); + wcscat( cmdline, appendix ); + } + + return cmdline; +} + +/* + * Gets the pointer to the error file. + * + * <p>The error file will only be created, if create != 0.</p> + * + * <p>The error file has the name <executable file name>-error.log and is + * created in the same directory as the executable file. If this fails, + * the error file is created in the directory designated for temporary files. + * </p> + + * @param create specifies, if the error file should be created (create != 0) + * + * @return the pointer to the open error file or NULL, if no error file is + * open or can be created + */ +FILE* getErrorFile( int create ) +{ + const wchar_t* MODE = L"w"; + const wchar_t* BASEPOSTFIX = L"-error"; + const wchar_t* EXTENSION = L".log"; + + static FILE* ferr = NULL; + + wchar_t fname[ _MAX_PATH ]; + wchar_t drive[ _MAX_DRIVE ]; + wchar_t dir[ _MAX_PATH ]; + wchar_t base[ _MAX_FNAME ]; + wchar_t newbase[ _MAX_FNAME ]; + wchar_t ext[ _MAX_EXT ]; + + if ( ferr == NULL && create ) + { + /* get the absolute path of the executable file */ + if ( GetModuleFileNameW( NULL, fname, MY_SIZE( fname ) ) ) + { + /* create error file in the directory of the executable file */ + _wsplitpath( fname, drive, dir, base, ext ); + wcscpy( newbase, base ); + wcscat( newbase, BASEPOSTFIX ); + _wmakepath( fname, drive, dir, newbase, EXTENSION ); + ferr = _wfopen( fname, MODE ); + + if ( ferr == NULL ) + { + /* create error file in the temp directory */ + GetTempPathW(MY_SIZE( fname ), fname ); + wcscat( fname, newbase ); + wcscat( fname, EXTENSION ); + ferr = _wfopen( fname, MODE ); + } + } + } + + return ferr; +} + +/* + * Writes an error message to the error file. + * + * @param errstr specifies the error message + */ +void writeError( const char* errstr ) +{ + FILE* ferr = getErrorFile( 1 ); + if ( ferr != NULL ) + { + fputs( errstr, ferr ); + fflush( ferr ); + } +} + +/* + * Closes the error file. + */ +void closeErrorFile() +{ + FILE* ferr = getErrorFile( 0 ); + if ( ferr != NULL ) + fclose( ferr ); +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/odk/source/unowinreg/win/unowinreg.cxx b/odk/source/unowinreg/win/unowinreg.cxx new file mode 100644 index 000000000..ba28809bf --- /dev/null +++ b/odk/source/unowinreg/win/unowinreg.cxx @@ -0,0 +1,170 @@ +/* -*- 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 . + */ + + +#if !defined WIN32_LEAN_AND_MEAN +# define WIN32_LEAN_AND_MEAN +#endif +#include <windows.h> + +#include <jni.h> + +extern "C" BOOL __stdcall _DllMainCRTStartup(HINSTANCE, DWORD, LPVOID) +{ + return TRUE; +} + +extern "C" JNIEXPORT jboolean JNICALL + Java_com_sun_star_lib_loader_WinRegKey_winreg_1RegOpenClassesRoot( + JNIEnv *env, jclass, jlongArray hkresult) +{ + bool ret = JNI_FALSE; + auto phkey = env->GetLongArrayElements(hkresult, nullptr); + if (RegOpenKeyEx(HKEY_CLASSES_ROOT, nullptr, 0, KEY_READ, reinterpret_cast<PHKEY>(phkey)) + == ERROR_SUCCESS) + ret = JNI_TRUE; + env->ReleaseLongArrayElements(hkresult, phkey, 0); + return ret; +} + +extern "C" JNIEXPORT jboolean JNICALL + Java_com_sun_star_lib_loader_WinRegKey_winreg_1RegOpenCurrentConfig( + JNIEnv *env, jclass, jlongArray hkresult) +{ + bool ret = JNI_FALSE; + auto phkey = env->GetLongArrayElements(hkresult, nullptr); + if (RegOpenKeyEx(HKEY_CURRENT_CONFIG, nullptr, 0, KEY_READ, reinterpret_cast<PHKEY>(phkey)) + == ERROR_SUCCESS) + ret = JNI_TRUE; + env->ReleaseLongArrayElements(hkresult, phkey, 0); + return ret; +} + +extern "C" JNIEXPORT jboolean JNICALL + Java_com_sun_star_lib_loader_WinRegKey_winreg_1RegOpenCurrentUser( + JNIEnv *env, jclass, jlongArray hkresult) +{ + bool ret = JNI_FALSE; + auto phkey = env->GetLongArrayElements(hkresult, nullptr); + if (RegOpenKeyEx(HKEY_CURRENT_USER, nullptr, 0, KEY_READ, reinterpret_cast<PHKEY>(phkey)) + == ERROR_SUCCESS) + ret = JNI_TRUE; + env->ReleaseLongArrayElements(hkresult, phkey, 0); + return ret; +} + +extern "C" JNIEXPORT jboolean JNICALL + Java_com_sun_star_lib_loader_WinRegKey_winreg_1RegOpenLocalMachine( + JNIEnv *env, jclass, jlongArray hkresult) +{ + bool ret = JNI_FALSE; + auto phkey = env->GetLongArrayElements(hkresult, nullptr); + if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, nullptr, 0, KEY_READ, reinterpret_cast<PHKEY>(phkey)) + == ERROR_SUCCESS) + ret = JNI_TRUE; + env->ReleaseLongArrayElements(hkresult, phkey, 0); + return ret; +} + +extern "C" JNIEXPORT jboolean JNICALL + Java_com_sun_star_lib_loader_WinRegKey_winreg_1RegOpenUsers( + JNIEnv *env, jclass, jlongArray hkresult) +{ + bool ret = JNI_FALSE; + auto phkey = env->GetLongArrayElements(hkresult, nullptr); + if (RegOpenKeyEx(HKEY_USERS, nullptr, 0, KEY_READ, reinterpret_cast<PHKEY>(phkey)) == ERROR_SUCCESS) + ret = JNI_TRUE; + env->ReleaseLongArrayElements(hkresult, phkey, 0); + return ret; +} + +extern "C" JNIEXPORT jboolean JNICALL + Java_com_sun_star_lib_loader_WinRegKey_winreg_1RegOpenKeyEx( + JNIEnv *env, jclass, jlong parent, jstring name, jlongArray hkresult) +{ + bool ret = JNI_FALSE; + const char *namestr = env->GetStringUTFChars(name, nullptr); + auto phkey = env->GetLongArrayElements(hkresult, nullptr); + if (RegOpenKeyEx(reinterpret_cast<HKEY>(parent), namestr, 0, KEY_READ, reinterpret_cast<PHKEY>(phkey)) + == ERROR_SUCCESS) + ret = JNI_TRUE; + env->ReleaseStringUTFChars(name, namestr); + env->ReleaseLongArrayElements(hkresult, phkey, 0); + return ret; +} + + +extern "C" JNIEXPORT jboolean JNICALL + Java_com_sun_star_lib_loader_WinRegKey_winreg_1RegCloseKey( + JNIEnv *, jclass, jlong hkey) +{ + bool ret = JNI_FALSE; + if (RegCloseKey(reinterpret_cast<HKEY>(hkey)) == ERROR_SUCCESS) + ret = JNI_TRUE; + return ret; +} + +extern "C" JNIEXPORT jboolean + JNICALL Java_com_sun_star_lib_loader_WinRegKey_winreg_1RegQueryValueEx( + JNIEnv *env, jclass, jlong hkey, jstring value, jlongArray type, + jbyteArray data, jlongArray size) +{ + bool ret = JNI_FALSE; + const char* valuestr = env->GetStringUTFChars(value, nullptr); + auto ptype = env->GetLongArrayElements(type, nullptr); + auto pdata = env->GetByteArrayElements(data, nullptr); + auto psize = env->GetLongArrayElements(size, nullptr); + if (RegQueryValueEx(reinterpret_cast<HKEY>(hkey), valuestr, nullptr, reinterpret_cast<LPDWORD>(ptype), reinterpret_cast<LPBYTE>(pdata), reinterpret_cast<LPDWORD>(psize)) + == ERROR_SUCCESS) + ret = JNI_TRUE; + env->ReleaseStringUTFChars(value, valuestr); + env->ReleaseLongArrayElements(type, ptype, 0); + env->ReleaseByteArrayElements(data, pdata, 0); + env->ReleaseLongArrayElements(size, psize, 0); + return ret; +} + +extern "C" JNIEXPORT jboolean JNICALL + Java_com_sun_star_lib_loader_WinRegKey_winreg_1RegQueryInfoKey( + JNIEnv *env, jclass, jlong hkey, jlongArray subkeys, + jlongArray maxSubkeyLen, jlongArray values, jlongArray maxValueNameLen, + jlongArray maxValueLen, jlongArray secDescriptor) +{ + bool ret = JNI_FALSE; + auto psubkeys = env->GetLongArrayElements(subkeys, nullptr); + auto pmaxSubkeyLen = env->GetLongArrayElements(maxSubkeyLen, nullptr); + auto pvalues = env->GetLongArrayElements(values, nullptr); + auto pmaxValueNameLen = env->GetLongArrayElements(maxValueNameLen, nullptr); + auto pmaxValueLen = env->GetLongArrayElements(maxValueLen, nullptr); + auto psecDescriptor = env->GetLongArrayElements(secDescriptor, nullptr); + FILETIME ft; + if (RegQueryInfoKey(reinterpret_cast<HKEY>(hkey), nullptr, nullptr, nullptr, reinterpret_cast<LPDWORD>(psubkeys), reinterpret_cast<LPDWORD>(pmaxSubkeyLen), + nullptr, reinterpret_cast<LPDWORD>(pvalues), reinterpret_cast<LPDWORD>(pmaxValueNameLen), reinterpret_cast<LPDWORD>(pmaxValueLen), + reinterpret_cast<LPDWORD>(psecDescriptor), &ft) == ERROR_SUCCESS) + ret = JNI_TRUE; + env->ReleaseLongArrayElements(subkeys, psubkeys, 0); + env->ReleaseLongArrayElements(maxSubkeyLen, pmaxSubkeyLen, 0); + env->ReleaseLongArrayElements(values, pvalues, 0); + env->ReleaseLongArrayElements(maxValueNameLen, pmaxValueNameLen, 0); + env->ReleaseLongArrayElements(maxValueLen, pmaxValueLen, 0); + env->ReleaseLongArrayElements(secDescriptor, psecDescriptor, 0); + return ret; +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |