util.c
7.68 KB
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/*
* "Software pw3270, desenvolvido com base nos códigos fontes do WC3270 e X3270
* (Paul Mattes Paul.Mattes@usa.net), de emulação de terminal 3270 para acesso a
* aplicativos mainframe. Registro no INPI sob o nome G3270. Registro no INPI sob
* o nome G3270.
*
* Copyright (C) <2008> <Banco do Brasil S.A.>
*
* Este programa é software livre. Você pode redistribuí-lo e/ou modificá-lo sob
* os termos da GPL v.2 - Licença Pública Geral GNU, conforme publicado pela
* Free Software Foundation.
*
* Este programa é distribuído na expectativa de ser útil, mas SEM QUALQUER
* GARANTIA; sem mesmo a garantia implícita de COMERCIALIZAÇÃO ou de ADEQUAÇÃO
* A QUALQUER PROPÓSITO EM PARTICULAR. Consulte a Licença Pública Geral GNU para
* obter mais detalhes.
*
* Você deve ter recebido uma cópia da Licença Pública Geral GNU junto com este
* programa; se não, escreva para a Free Software Foundation, Inc., 51 Franklin
* St, Fifth Floor, Boston, MA 02110-1301 USA
*
* Este programa está nomeado como - e possui - linhas de código.
*
* Contatos:
*
* perry.werneck@gmail.com (Alexandre Perry de Souza Werneck)
* erico.mendonca@gmail.com (Erico Mascarenhas Mendonça)
*
*/
/**
* @brief Win32 Utility functions.
*/
#include <winsock2.h>
#include <windows.h>
#include <lmcons.h>
#include <internals.h>
#include "winversc.h"
#include <ws2tcpip.h>
#include <stdio.h>
#include <errno.h>
#include "w3miscc.h"
#include <malloc.h>
#ifdef HAVE_ICONV
#include <iconv.h>
#endif // HAVE_ICONV
#include <lib3270/log.h>
#include <lib3270/win32.h>
#define my_isspace(c) isspace((unsigned char)c)
int is_nt = 1;
int has_ipv6 = 1;
int get_version_info(void) {
OSVERSIONINFO info;
// Figure out what version of Windows this is.
memset(&info, '\0', sizeof(info));
info.dwOSVersionInfoSize = sizeof(info);
if(GetVersionEx(&info) == 0) {
lib3270_write_log(NULL,"lib3270","%s","Can't get Windows version");
return -1;
}
// Yes, people still run Win98.
if (info.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS)
is_nt = 0;
// Win2K and earlier is IPv4-only. WinXP and later can have IPv6.
if (!is_nt || info.dwMajorVersion < 5 || (info.dwMajorVersion == 5 && info.dwMinorVersion < 1)) {
has_ipv6 = 0;
}
return 0;
}
// Convert a network address to a string.
#ifndef HAVE_INET_NTOP
const char * inet_ntop(int af, const void *src, char *dst, socklen_t cnt) {
union {
struct sockaddr sa;
struct sockaddr_in sin;
struct sockaddr_in6 sin6;
} sa;
DWORD ssz;
DWORD sz = cnt;
memset(&sa, '\0', sizeof(sa));
switch (af) {
case AF_INET:
sa.sin = *(struct sockaddr_in *)src; // struct copy
ssz = sizeof(struct sockaddr_in);
break;
case AF_INET6:
sa.sin6 = *(struct sockaddr_in6 *)src; // struct copy
ssz = sizeof(struct sockaddr_in6);
break;
default:
if (cnt > 0)
dst[0] = '\0';
return NULL;
}
sa.sa.sa_family = af;
if (WSAAddressToString(&sa.sa, ssz, NULL, dst, &sz) != 0) {
if (cnt > 0)
dst[0] = '\0';
return NULL;
}
return dst;
}
#endif // HAVE_INET_NTOP
LIB3270_EXPORT char * lib3270_win32_translate_error_code(int lasterror) {
char * buffer = lib3270_malloc(4096);
if(FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,NULL,lasterror,MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),buffer,4096,NULL) == 0) {
snprintf(buffer, 4095, _( "Windows error %d" ), lasterror);
}
#ifdef HAVE_ICONV
{
// Convert from windows codepage to pw3270´s default charset (UTF-8)
iconv_t hConv = iconv_open("UTF-8",lib3270_win32_local_charset());
trace("[%s]",buffer);
if(hConv == (iconv_t) -1) {
lib3270_write_log(NULL,"iconv","%s: Error creating charset conversion",__FUNCTION__);
} else {
size_t in = strlen(buffer);
size_t out = (in << 1);
char * ptr;
char * outBuffer = (char *) malloc(out);
ICONV_CONST char * inBuffer = (ICONV_CONST char *) buffer;
memset(ptr=outBuffer,0,out);
iconv(hConv,NULL,NULL,NULL,NULL); // Reset state
if(iconv(hConv,&inBuffer,&in,&ptr,&out) != ((size_t) -1)) {
strncpy(buffer,outBuffer,4095);
}
free(outBuffer);
iconv_close(hConv);
}
}
#endif // HAVE_ICONV
return buffer;
}
// Decode a Win32 error number.
LIB3270_EXPORT const char * lib3270_win32_strerror(int e) {
static char buffer[4096];
if(FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,NULL,e,MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),buffer,sizeof(buffer),NULL) == 0) {
snprintf(buffer, 4095, _( "Windows error %d" ), e);
return buffer;
}
#ifdef HAVE_ICONV
{
// Convert from windows codepage to UTF-8 pw3270´s default charset
iconv_t hConv = iconv_open("UTF-8",lib3270_win32_local_charset());
trace("[%s]",buffer);
if(hConv == (iconv_t) -1) {
lib3270_write_log(NULL,"iconv","%s: Error creating charset conversion",__FUNCTION__);
} else {
size_t in = strlen(buffer);
size_t out = (in << 1);
char * ptr;
char * outBuffer = (char *) malloc(out);
ICONV_CONST char * inBuffer = (ICONV_CONST char *) buffer;
memset(ptr=outBuffer,0,out);
iconv(hConv,NULL,NULL,NULL,NULL); // Reset state
if(iconv(hConv,&inBuffer,&in,&ptr,&out) != ((size_t) -1)) {
strncpy(buffer,outBuffer,4095);
}
free(outBuffer);
iconv_close(hConv);
}
}
#else
#error NO-ICONV
#endif // HAVE_ICONV
return buffer;
}
LIB3270_EXPORT const char * lib3270_win32_local_charset(void) {
// Reference:
// http://msdn.microsoft.com/en-us/library/windows/desktop/dd318070(v=vs.85).aspx
/// TODO: Use GetACP() to identify the correct code page
trace("Windows CHARSET is %u",GetACP());
return "CP1252";
}
#define SECS_BETWEEN_EPOCHS 11644473600ULL
#define SECS_TO_100NS 10000000ULL /* 10^7 */
LIB3270_EXPORT char * lib3270_get_installation_path() {
char lpFilename[4096];
memset(lpFilename,0,sizeof(lpFilename));
DWORD szPath = GetModuleFileName(hModule,lpFilename,sizeof(lpFilename));
lpFilename[szPath] = 0;
char * ptr = strrchr(lpFilename,'\\');
if(ptr)
ptr[1] = 0;
return strdup(lpFilename);
}
char * lib3270_get_user_name() {
char username[UNLEN + 1];
DWORD szName = UNLEN;
memset(username,0,UNLEN + 1);
GetUserName(username, &szName);
return strdup(username);
}
static char * concat(char *path, const char *name, size_t *length) {
char *ptr;
size_t szCurrent = strlen(path);
for(ptr=path; *ptr; ptr++) {
if(*ptr == '/')
*ptr = '\\';
}
if(szCurrent > 1 && path[szCurrent-1] != '\\')
strcat(path,"\\");
szCurrent += strlen(name);
if(szCurrent >= *length) {
*length += (szCurrent + 1024);
path = lib3270_realloc(path,*length);
}
strcat(path,name);
return path;
}
static char * build_filename(const char *str, va_list args) {
size_t szFilename = MAX_PATH;
char *ptr;
char * filename = (char *) lib3270_malloc(szFilename);
memset(filename,0,szFilename);
#ifdef DEBUG
filename[0] = '.';
filename[1] = '\\';
#else
DWORD szPath = GetModuleFileName(hModule,filename,szFilename);
filename[szPath] = 0;
#endif // DEBUG
ptr = strrchr(filename,'\\');
if(ptr)
ptr[1] = 0;
while(str) {
filename = concat(filename,str,&szFilename);
str = va_arg(args, const char *);
}
return (char *) lib3270_realloc(filename,strlen(filename)+1);
}
char * lib3270_build_data_filename(const char *str, ...) {
va_list args;
va_start (args, str);
char *filename = build_filename(str, args);
va_end (args);
return filename;
}
char * lib3270_build_config_filename(const char *str, ...) {
va_list args;
va_start (args, str);
char *filename = build_filename(str, args);
va_end (args);
return filename;
}
char * lib3270_build_filename(const char *str, ...) {
va_list args;
va_start (args, str);
char *filename = build_filename(str, args);
va_end (args);
return filename;
}
LIB3270_EXPORT void lib3270_autoptr_cleanup_HKEY(HKEY *hKey) {
if(*hKey) {
RegCloseKey(*hKey);
*hKey = 0;
}
}