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