aboutsummaryrefslogtreecommitdiff
path: root/vmime-master/src/vmime/platforms/windows/windowsHandler.cpp
blob: 551f6726dab97783f6a09d9fb39c7abd69ac1b40 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
//
// VMime library (http://www.vmime.org)
// Copyright (C) 2002 Vincent Richard <vincent@vmime.org>
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License as
// published by the Free Software Foundation; either version 3 of
// the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
//
// Linking this library statically or dynamically with other modules is making
// a combined work based on this library.  Thus, the terms and conditions of
// the GNU General Public License cover the whole combination.
//

#include "vmime/config.hpp"


#if VMIME_PLATFORM_IS_WINDOWS


#include "vmime/platforms/windows/windowsHandler.hpp"

#include "vmime/platforms/windows/windowsCriticalSection.hpp"

#include "vmime/utility/stringUtils.hpp"

#include <time.h>
#include <locale.h>
#include <process.h>
#include <winsock2.h> // for WSAStartup()
#include <windows.h>  // for winnls.h
#include <ws2tcpip.h>
#include <wincrypt.h>

#if VMIME_HAVE_MLANG
#   include <mlang.h>
#endif


namespace vmime {
namespace platforms {
namespace windows {


windowsHandler::windowsHandler() {

	WSAData wsaData;
	WSAStartup(MAKEWORD(1, 1), &wsaData);

#if VMIME_HAVE_MESSAGING_FEATURES
	m_socketFactory = make_shared <windowsSocketFactory>();
#endif
#if VMIME_HAVE_FILESYSTEM_FEATURES
	m_fileSysFactory = make_shared <windowsFileSystemFactory>();
#endif

}


windowsHandler::~windowsHandler() {

	WSACleanup();
}


unsigned long windowsHandler::getUnixTime() const {

	return static_cast <unsigned long>(::time(NULL));
}


const vmime::datetime windowsHandler::getCurrentLocalTime() const {

	const time_t t(::time(NULL));

	// Get the local time
#if VMIME_HAVE_LOCALTIME_S
	tm local;
	::localtime_s(&local, &t);
#elif VMIME_HAVE_LOCALTIME_R
	tm local;
	::localtime_r(&t, &local);
#else
	tm local = *::localtime(&t);  // WARNING: this is not thread-safe!
#endif

	// Get the UTC time
#if VMIME_HAVE_GMTIME_S
	tm gmt;
	::gmtime_s(&gmt, &t);
#elif VMIME_HAVE_GMTIME_R
	tm gmt;
	::gmtime_r(&t, &gmt);
#else
	tm gmt = *::gmtime(&t);  // WARNING: this is not thread-safe!
#endif

	// "A negative value for tm_isdst causes mktime() to attempt
	//  to determine whether Daylight Saving Time is in effect
	//  for the specified time."
	local.tm_isdst = -1;
	gmt.tm_isdst = -1;

	// Calculate the difference (in seconds)
	const int diff = (const int)(::mktime(&local) - ::mktime(&gmt));

	// Return the date
	return vmime::datetime(
		local.tm_year + 1900, local.tm_mon + 1, local.tm_mday,
		local.tm_hour, local.tm_min, local.tm_sec, diff / 60  // minutes needed
	);
}


const vmime::charset windowsHandler::getLocalCharset() const {

#if VMIME_HAVE_MLANG

	char szCharset[256];

	CoInitialize(NULL);
	{
		IMultiLanguage* pMultiLanguage;

		CoCreateInstance(
			CLSID_CMultiLanguage,
			NULL,
			CLSCTX_INPROC_SERVER,
			IID_IMultiLanguage,
			(void**) &pMultiLanguage
		);

		UINT codePage = GetACP();
		MIMECPINFO cpInfo;
		pMultiLanguage->GetCodePageInfo(codePage, &cpInfo);

		int nLengthW = lstrlenW(cpInfo.wszBodyCharset) + 1;

		WideCharToMultiByte(
			codePage, 0, cpInfo.wszBodyCharset, nLengthW,
			szCharset, sizeof(szCharset), NULL, NULL
		);

		pMultiLanguage->Release();
	}
	CoUninitialize();

	return vmime::charset(szCharset);

#else // VMIME_HAVE_MLANG

	vmime::string ch = vmime::charsets::ISO8859_1; // default

	switch (GetACP()) {

		case 437: ch = vmime::charsets::CP_437; break;
		case 737: ch = vmime::charsets::CP_737; break;
		case 775: ch = vmime::charsets::CP_775; break;
		case 850: ch = vmime::charsets::CP_850; break;
		case 852: ch = vmime::charsets::CP_852; break;
		case 853: ch = vmime::charsets::CP_853; break;
		case 855: ch = vmime::charsets::CP_855; break;
		case 857: ch = vmime::charsets::CP_857; break;
		case 858: ch = vmime::charsets::CP_858; break;
		case 860: ch = vmime::charsets::CP_860; break;
		case 861: ch = vmime::charsets::CP_861; break;
		case 862: ch = vmime::charsets::CP_862; break;
		case 863: ch = vmime::charsets::CP_863; break;
		case 864: ch = vmime::charsets::CP_864; break;
		case 865: ch = vmime::charsets::CP_865; break;
		case 866: ch = vmime::charsets::CP_866; break;
		case 869: ch = vmime::charsets::CP_869; break;
		case 874: ch = vmime::charsets::CP_874; break;

		case 1125: ch = vmime::charsets::CP_1125; break;
		case 1250: ch = vmime::charsets::CP_1250; break;
		case 1251: ch = vmime::charsets::CP_1251; break;
		case 1252: ch = vmime::charsets::CP_1252; break;
		case 1253: ch = vmime::charsets::CP_1253; break;
		case 1254: ch = vmime::charsets::CP_1254; break;
		case 1255: ch = vmime::charsets::CP_1255; break;
		case 1256: ch = vmime::charsets::CP_1256; break;
		case 1257: ch = vmime::charsets::CP_1257; break;

		case 28591: ch = vmime::charsets::ISO8859_1; break;
		case 28592: ch = vmime::charsets::ISO8859_2; break;
		case 28593: ch = vmime::charsets::ISO8859_3; break;
		case 28594: ch = vmime::charsets::ISO8859_4; break;
		case 28595: ch = vmime::charsets::ISO8859_5; break;
		case 28596: ch = vmime::charsets::ISO8859_6; break;
		case 28597: ch = vmime::charsets::ISO8859_7; break;
		case 28598: ch = vmime::charsets::ISO8859_8; break;
		case 28599: ch = vmime::charsets::ISO8859_9; break;
		case 28605: ch = vmime::charsets::ISO8859_15; break;

		case 65000: ch = vmime::charsets::UTF_7; break;
		case 65001: ch = vmime::charsets::UTF_8; break;
	}

	return vmime::charset(ch);

#endif

}


const vmime::string windowsHandler::getHostName() const {

	char hostname[1024];
	DWORD hostnameLen;

	// First, try to get a Fully-Qualified Domain Name (FQDN)
	for (int cnf = ComputerNameDnsHostname ; cnf <= ComputerNameDnsFullyQualified ; ++cnf) {

		hostnameLen = sizeof(hostname);

		if (GetComputerNameEx((COMPUTER_NAME_FORMAT) cnf, hostname, &hostnameLen)) {

			const vmime::string hostnameStr(hostname);

			if (utility::stringUtils::isValidFQDN(hostnameStr)) {
				return hostnameStr;
			}
		}
	}

	// Anything else will be OK, as long as it is a valid hostname
	for (int cnf = 0 ; cnf < ComputerNameMax ; ++cnf) {

		hostnameLen = sizeof(hostname);

		if (GetComputerNameEx((COMPUTER_NAME_FORMAT) cnf, hostname, &hostnameLen)) {

			const vmime::string hostnameStr(hostname);

			if (utility::stringUtils::isValidHostname(hostnameStr)) {
				return hostnameStr;
			}
		}
	}

	return "localhost.localdomain";
}


unsigned int windowsHandler::getProcessId() const {

	return static_cast <unsigned int>(::GetCurrentProcessId());
}


unsigned int windowsHandler::getThreadId() const {

	return static_cast <unsigned int>(::GetCurrentThreadId());
}


#if VMIME_HAVE_MESSAGING_FEATURES

shared_ptr <vmime::net::socketFactory> windowsHandler::getSocketFactory() {

	return m_socketFactory;
}

#endif


#if VMIME_HAVE_FILESYSTEM_FEATURES

shared_ptr <vmime::utility::fileSystemFactory> windowsHandler::getFileSystemFactory() {

	return m_fileSysFactory;
}


shared_ptr <vmime::utility::childProcessFactory> windowsHandler::getChildProcessFactory() {

	// TODO: Not implemented
	return null;
}

#endif


void windowsHandler::generateRandomBytes(unsigned char* buffer, const unsigned int count) {

	HCRYPTPROV cryptProvider = 0;
	CryptAcquireContext(&cryptProvider, 0, 0, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT);
	CryptGenRandom(cryptProvider, static_cast <unsigned long>(count), static_cast <unsigned char*>(buffer));
	CryptReleaseContext(cryptProvider, 0);
}


shared_ptr <utility::sync::criticalSection> windowsHandler::createCriticalSection() {

	return make_shared <windowsCriticalSection>();
}


} // posix
} // platforms
} // vmime


#endif // VMIME_PLATFORM_IS_WINDOWS