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