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