src/platform_windows.cpp
author Roker <roker@pep-project.org>
Tue, 20 Sep 2016 15:54:57 +0200
branchroker-linux
changeset 1168 4d4e3ecbaf8b
parent 920 4b31d5d0e4d0
child 948 2c689b7707a9
permissions -rw-r--r--
merge "default" into my branch
vb@0
     1
// Windows platform specifica
vb@0
     2
vb@0
     3
#define WIN32_LEAN_AND_MEAN
vb@0
     4
#ifndef UNICODE
vb@0
     5
#define UNICODE
vb@0
     6
#endif
vb@0
     7
#define _WIN32_WINNT 0x0600
vb@0
     8
vb@0
     9
#include <windows.h>
vb@54
    10
#define _CRT_RAND_S
vb@54
    11
#include <stdlib.h>
vb@0
    12
#include <assert.h>
vb@80
    13
#include <string.h>
vb@0
    14
#include <string>
vb@0
    15
#include <stdexcept>
vb@0
    16
#include "platform_windows.h"
vb@130
    17
#include <fcntl.h>
vb@130
    18
#include <sys\stat.h>
vb@0
    19
vb@0
    20
#ifndef WC_ERR_INVALID_CHARS
vb@0
    21
#define WC_ERR_INVALID_CHARS      0x00000080  // error for invalid chars
vb@0
    22
#endif
vb@0
    23
vb@0
    24
using namespace std;
vb@0
    25
vb@0
    26
static string utf8_string(wstring wstr) {
vb@0
    27
    string result;
vb@0
    28
vb@0
    29
    if (wstr.length()) {
vb@0
    30
        int size = WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS,
vb@0
    31
                wstr.c_str(), -1, NULL, 0, NULL, NULL);
vb@0
    32
        assert(size);
vb@0
    33
        if (size) {
vb@0
    34
            char *buf = new char[size];
vb@0
    35
            WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, wstr.c_str(),
vb@0
    36
                    -1, buf, size, NULL, NULL);
vb@0
    37
            result = buf;
vb@0
    38
            delete[] buf;
vb@0
    39
        } else
vb@0
    40
            throw out_of_range("input wstring is not valid"
vb@0
    41
                    " while converting UTF-16 to UTF-8.");
vb@0
    42
    }
vb@0
    43
vb@0
    44
    return result;
vb@0
    45
}
vb@0
    46
vb@0
    47
static wstring utf16_string(string str) {
vb@0
    48
    wstring result;
vb@0
    49
vb@0
    50
    if (str.length()) {
vb@0
    51
        int size = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS,
vb@0
    52
                str.c_str(), -1, NULL, 0);
vb@0
    53
        assert(size);
vb@0
    54
        if (size) {
vb@0
    55
            wchar_t * buf = new wchar_t[size];
vb@0
    56
            MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, str.c_str(), -1,
vb@0
    57
                    buf, size);
vb@0
    58
            result = buf;
vb@0
    59
            delete[] buf;
vb@0
    60
        } else
vb@0
    61
            throw out_of_range("input string is not valid"
vb@0
    62
                    " while converting UTF-8 to UTF-16.");
vb@0
    63
    }
vb@0
    64
vb@0
    65
    return result;
vb@0
    66
}
vb@0
    67
vb@0
    68
static bool readRegistryString(
vb@0
    69
        HKEY hKey, LPCTSTR lpSubKey, LPCTSTR lpValueName, LPTSTR lpResult,
vb@0
    70
        DWORD dwSize, LPCTSTR lpDefault
vb@0
    71
    )
vb@0
    72
{
vb@0
    73
    assert(lpResult);
vb@0
    74
vb@0
    75
	HKEY theKey;
vb@0
    76
	DWORD type;
vb@0
    77
	DWORD bytesCopied = dwSize;
vb@0
    78
	HRESULT result;
vb@0
    79
vb@0
    80
	result = RegOpenKeyEx(hKey, lpSubKey, 0, KEY_READ, &theKey);
vb@0
    81
	if (result != ERROR_SUCCESS) {
vb@0
    82
		if (lpDefault) {
vb@0
    83
			wcsncpy_s(lpResult, dwSize, lpDefault, _TRUNCATE);
vb@0
    84
			return true;
vb@0
    85
		}
vb@0
    86
		else
vb@0
    87
			return false;
vb@0
    88
	}
vb@0
    89
vb@0
    90
	result = RegQueryValueEx(theKey, lpValueName, NULL, &type,
vb@0
    91
            (LPBYTE) lpResult, &bytesCopied);
vb@0
    92
    if (result != ERROR_SUCCESS || (type != REG_EXPAND_SZ && type != REG_SZ)) {
vb@0
    93
		if (lpDefault) {
vb@0
    94
			wcsncpy_s(lpResult, dwSize, lpDefault, _TRUNCATE);
vb@0
    95
			RegCloseKey(theKey);
vb@0
    96
			return true;
vb@0
    97
		}
vb@0
    98
		else {
vb@0
    99
			RegCloseKey(theKey);
vb@0
   100
			return false;
vb@0
   101
		}
vb@0
   102
	}
vb@0
   103
vb@0
   104
	RegCloseKey(theKey);
vb@0
   105
	return true;
vb@0
   106
}
vb@0
   107
vb@0
   108
static const DWORD PATH_BUF_SIZE = 32768;
vb@0
   109
vb@0
   110
static inline string managementPath(const char *file_path, const char *file_name)
vb@0
   111
{
vb@0
   112
    string path;
vb@0
   113
	static TCHAR tPath[PATH_BUF_SIZE];
vb@0
   114
vb@0
   115
    DWORD length = ExpandEnvironmentStringsW(utf16_string(file_path).c_str(),
vb@0
   116
            tPath, PATH_BUF_SIZE);
vb@0
   117
	assert(length);
vb@0
   118
    if (length == 0)
vb@8
   119
        throw bad_alloc(); // BUG: there are other errors possible beside out of memory
vb@0
   120
vb@0
   121
	CreateDirectory(tPath, NULL);
vb@0
   122
	DWORD error = GetLastError();
vb@0
   123
vb@0
   124
	path = utf8_string(tPath);
vb@0
   125
	path += "\\";
vb@0
   126
	path += file_name;
vb@0
   127
vb@0
   128
	return path;
vb@0
   129
}
vb@0
   130
vb@0
   131
extern "C" {
vb@0
   132
vb@0
   133
void *dlopen(const char *filename, int flag) {
vb@0
   134
	static TCHAR path[PATH_BUF_SIZE];
vb@0
   135
vb@0
   136
    assert(filename);
vb@0
   137
	assert(flag == RTLD_LAZY); // only lazy binding is implemented
vb@0
   138
vb@0
   139
    bool result = readRegistryString(HKEY_LOCAL_MACHINE,
vb@0
   140
            TEXT("SOFTWARE\\GNU\\GnuPG"), TEXT("Install Directory"), path,
vb@0
   141
            PATH_BUF_SIZE, NULL);
vb@0
   142
	assert(result);
vb@0
   143
	if (!result)
vb@0
   144
		return NULL;
vb@0
   145
vb@0
   146
    SetDllDirectory(TEXT(""));
vb@0
   147
    BOOL _result = SetDllDirectory(path);
vb@0
   148
    assert(_result != 0);
vb@0
   149
    if (_result == 0)
vb@0
   150
        return NULL;
vb@0
   151
vb@0
   152
	HMODULE module = LoadLibrary(utf16_string(filename).c_str());
vb@0
   153
    SetDllDirectory(NULL);
vb@0
   154
	if (module == NULL)
vb@0
   155
		return NULL;
vb@0
   156
	else
vb@0
   157
		return (void *) module;
vb@0
   158
}
vb@0
   159
vb@0
   160
int dlclose(void *handle) {
vb@0
   161
	if (FreeLibrary((HMODULE) handle))
vb@0
   162
		return 0;
vb@0
   163
	else
vb@0
   164
		return 1;
vb@0
   165
}
vb@0
   166
vb@0
   167
void *dlsym(void *handle, const char *symbol) {
vb@0
   168
	return (void *) (intptr_t) GetProcAddress((HMODULE) handle, symbol);
vb@0
   169
}
vb@0
   170
vb@55
   171
const char *windoze_local_db(void) {
vb@0
   172
	static string path;
vb@0
   173
	if (path.length() == 0)
vb@0
   174
        path = managementPath("%LOCALAPPDATA%\\pEp", "management.db");
vb@0
   175
    return path.c_str();
vb@0
   176
}
vb@0
   177
vb@55
   178
const char *windoze_system_db(void) {
vb@0
   179
	static string path;
vb@0
   180
	if (path.length() == 0)
vb@0
   181
		path = managementPath("%ALLUSERSPROFILE%\\pEp", "system.db");
vb@0
   182
    return path.c_str();
vb@0
   183
}
vb@0
   184
vb@55
   185
const char *gpg_conf(void)
vb@0
   186
{
vb@0
   187
    static string path;
vb@0
   188
    if (path.length() == 0)
vb@0
   189
        path = managementPath("%APPDATA%\\gnupg", "gpg.conf");
vb@0
   190
    return path.c_str();
vb@0
   191
}
vb@0
   192
krista@763
   193
const char *gpg_agent_conf(void)
krista@763
   194
{
krista@763
   195
    static string agent_path;
krista@763
   196
    if (agent_path.length() == 0)
krista@763
   197
        agent_path = managementPath("%APPDATA%\\gnupg", "gpg-agent.conf");
krista@763
   198
    return agent_path.c_str();
krista@763
   199
}
krista@763
   200
krista@763
   201
vb@54
   202
long random(void)
vb@54
   203
{
vb@54
   204
    unsigned int r;
vb@54
   205
    errno_t e;
vb@54
   206
vb@54
   207
    assert(sizeof(unsigned int) == sizeof(long)); // this is Windoze
vb@54
   208
vb@54
   209
    do {
vb@54
   210
        e = rand_s(&r);
vb@54
   211
    } while (e);
vb@54
   212
vb@130
   213
    return (long) (r & ((1U<<31)-1));
vb@54
   214
}
vb@54
   215
vb@82
   216
char *strndup(const char *s1, size_t n)
vb@82
   217
{
vb@130
   218
    char *str = (char *) calloc(n + 1, 1);
vb@82
   219
    if (str == NULL)
vb@82
   220
        return NULL;
vb@82
   221
vb@82
   222
    strncpy(str, s1, n);
vb@82
   223
    return str;
vb@82
   224
}
vb@80
   225
vb@282
   226
char *stpcpy(char *dst, const char *src)
vb@282
   227
{
vb@282
   228
    for (;; ++dst, ++src) {
vb@282
   229
        *dst = *src;
vb@282
   230
        if (*dst == 0)
vb@282
   231
            break;
vb@282
   232
    }
vb@282
   233
    return dst;
vb@282
   234
}
vb@282
   235
krista@920
   236
size_t strlcpy(char* dst, const	char* src, size_t size) {
krista@920
   237
    size_t retval = strlen(src);
krista@920
   238
    size_t size_to_copy = (retval < size ? retval : size - 1);
krista@920
   239
    
krista@920
   240
    // strlcpy doc says src and dst not allowed to overlap, as
krista@920
   241
    // it's undefined. So this is acceptable:
krista@920
   242
    memcpy((void*)dst, (void*)src, size_to_copy); // no defined error return, but strcpy doesn't either
krista@920
   243
    dst[size_to_copy] = '\0';
krista@920
   244
    return retval;
krista@920
   245
}
krista@920
   246
size_t strlcat(char* dst, const	char* src, size_t size) {
krista@920
   247
    size_t start_len = strnlen(dst, size);
krista@920
   248
    if (start_len == size)
krista@920
   249
        return size; // no copy, no null termination in size bytes, according to spec
krista@920
   250
    
krista@920
   251
    size_t add_len = strlen(src);
krista@920
   252
    size_t retval = start_len + add_len;
krista@920
   253
    size_t size_to_copy = (retval < size ? add_len : (size - start_len) - 1);
krista@920
   254
    
krista@920
   255
    // strlcat doc says src and dst not allowed to overlap, as
krista@920
   256
    // it's undefined. So this is acceptable:
krista@920
   257
    memcpy((void*)(dst + start_len), (void*)src, size_to_copy); // no defined error return, but strcpy doesn't either
krista@920
   258
    dst[start_len + size_to_copy] = '\0';
krista@920
   259
    return retval;
krista@920
   260
}
krista@920
   261
vb@140
   262
int mkstemp(char *templ)
vb@140
   263
{
vb@140
   264
    char *pathname = _mktemp(templ);
vb@140
   265
    if (errno)
vb@140
   266
        return -1;
vb@140
   267
    return _open(pathname, _O_RDWR | _O_CREAT | _O_EXCL, _S_IREAD | _S_IWRITE);
vb@140
   268
}
vb@130
   269
vb@0
   270
} // "C"