src/platform_windows.cpp
changeset 0 16f27efbef98
child 8 26cc9f0228f4
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/platform_windows.cpp	Wed Jun 25 18:44:58 2014 +0200
     1.3 @@ -0,0 +1,189 @@
     1.4 +// Windows platform specifica
     1.5 +
     1.6 +#define WIN32_LEAN_AND_MEAN
     1.7 +#ifndef UNICODE
     1.8 +#define UNICODE
     1.9 +#endif
    1.10 +#define _WIN32_WINNT 0x0600
    1.11 +
    1.12 +#include <windows.h>
    1.13 +#include <assert.h>
    1.14 +#include <string>
    1.15 +#include <stdexcept>
    1.16 +#include "platform_windows.h"
    1.17 +
    1.18 +#ifndef WC_ERR_INVALID_CHARS
    1.19 +#define WC_ERR_INVALID_CHARS      0x00000080  // error for invalid chars
    1.20 +#endif
    1.21 +
    1.22 +using namespace std;
    1.23 +
    1.24 +static string utf8_string(wstring wstr) {
    1.25 +    string result;
    1.26 +
    1.27 +    if (wstr.length()) {
    1.28 +        int size = WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS,
    1.29 +                wstr.c_str(), -1, NULL, 0, NULL, NULL);
    1.30 +        assert(size);
    1.31 +        if (size) {
    1.32 +            char *buf = new char[size];
    1.33 +            WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, wstr.c_str(),
    1.34 +                    -1, buf, size, NULL, NULL);
    1.35 +            result = buf;
    1.36 +            delete[] buf;
    1.37 +        } else
    1.38 +            throw out_of_range("input wstring is not valid"
    1.39 +                    " while converting UTF-16 to UTF-8.");
    1.40 +    }
    1.41 +
    1.42 +    return result;
    1.43 +}
    1.44 +
    1.45 +static wstring utf16_string(string str) {
    1.46 +    wstring result;
    1.47 +
    1.48 +    if (str.length()) {
    1.49 +        int size = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS,
    1.50 +                str.c_str(), -1, NULL, 0);
    1.51 +        assert(size);
    1.52 +        if (size) {
    1.53 +            wchar_t * buf = new wchar_t[size];
    1.54 +            MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, str.c_str(), -1,
    1.55 +                    buf, size);
    1.56 +            result = buf;
    1.57 +            delete[] buf;
    1.58 +        } else
    1.59 +            throw out_of_range("input string is not valid"
    1.60 +                    " while converting UTF-8 to UTF-16.");
    1.61 +    }
    1.62 +
    1.63 +    return result;
    1.64 +}
    1.65 +
    1.66 +static bool readRegistryString(
    1.67 +        HKEY hKey, LPCTSTR lpSubKey, LPCTSTR lpValueName, LPTSTR lpResult,
    1.68 +        DWORD dwSize, LPCTSTR lpDefault
    1.69 +    )
    1.70 +{
    1.71 +    assert(lpResult);
    1.72 +
    1.73 +	HKEY theKey;
    1.74 +	DWORD type;
    1.75 +	DWORD bytesCopied = dwSize;
    1.76 +	HRESULT result;
    1.77 +
    1.78 +	result = RegOpenKeyEx(hKey, lpSubKey, 0, KEY_READ, &theKey);
    1.79 +	if (result != ERROR_SUCCESS) {
    1.80 +		if (lpDefault) {
    1.81 +			wcsncpy_s(lpResult, dwSize, lpDefault, _TRUNCATE);
    1.82 +			return true;
    1.83 +		}
    1.84 +		else
    1.85 +			return false;
    1.86 +	}
    1.87 +
    1.88 +	result = RegQueryValueEx(theKey, lpValueName, NULL, &type,
    1.89 +            (LPBYTE) lpResult, &bytesCopied);
    1.90 +    if (result != ERROR_SUCCESS || (type != REG_EXPAND_SZ && type != REG_SZ)) {
    1.91 +		if (lpDefault) {
    1.92 +			wcsncpy_s(lpResult, dwSize, lpDefault, _TRUNCATE);
    1.93 +			RegCloseKey(theKey);
    1.94 +			return true;
    1.95 +		}
    1.96 +		else {
    1.97 +			RegCloseKey(theKey);
    1.98 +			return false;
    1.99 +		}
   1.100 +	}
   1.101 +
   1.102 +	RegCloseKey(theKey);
   1.103 +	return true;
   1.104 +}
   1.105 +
   1.106 +static const DWORD PATH_BUF_SIZE = 32768;
   1.107 +
   1.108 +static inline string managementPath(const char *file_path, const char *file_name)
   1.109 +{
   1.110 +    string path;
   1.111 +	static TCHAR tPath[PATH_BUF_SIZE];
   1.112 +
   1.113 +    DWORD length = ExpandEnvironmentStringsW(utf16_string(file_path).c_str(),
   1.114 +            tPath, PATH_BUF_SIZE);
   1.115 +	assert(length);
   1.116 +    if (length == 0)
   1.117 +        throw bad_alloc();
   1.118 +
   1.119 +	CreateDirectory(tPath, NULL);
   1.120 +	DWORD error = GetLastError();
   1.121 +	assert(error == 0 || error == ERROR_ALREADY_EXISTS);
   1.122 +
   1.123 +	path = utf8_string(tPath);
   1.124 +	path += "\\";
   1.125 +	path += file_name;
   1.126 +
   1.127 +	return path;
   1.128 +}
   1.129 +
   1.130 +extern "C" {
   1.131 +
   1.132 +void *dlopen(const char *filename, int flag) {
   1.133 +	static TCHAR path[PATH_BUF_SIZE];
   1.134 +
   1.135 +    assert(filename);
   1.136 +	assert(flag == RTLD_LAZY); // only lazy binding is implemented
   1.137 +
   1.138 +    bool result = readRegistryString(HKEY_LOCAL_MACHINE,
   1.139 +            TEXT("SOFTWARE\\GNU\\GnuPG"), TEXT("Install Directory"), path,
   1.140 +            PATH_BUF_SIZE, NULL);
   1.141 +	assert(result);
   1.142 +	if (!result)
   1.143 +		return NULL;
   1.144 +
   1.145 +    SetDllDirectory(TEXT(""));
   1.146 +    BOOL _result = SetDllDirectory(path);
   1.147 +    assert(_result != 0);
   1.148 +    if (_result == 0)
   1.149 +        return NULL;
   1.150 +
   1.151 +	HMODULE module = LoadLibrary(utf16_string(filename).c_str());
   1.152 +    SetDllDirectory(NULL);
   1.153 +	if (module == NULL)
   1.154 +		return NULL;
   1.155 +	else
   1.156 +		return (void *) module;
   1.157 +}
   1.158 +
   1.159 +int dlclose(void *handle) {
   1.160 +	if (FreeLibrary((HMODULE) handle))
   1.161 +		return 0;
   1.162 +	else
   1.163 +		return 1;
   1.164 +}
   1.165 +
   1.166 +void *dlsym(void *handle, const char *symbol) {
   1.167 +	return (void *) (intptr_t) GetProcAddress((HMODULE) handle, symbol);
   1.168 +}
   1.169 +
   1.170 +const char *windoze_local_db() {
   1.171 +	static string path;
   1.172 +	if (path.length() == 0)
   1.173 +        path = managementPath("%LOCALAPPDATA%\\pEp", "management.db");
   1.174 +    return path.c_str();
   1.175 +}
   1.176 +
   1.177 +const char *windoze_system_db() {
   1.178 +	static string path;
   1.179 +	if (path.length() == 0)
   1.180 +		path = managementPath("%ALLUSERSPROFILE%\\pEp", "system.db");
   1.181 +    return path.c_str();
   1.182 +}
   1.183 +
   1.184 +const char *gpg_conf()
   1.185 +{
   1.186 +    static string path;
   1.187 +    if (path.length() == 0)
   1.188 +        path = managementPath("%APPDATA%\\gnupg", "gpg.conf");
   1.189 +    return path.c_str();
   1.190 +}
   1.191 +
   1.192 +} // "C"