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@0
|
17 |
|
vb@0
|
18 |
#ifndef WC_ERR_INVALID_CHARS
|
vb@0
|
19 |
#define WC_ERR_INVALID_CHARS 0x00000080 // error for invalid chars
|
vb@0
|
20 |
#endif
|
vb@0
|
21 |
|
vb@0
|
22 |
using namespace std;
|
vb@0
|
23 |
|
vb@0
|
24 |
static string utf8_string(wstring wstr) {
|
vb@0
|
25 |
string result;
|
vb@0
|
26 |
|
vb@0
|
27 |
if (wstr.length()) {
|
vb@0
|
28 |
int size = WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS,
|
vb@0
|
29 |
wstr.c_str(), -1, NULL, 0, NULL, NULL);
|
vb@0
|
30 |
assert(size);
|
vb@0
|
31 |
if (size) {
|
vb@0
|
32 |
char *buf = new char[size];
|
vb@0
|
33 |
WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, wstr.c_str(),
|
vb@0
|
34 |
-1, buf, size, NULL, NULL);
|
vb@0
|
35 |
result = buf;
|
vb@0
|
36 |
delete[] buf;
|
vb@0
|
37 |
} else
|
vb@0
|
38 |
throw out_of_range("input wstring is not valid"
|
vb@0
|
39 |
" while converting UTF-16 to UTF-8.");
|
vb@0
|
40 |
}
|
vb@0
|
41 |
|
vb@0
|
42 |
return result;
|
vb@0
|
43 |
}
|
vb@0
|
44 |
|
vb@0
|
45 |
static wstring utf16_string(string str) {
|
vb@0
|
46 |
wstring result;
|
vb@0
|
47 |
|
vb@0
|
48 |
if (str.length()) {
|
vb@0
|
49 |
int size = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS,
|
vb@0
|
50 |
str.c_str(), -1, NULL, 0);
|
vb@0
|
51 |
assert(size);
|
vb@0
|
52 |
if (size) {
|
vb@0
|
53 |
wchar_t * buf = new wchar_t[size];
|
vb@0
|
54 |
MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, str.c_str(), -1,
|
vb@0
|
55 |
buf, size);
|
vb@0
|
56 |
result = buf;
|
vb@0
|
57 |
delete[] buf;
|
vb@0
|
58 |
} else
|
vb@0
|
59 |
throw out_of_range("input string is not valid"
|
vb@0
|
60 |
" while converting UTF-8 to UTF-16.");
|
vb@0
|
61 |
}
|
vb@0
|
62 |
|
vb@0
|
63 |
return result;
|
vb@0
|
64 |
}
|
vb@0
|
65 |
|
vb@0
|
66 |
static bool readRegistryString(
|
vb@0
|
67 |
HKEY hKey, LPCTSTR lpSubKey, LPCTSTR lpValueName, LPTSTR lpResult,
|
vb@0
|
68 |
DWORD dwSize, LPCTSTR lpDefault
|
vb@0
|
69 |
)
|
vb@0
|
70 |
{
|
vb@0
|
71 |
assert(lpResult);
|
vb@0
|
72 |
|
vb@0
|
73 |
HKEY theKey;
|
vb@0
|
74 |
DWORD type;
|
vb@0
|
75 |
DWORD bytesCopied = dwSize;
|
vb@0
|
76 |
HRESULT result;
|
vb@0
|
77 |
|
vb@0
|
78 |
result = RegOpenKeyEx(hKey, lpSubKey, 0, KEY_READ, &theKey);
|
vb@0
|
79 |
if (result != ERROR_SUCCESS) {
|
vb@0
|
80 |
if (lpDefault) {
|
vb@0
|
81 |
wcsncpy_s(lpResult, dwSize, lpDefault, _TRUNCATE);
|
vb@0
|
82 |
return true;
|
vb@0
|
83 |
}
|
vb@0
|
84 |
else
|
vb@0
|
85 |
return false;
|
vb@0
|
86 |
}
|
vb@0
|
87 |
|
vb@0
|
88 |
result = RegQueryValueEx(theKey, lpValueName, NULL, &type,
|
vb@0
|
89 |
(LPBYTE) lpResult, &bytesCopied);
|
vb@0
|
90 |
if (result != ERROR_SUCCESS || (type != REG_EXPAND_SZ && type != REG_SZ)) {
|
vb@0
|
91 |
if (lpDefault) {
|
vb@0
|
92 |
wcsncpy_s(lpResult, dwSize, lpDefault, _TRUNCATE);
|
vb@0
|
93 |
RegCloseKey(theKey);
|
vb@0
|
94 |
return true;
|
vb@0
|
95 |
}
|
vb@0
|
96 |
else {
|
vb@0
|
97 |
RegCloseKey(theKey);
|
vb@0
|
98 |
return false;
|
vb@0
|
99 |
}
|
vb@0
|
100 |
}
|
vb@0
|
101 |
|
vb@0
|
102 |
RegCloseKey(theKey);
|
vb@0
|
103 |
return true;
|
vb@0
|
104 |
}
|
vb@0
|
105 |
|
vb@0
|
106 |
static const DWORD PATH_BUF_SIZE = 32768;
|
vb@0
|
107 |
|
vb@0
|
108 |
static inline string managementPath(const char *file_path, const char *file_name)
|
vb@0
|
109 |
{
|
vb@0
|
110 |
string path;
|
vb@0
|
111 |
static TCHAR tPath[PATH_BUF_SIZE];
|
vb@0
|
112 |
|
vb@0
|
113 |
DWORD length = ExpandEnvironmentStringsW(utf16_string(file_path).c_str(),
|
vb@0
|
114 |
tPath, PATH_BUF_SIZE);
|
vb@0
|
115 |
assert(length);
|
vb@0
|
116 |
if (length == 0)
|
vb@8
|
117 |
throw bad_alloc(); // BUG: there are other errors possible beside out of memory
|
vb@0
|
118 |
|
vb@0
|
119 |
CreateDirectory(tPath, NULL);
|
vb@0
|
120 |
DWORD error = GetLastError();
|
vb@0
|
121 |
assert(error == 0 || error == ERROR_ALREADY_EXISTS);
|
vb@0
|
122 |
|
vb@0
|
123 |
path = utf8_string(tPath);
|
vb@0
|
124 |
path += "\\";
|
vb@0
|
125 |
path += file_name;
|
vb@0
|
126 |
|
vb@0
|
127 |
return path;
|
vb@0
|
128 |
}
|
vb@0
|
129 |
|
vb@0
|
130 |
extern "C" {
|
vb@0
|
131 |
|
vb@0
|
132 |
void *dlopen(const char *filename, int flag) {
|
vb@0
|
133 |
static TCHAR path[PATH_BUF_SIZE];
|
vb@0
|
134 |
|
vb@0
|
135 |
assert(filename);
|
vb@0
|
136 |
assert(flag == RTLD_LAZY); // only lazy binding is implemented
|
vb@0
|
137 |
|
vb@0
|
138 |
bool result = readRegistryString(HKEY_LOCAL_MACHINE,
|
vb@0
|
139 |
TEXT("SOFTWARE\\GNU\\GnuPG"), TEXT("Install Directory"), path,
|
vb@0
|
140 |
PATH_BUF_SIZE, NULL);
|
vb@0
|
141 |
assert(result);
|
vb@0
|
142 |
if (!result)
|
vb@0
|
143 |
return NULL;
|
vb@0
|
144 |
|
vb@0
|
145 |
SetDllDirectory(TEXT(""));
|
vb@0
|
146 |
BOOL _result = SetDllDirectory(path);
|
vb@0
|
147 |
assert(_result != 0);
|
vb@0
|
148 |
if (_result == 0)
|
vb@0
|
149 |
return NULL;
|
vb@0
|
150 |
|
vb@0
|
151 |
HMODULE module = LoadLibrary(utf16_string(filename).c_str());
|
vb@0
|
152 |
SetDllDirectory(NULL);
|
vb@0
|
153 |
if (module == NULL)
|
vb@0
|
154 |
return NULL;
|
vb@0
|
155 |
else
|
vb@0
|
156 |
return (void *) module;
|
vb@0
|
157 |
}
|
vb@0
|
158 |
|
vb@0
|
159 |
int dlclose(void *handle) {
|
vb@0
|
160 |
if (FreeLibrary((HMODULE) handle))
|
vb@0
|
161 |
return 0;
|
vb@0
|
162 |
else
|
vb@0
|
163 |
return 1;
|
vb@0
|
164 |
}
|
vb@0
|
165 |
|
vb@0
|
166 |
void *dlsym(void *handle, const char *symbol) {
|
vb@0
|
167 |
return (void *) (intptr_t) GetProcAddress((HMODULE) handle, symbol);
|
vb@0
|
168 |
}
|
vb@0
|
169 |
|
vb@55
|
170 |
const char *windoze_local_db(void) {
|
vb@0
|
171 |
static string path;
|
vb@0
|
172 |
if (path.length() == 0)
|
vb@0
|
173 |
path = managementPath("%LOCALAPPDATA%\\pEp", "management.db");
|
vb@0
|
174 |
return path.c_str();
|
vb@0
|
175 |
}
|
vb@0
|
176 |
|
vb@55
|
177 |
const char *windoze_system_db(void) {
|
vb@0
|
178 |
static string path;
|
vb@0
|
179 |
if (path.length() == 0)
|
vb@0
|
180 |
path = managementPath("%ALLUSERSPROFILE%\\pEp", "system.db");
|
vb@0
|
181 |
return path.c_str();
|
vb@0
|
182 |
}
|
vb@0
|
183 |
|
vb@55
|
184 |
const char *gpg_conf(void)
|
vb@0
|
185 |
{
|
vb@0
|
186 |
static string path;
|
vb@0
|
187 |
if (path.length() == 0)
|
vb@0
|
188 |
path = managementPath("%APPDATA%\\gnupg", "gpg.conf");
|
vb@0
|
189 |
return path.c_str();
|
vb@0
|
190 |
}
|
vb@0
|
191 |
|
vb@54
|
192 |
long random(void)
|
vb@54
|
193 |
{
|
vb@54
|
194 |
unsigned int r;
|
vb@54
|
195 |
errno_t e;
|
vb@54
|
196 |
|
vb@54
|
197 |
assert(sizeof(unsigned int) == sizeof(long)); // this is Windoze
|
vb@54
|
198 |
|
vb@54
|
199 |
do {
|
vb@54
|
200 |
e = rand_s(&r);
|
vb@54
|
201 |
} while (e);
|
vb@54
|
202 |
|
vb@54
|
203 |
return (long) (r & ((1<<31)-1));
|
vb@54
|
204 |
}
|
vb@54
|
205 |
|
vb@82
|
206 |
char *strndup(const char *s1, size_t n)
|
vb@82
|
207 |
{
|
vb@107
|
208 |
char *str = calloc(n + 1, 1);
|
vb@82
|
209 |
if (str == NULL)
|
vb@82
|
210 |
return NULL;
|
vb@82
|
211 |
|
vb@82
|
212 |
strncpy(str, s1, n);
|
vb@82
|
213 |
return str;
|
vb@82
|
214 |
}
|
vb@80
|
215 |
|
vb@0
|
216 |
} // "C"
|
vb@59
|
217 |
|