util.c 8.78 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 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408
/* SPDX-License-Identifier: LGPL-3.0-or-later */

/*
 * Copyright (C) 2008 Banco do Brasil S.A.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser 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 Lesser General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

/*
 * 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 <io.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[MAX_PATH+1];

	memset(lpFilename,0,sizeof(lpFilename));
	DWORD szPath = GetModuleFileName(hModule,lpFilename,MAX_PATH);
	lpFilename[szPath] = 0;

	char *ptr = strrchr(lpFilename,'\\');
	if(ptr) {
		ptr[0] = 0;

		ptr = strrchr(lpFilename,'\\');
		if(ptr && !(strcasecmp(ptr,"\\bin") && strcasecmp(ptr,"\\lib"))) {
			*ptr = 0;
		}

		strncat(lpFilename,"\\",MAX_PATH);
	}

	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);

	DWORD szPath = GetModuleFileName(hModule,filename,szFilename);
	filename[szPath] = 0;

	ptr = strrchr(filename,'\\');
	if(ptr) {
		ptr[0] = 0;

		ptr = strrchr(filename,'\\');
		if(ptr && !(strcasecmp(ptr,"\\bin") && strcasecmp(ptr,"\\lib"))) {
			*ptr = 0;
		}

		strncat(filename,"\\",szFilename);
	}

	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, ...) {

	char *ptr;
	lib3270_autoptr(char) instpath = lib3270_get_installation_path();

	for(ptr = instpath; *ptr; ptr++) {
		if(*ptr == '/') {
			*ptr = '\\';
		}
	}

	if( *(instpath+strlen(instpath)-1) == '\\') {
		instpath[strlen(instpath)-1] = 0;
	}

	char relative[MAX_PATH+1];
	memset(relative,0,MAX_PATH);

	{
		va_list args;
		va_start (args, str);

		while(str) {

			if(str[0] == '\\' || str[0] == '/') {
				strncat(relative,str,MAX_PATH);
			} else {
				strncat(relative,"\\",MAX_PATH);
				strncat(relative,str,MAX_PATH);
			}

			str = va_arg(args, const char *);
		}

		va_end (args);
	}

	for(ptr = relative; *ptr; ptr++) {
		if(*ptr == '/') {
			*ptr = '\\';
		}
	}

	char filename[MAX_PATH+1];
	memset(filename,0,MAX_PATH+1);

	// Check instdir
	strncpy(filename,instpath,MAX_PATH);
	strncat(filename,"\\share",MAX_PATH);
	strncat(filename,relative,MAX_PATH);

	if(access(filename,0) == 0) {
		return strdup(filename);
	}

	strncpy(filename,instpath,MAX_PATH);
	strncat(filename,"\\share\\",MAX_PATH);
	strncat(filename,LIB3270_STRINGIZE_VALUE_OF(PRODUCT_NAME),MAX_PATH);
	strncat(filename,relative,MAX_PATH);

	if(access(filename,0) == 0) {
		return strdup(filename);
	}

	// Default behavior.
	strncpy(filename,instpath,MAX_PATH);
	strncat(filename,relative,MAX_PATH);

	return strdup(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;
	}
}