1 /* 2 * Copyright (c) 1998, 2012, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 /* Access APIs for Windows Vista and above */ 27 #ifndef _WIN32_WINNT 28 #define _WIN32_WINNT 0x0601 29 #endif 30 31 #include <windows.h> 32 #include <shlobj.h> 33 #include <objidl.h> 34 #include <locale.h> 35 #include <sys/types.h> 36 #include <sys/timeb.h> 37 #include <tchar.h> 38 39 #include <stdlib.h> 40 #include <Wincon.h> 41 42 #include "locale_str.h" 43 #include "java_props.h" 44 45 #ifndef VER_PLATFORM_WIN32_WINDOWS 46 #define VER_PLATFORM_WIN32_WINDOWS 1 47 #endif 48 49 #ifndef PROCESSOR_ARCHITECTURE_AMD64 50 #define PROCESSOR_ARCHITECTURE_AMD64 9 51 #endif 52 53 typedef void (WINAPI *PGNSI)(LPSYSTEM_INFO); 54 static void SetupI18nProps(LCID lcid, char** language, char** script, char** country, 55 char** variant, char** encoding); 56 57 #define PROPSIZE 9 // eight-letter + null terminator 58 #define SNAMESIZE 86 // max number of chars for LOCALE_SNAME is 85 59 60 static char * 61 getEncodingInternal(LCID lcid) 62 { 63 char * ret = malloc(16); 64 int codepage; 65 66 if (GetLocaleInfo(lcid, 67 LOCALE_IDEFAULTANSICODEPAGE, 68 ret+2, 14) == 0) { 69 codepage = 1252; 70 } else { 71 codepage = atoi(ret+2); 72 } 73 74 switch (codepage) { 75 case 0: 76 strcpy(ret, "UTF-8"); 77 break; 78 case 874: /* 9:Thai */ 79 case 932: /* 10:Japanese */ 80 case 949: /* 12:Korean Extended Wansung */ 81 case 950: /* 13:Chinese (Taiwan, Hongkong, Macau) */ 82 case 1361: /* 15:Korean Johab */ 83 ret[0] = 'M'; 84 ret[1] = 'S'; 85 break; 86 case 936: 87 strcpy(ret, "GBK"); 88 break; 89 case 54936: 90 strcpy(ret, "GB18030"); 91 break; 92 default: 93 ret[0] = 'C'; 94 ret[1] = 'p'; 95 break; 96 } 97 98 //Traditional Chinese Windows should use MS950_HKSCS_XP as the 99 //default encoding, if HKSCS patch has been installed. 100 // "old" MS950 0xfa41 -> u+e001 101 // "new" MS950 0xfa41 -> u+92db 102 if (strcmp(ret, "MS950") == 0) { 103 TCHAR mbChar[2] = {(char)0xfa, (char)0x41}; 104 WCHAR unicodeChar; 105 MultiByteToWideChar(CP_ACP, 0, mbChar, 2, &unicodeChar, 1); 106 if (unicodeChar == 0x92db) { 107 strcpy(ret, "MS950_HKSCS_XP"); 108 } 109 } else { 110 //SimpChinese Windows should use GB18030 as the default 111 //encoding, if gb18030 patch has been installed (on windows 112 //2000/XP, (1)Codepage 54936 will be available 113 //(2)simsun18030.ttc will exist under system fonts dir ) 114 if (strcmp(ret, "GBK") == 0 && IsValidCodePage(54936)) { 115 char systemPath[MAX_PATH + 1]; 116 char* gb18030Font = "\\FONTS\\SimSun18030.ttc"; 117 FILE *f = NULL; 118 if (GetWindowsDirectory(systemPath, MAX_PATH + 1) != 0 && 119 strlen(systemPath) + strlen(gb18030Font) < MAX_PATH + 1) { 120 strcat(systemPath, "\\FONTS\\SimSun18030.ttc"); 121 if ((f = fopen(systemPath, "r")) != NULL) { 122 fclose(f); 123 strcpy(ret, "GB18030"); 124 } 125 } 126 } 127 } 128 129 return ret; 130 } 131 132 static char* getConsoleEncoding() 133 { 134 char* buf = malloc(16); 135 int cp = GetConsoleCP(); 136 if (cp >= 874 && cp <= 950) 137 sprintf(buf, "ms%d", cp); 138 else 139 sprintf(buf, "cp%d", cp); 140 return buf; 141 } 142 143 // Exported entries for AWT 144 DllExport const char * 145 getEncodingFromLangID(LANGID langID) 146 { 147 return getEncodingInternal(MAKELCID(langID, SORT_DEFAULT)); 148 } 149 150 // Returns BCP47 Language Tag 151 DllExport const char * 152 getJavaIDFromLangID(LANGID langID) 153 { 154 char * elems[5]; // lang, script, ctry, variant, encoding 155 char * ret = malloc(SNAMESIZE); 156 int index; 157 158 SetupI18nProps(MAKELCID(langID, SORT_DEFAULT), 159 &(elems[0]), &(elems[1]), &(elems[2]), &(elems[3]), &(elems[4])); 160 161 // there always is the "language" tag 162 strcpy(ret, elems[0]); 163 164 // append other elements, if any 165 for (index = 1; index < 4; index++) { 166 if ((elems[index])[0] != '\0') { 167 strcat(ret, "-"); 168 strcat(ret, elems[index]); 169 } 170 } 171 172 for (index = 0; index < 5; index++) { 173 free(elems[index]); 174 } 175 176 return ret; 177 } 178 179 /* 180 * Code to figure out the user's home directory using shell32.dll 181 */ 182 WCHAR* 183 getHomeFromShell32() 184 { 185 /* 186 * Note that we don't free the memory allocated 187 * by getHomeFromShell32. 188 */ 189 static WCHAR *u_path = NULL; 190 if (u_path == NULL) { 191 HRESULT hr; 192 193 /* 194 * SHELL32 DLL is delay load DLL and we can use the trick with 195 * __try/__except block. 196 */ 197 __try { 198 /* 199 * For Windows Vista and later (or patched MS OS) we need to use 200 * [SHGetKnownFolderPath] call to avoid MAX_PATH length limitation. 201 * Shell32.dll (version 6.0.6000 or later) 202 */ 203 hr = SHGetKnownFolderPath(&FOLDERID_Profile, KF_FLAG_DONT_VERIFY, NULL, &u_path); 204 } __except(EXCEPTION_EXECUTE_HANDLER) { 205 /* Exception: no [SHGetKnownFolderPath] entry */ 206 hr = E_FAIL; 207 } 208 209 if (FAILED(hr)) { 210 WCHAR path[MAX_PATH+1]; 211 212 /* fallback solution for WinXP and Windows 2000 */ 213 hr = SHGetFolderPathW(NULL, CSIDL_FLAG_DONT_VERIFY | CSIDL_PROFILE, NULL, SHGFP_TYPE_CURRENT, path); 214 if (FAILED(hr)) { 215 /* we can't find the shell folder. */ 216 u_path = NULL; 217 } else { 218 /* Just to be sure about the path length until Windows Vista approach. 219 * [S_FALSE] could not be returned due to [CSIDL_FLAG_DONT_VERIFY] flag and UNICODE version. 220 */ 221 path[MAX_PATH] = 0; 222 u_path = _wcsdup(path); 223 } 224 } 225 } 226 return u_path; 227 } 228 229 static boolean 230 haveMMX(void) 231 { 232 return IsProcessorFeaturePresent(PF_MMX_INSTRUCTIONS_AVAILABLE); 233 } 234 235 static const char * 236 cpu_isalist(void) 237 { 238 SYSTEM_INFO info; 239 GetSystemInfo(&info); 240 switch (info.wProcessorArchitecture) { 241 #ifdef PROCESSOR_ARCHITECTURE_IA64 242 case PROCESSOR_ARCHITECTURE_IA64: return "ia64"; 243 #endif 244 #ifdef PROCESSOR_ARCHITECTURE_AMD64 245 case PROCESSOR_ARCHITECTURE_AMD64: return "amd64"; 246 #endif 247 case PROCESSOR_ARCHITECTURE_INTEL: 248 switch (info.wProcessorLevel) { 249 case 6: return haveMMX() 250 ? "pentium_pro+mmx pentium_pro pentium+mmx pentium i486 i386 i86" 251 : "pentium_pro pentium i486 i386 i86"; 252 case 5: return haveMMX() 253 ? "pentium+mmx pentium i486 i386 i86" 254 : "pentium i486 i386 i86"; 255 case 4: return "i486 i386 i86"; 256 case 3: return "i386 i86"; 257 } 258 } 259 return NULL; 260 } 261 262 static void 263 SetupI18nProps(LCID lcid, char** language, char** script, char** country, 264 char** variant, char** encoding) { 265 /* script */ 266 char tmp[SNAMESIZE]; 267 *script = malloc(PROPSIZE); 268 if (GetLocaleInfo(lcid, 269 LOCALE_SNAME, tmp, SNAMESIZE) == 0 || 270 sscanf(tmp, "%*[a-z\\-]%1[A-Z]%[a-z]", *script, &((*script)[1])) == 0 || 271 strlen(*script) != 4) { 272 (*script)[0] = '\0'; 273 } 274 275 /* country */ 276 *country = malloc(PROPSIZE); 277 if (GetLocaleInfo(lcid, 278 LOCALE_SISO3166CTRYNAME, *country, PROPSIZE) == 0 && 279 GetLocaleInfo(lcid, 280 LOCALE_SISO3166CTRYNAME2, *country, PROPSIZE) == 0) { 281 (*country)[0] = '\0'; 282 } 283 284 /* language */ 285 *language = malloc(PROPSIZE); 286 if (GetLocaleInfo(lcid, 287 LOCALE_SISO639LANGNAME, *language, PROPSIZE) == 0 && 288 GetLocaleInfo(lcid, 289 LOCALE_SISO639LANGNAME2, *language, PROPSIZE) == 0) { 290 /* defaults to en_US */ 291 strcpy(*language, "en"); 292 strcpy(*country, "US"); 293 } 294 295 /* variant */ 296 *variant = malloc(PROPSIZE); 297 (*variant)[0] = '\0'; 298 299 /* handling for Norwegian */ 300 if (strcmp(*language, "nb") == 0) { 301 strcpy(*language, "no"); 302 strcpy(*country , "NO"); 303 } else if (strcmp(*language, "nn") == 0) { 304 strcpy(*language, "no"); 305 strcpy(*country , "NO"); 306 strcpy(*variant, "NY"); 307 } 308 309 /* encoding */ 310 *encoding = getEncodingInternal(lcid); 311 } 312 313 java_props_t * 314 GetJavaProperties(JNIEnv* env) 315 { 316 static java_props_t sprops = {0}; 317 318 OSVERSIONINFOEX ver; 319 320 if (sprops.line_separator) { 321 return &sprops; 322 } 323 324 /* AWT properties */ 325 sprops.awt_toolkit = "sun.awt.windows.WToolkit"; 326 327 /* tmp dir */ 328 { 329 WCHAR tmpdir[MAX_PATH + 1]; 330 /* we might want to check that this succeed */ 331 GetTempPathW(MAX_PATH + 1, tmpdir); 332 sprops.tmp_dir = _wcsdup(tmpdir); 333 } 334 335 /* Printing properties */ 336 sprops.printerJob = "sun.awt.windows.WPrinterJob"; 337 338 /* Java2D properties */ 339 sprops.graphics_env = "sun.awt.Win32GraphicsEnvironment"; 340 341 { /* This is used only for debugging of font problems. */ 342 WCHAR *path = _wgetenv(L"JAVA2D_FONTPATH"); 343 sprops.font_dir = (path != NULL) ? _wcsdup(path) : NULL; 344 } 345 346 /* OS properties */ 347 { 348 char buf[100]; 349 SYSTEM_INFO si; 350 PGNSI pGNSI; 351 352 ver.dwOSVersionInfoSize = sizeof(ver); 353 GetVersionEx((OSVERSIONINFO *) &ver); 354 355 ZeroMemory(&si, sizeof(SYSTEM_INFO)); 356 // Call GetNativeSystemInfo if supported or GetSystemInfo otherwise. 357 pGNSI = (PGNSI) GetProcAddress( 358 GetModuleHandle(TEXT("kernel32.dll")), 359 "GetNativeSystemInfo"); 360 if(NULL != pGNSI) 361 pGNSI(&si); 362 else 363 GetSystemInfo(&si); 364 365 /* 366 * From msdn page on OSVERSIONINFOEX, current as of this 367 * writing, decoding of dwMajorVersion and dwMinorVersion. 368 * 369 * Operating system dwMajorVersion dwMinorVersion 370 * ================== ============== ============== 371 * 372 * Windows 95 4 0 373 * Windows 98 4 10 374 * Windows ME 4 90 375 * Windows 3.51 3 51 376 * Windows NT 4.0 4 0 377 * Windows 2000 5 0 378 * Windows XP 32 bit 5 1 379 * Windows Server 2003 family 5 2 380 * Windows XP 64 bit 5 2 381 * where ((&ver.wServicePackMinor) + 2) = 1 382 * and si.wProcessorArchitecture = 9 383 * Windows Vista family 6 0 (VER_NT_WORKSTATION) 384 * Windows Server 2008 6 0 (!VER_NT_WORKSTATION) 385 * Windows 7 6 1 (VER_NT_WORKSTATION) 386 * Windows Server 2008 R2 6 1 (!VER_NT_WORKSTATION) 387 * Windows 8 6 2 (VER_NT_WORKSTATION) 388 * Windows Server 2012 6 2 (!VER_NT_WORKSTATION) 389 * 390 * This mapping will presumably be augmented as new Windows 391 * versions are released. 392 */ 393 switch (ver.dwPlatformId) { 394 case VER_PLATFORM_WIN32s: 395 sprops.os_name = "Windows 3.1"; 396 break; 397 case VER_PLATFORM_WIN32_WINDOWS: 398 if (ver.dwMajorVersion == 4) { 399 switch (ver.dwMinorVersion) { 400 case 0: sprops.os_name = "Windows 95"; break; 401 case 10: sprops.os_name = "Windows 98"; break; 402 case 90: sprops.os_name = "Windows Me"; break; 403 default: sprops.os_name = "Windows 9X (unknown)"; break; 404 } 405 } else { 406 sprops.os_name = "Windows 9X (unknown)"; 407 } 408 break; 409 case VER_PLATFORM_WIN32_NT: 410 if (ver.dwMajorVersion <= 4) { 411 sprops.os_name = "Windows NT"; 412 } else if (ver.dwMajorVersion == 5) { 413 switch (ver.dwMinorVersion) { 414 case 0: sprops.os_name = "Windows 2000"; break; 415 case 1: sprops.os_name = "Windows XP"; break; 416 case 2: 417 /* 418 * From MSDN OSVERSIONINFOEX and SYSTEM_INFO documentation: 419 * 420 * "Because the version numbers for Windows Server 2003 421 * and Windows XP 6u4 bit are identical, you must also test 422 * whether the wProductType member is VER_NT_WORKSTATION. 423 * and si.wProcessorArchitecture is 424 * PROCESSOR_ARCHITECTURE_AMD64 (which is 9) 425 * If it is, the operating system is Windows XP 64 bit; 426 * otherwise, it is Windows Server 2003." 427 */ 428 if(ver.wProductType == VER_NT_WORKSTATION && 429 si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64) { 430 sprops.os_name = "Windows XP"; /* 64 bit */ 431 } else { 432 sprops.os_name = "Windows 2003"; 433 } 434 break; 435 default: sprops.os_name = "Windows NT (unknown)"; break; 436 } 437 } else if (ver.dwMajorVersion == 6) { 438 /* 439 * See table in MSDN OSVERSIONINFOEX documentation. 440 */ 441 if (ver.wProductType == VER_NT_WORKSTATION) { 442 switch (ver.dwMinorVersion) { 443 case 0: sprops.os_name = "Windows Vista"; break; 444 case 1: sprops.os_name = "Windows 7"; break; 445 case 2: sprops.os_name = "Windows 8"; break; 446 case 3: sprops.os_name = "Windows 8.1"; break; 447 default: sprops.os_name = "Windows NT (unknown)"; 448 } 449 } else { 450 switch (ver.dwMinorVersion) { 451 case 0: sprops.os_name = "Windows Server 2008"; break; 452 case 1: sprops.os_name = "Windows Server 2008 R2"; break; 453 case 2: sprops.os_name = "Windows Server 2012"; break; 454 case 3: sprops.os_name = "Windows Server 2012 R2"; break; 455 default: sprops.os_name = "Windows NT (unknown)"; 456 } 457 } 458 } else { 459 sprops.os_name = "Windows NT (unknown)"; 460 } 461 break; 462 default: 463 sprops.os_name = "Windows (unknown)"; 464 break; 465 } 466 sprintf(buf, "%d.%d", ver.dwMajorVersion, ver.dwMinorVersion); 467 sprops.os_version = _strdup(buf); 468 #if _M_IA64 469 sprops.os_arch = "ia64"; 470 #elif _M_AMD64 471 sprops.os_arch = "amd64"; 472 #elif _X86_ 473 sprops.os_arch = "x86"; 474 #else 475 sprops.os_arch = "unknown"; 476 #endif 477 478 sprops.patch_level = _strdup(ver.szCSDVersion); 479 480 sprops.desktop = "windows"; 481 } 482 483 /* Endianness of platform */ 484 { 485 unsigned int endianTest = 0xff000000; 486 if (((char*)(&endianTest))[0] != 0) { 487 sprops.cpu_endian = "big"; 488 } else { 489 sprops.cpu_endian = "little"; 490 } 491 } 492 493 /* CPU ISA list */ 494 sprops.cpu_isalist = cpu_isalist(); 495 496 /* 497 * User name 498 * We try to avoid calling GetUserName as it turns out to 499 * be surprisingly expensive on NT. It pulls in an extra 500 * 100 K of footprint. 501 */ 502 { 503 WCHAR *uname = _wgetenv(L"USERNAME"); 504 if (uname != NULL && wcslen(uname) > 0) { 505 sprops.user_name = _wcsdup(uname); 506 } else { 507 DWORD buflen = 0; 508 if (GetUserNameW(NULL, &buflen) == 0 && 509 GetLastError() == ERROR_INSUFFICIENT_BUFFER) 510 { 511 uname = (WCHAR*)malloc(buflen * sizeof(WCHAR)); 512 if (uname != NULL && GetUserNameW(uname, &buflen) == 0) { 513 free(uname); 514 uname = NULL; 515 } 516 } else { 517 uname = NULL; 518 } 519 sprops.user_name = (uname != NULL) ? uname : L"unknown"; 520 } 521 } 522 523 /* 524 * Home directory 525 * 526 * The normal result is that for a given user name XXX: 527 * On multi-user NT, user.home gets set to c:\winnt\profiles\XXX. 528 * On multi-user Win95, user.home gets set to c:\windows\profiles\XXX. 529 * On single-user Win95, user.home gets set to c:\windows. 530 */ 531 { 532 WCHAR *homep = getHomeFromShell32(); 533 if (homep == NULL) { 534 homep = L"C:\\"; 535 } 536 sprops.user_home = homep; 537 } 538 539 /* 540 * user.language 541 * user.script, user.country, user.variant (if user's environment specifies them) 542 * file.encoding 543 * file.encoding.pkg 544 */ 545 { 546 /* 547 * query the system for the current system default locale 548 * (which is a Windows LCID value), 549 */ 550 LCID userDefaultLCID = GetUserDefaultLCID(); 551 LCID systemDefaultLCID = GetSystemDefaultLCID(); 552 LCID userDefaultUILang = GetUserDefaultUILanguage(); 553 554 { 555 char * display_encoding; 556 HANDLE hStdOutErr; 557 558 // Windows UI Language selection list only cares "language" 559 // information of the UI Language. For example, the list 560 // just lists "English" but it actually means "en_US", and 561 // the user cannot select "en_GB" (if exists) in the list. 562 // So, this hack is to use the user LCID region information 563 // for the UI Language, if the "language" portion of those 564 // two locales are the same. 565 if (PRIMARYLANGID(LANGIDFROMLCID(userDefaultLCID)) == 566 PRIMARYLANGID(LANGIDFROMLCID(userDefaultUILang))) { 567 userDefaultUILang = userDefaultLCID; 568 } 569 570 SetupI18nProps(userDefaultUILang, 571 &sprops.language, 572 &sprops.script, 573 &sprops.country, 574 &sprops.variant, 575 &display_encoding); 576 SetupI18nProps(userDefaultLCID, 577 &sprops.format_language, 578 &sprops.format_script, 579 &sprops.format_country, 580 &sprops.format_variant, 581 &sprops.encoding); 582 SetupI18nProps(userDefaultUILang, 583 &sprops.display_language, 584 &sprops.display_script, 585 &sprops.display_country, 586 &sprops.display_variant, 587 &display_encoding); 588 589 sprops.sun_jnu_encoding = getEncodingInternal(systemDefaultLCID); 590 if (LANGIDFROMLCID(userDefaultLCID) == 0x0c04 && ver.dwMajorVersion == 6) { 591 // MS claims "Vista has built-in support for HKSCS-2004. 592 // All of the HKSCS-2004 characters have Unicode 4.1. 593 // PUA code point assignments". But what it really means 594 // is that the HKSCS-2004 is ONLY supported in Unicode. 595 // Test indicates the MS950 in its zh_HK locale is a 596 // "regular" MS950 which does not handle HKSCS-2004 at 597 // all. Set encoding to MS950_HKSCS. 598 sprops.encoding = "MS950_HKSCS"; 599 sprops.sun_jnu_encoding = "MS950_HKSCS"; 600 } 601 602 hStdOutErr = GetStdHandle(STD_OUTPUT_HANDLE); 603 if (hStdOutErr != INVALID_HANDLE_VALUE && 604 GetFileType(hStdOutErr) == FILE_TYPE_CHAR) { 605 sprops.sun_stdout_encoding = getConsoleEncoding(); 606 } 607 hStdOutErr = GetStdHandle(STD_ERROR_HANDLE); 608 if (hStdOutErr != INVALID_HANDLE_VALUE && 609 GetFileType(hStdOutErr) == FILE_TYPE_CHAR) { 610 if (sprops.sun_stdout_encoding != NULL) 611 sprops.sun_stderr_encoding = sprops.sun_stdout_encoding; 612 else 613 sprops.sun_stderr_encoding = getConsoleEncoding(); 614 } 615 } 616 } 617 618 sprops.unicode_encoding = "UnicodeLittle"; 619 /* User TIMEZONE */ 620 { 621 /* 622 * We defer setting up timezone until it's actually necessary. 623 * Refer to TimeZone.getDefault(). However, the system 624 * property is necessary to be able to be set by the command 625 * line interface -D. Here temporarily set a null string to 626 * timezone. 627 */ 628 sprops.timezone = ""; 629 } 630 631 /* Current directory */ 632 { 633 WCHAR buf[MAX_PATH]; 634 if (GetCurrentDirectoryW(sizeof(buf)/sizeof(WCHAR), buf) != 0) 635 sprops.user_dir = _wcsdup(buf); 636 } 637 638 sprops.file_separator = "\\"; 639 sprops.path_separator = ";"; 640 sprops.line_separator = "\r\n"; 641 642 return &sprops; 643 } 644 645 jstring 646 GetStringPlatform(JNIEnv *env, nchar* wcstr) 647 { 648 return (*env)->NewString(env, wcstr, wcslen(wcstr)); 649 }