Sweden-Number/dlls/kernel32/locale.c

3892 lines
134 KiB
C

/*
* Locale support
*
* Copyright 1995 Martin von Loewis
* Copyright 1998 David Lee Lambert
* Copyright 2000 Julio César Gázquez
* Copyright 2002 Alexandre Julliard for CodeWeavers
*
* This library 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 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*/
#include "config.h"
#include "wine/port.h"
#include <assert.h>
#include <locale.h>
#include <string.h>
#include <stdarg.h>
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include "ntstatus.h"
#define WIN32_NO_STATUS
#include "windef.h"
#include "winbase.h"
#include "winuser.h" /* for RT_STRINGW */
#include "winternl.h"
#include "wine/unicode.h"
#include "winnls.h"
#include "winerror.h"
#include "winver.h"
#include "kernel_private.h"
#include "wine/heap.h"
#include "wine/debug.h"
WINE_DEFAULT_DEBUG_CHANNEL(nls);
#define LOCALE_LOCALEINFOFLAGSMASK (LOCALE_NOUSEROVERRIDE|LOCALE_USE_CP_ACP|\
LOCALE_RETURN_NUMBER|LOCALE_RETURN_GENITIVE_NAMES)
#define MB_FLAGSMASK (MB_PRECOMPOSED|MB_COMPOSITE|MB_USEGLYPHCHARS|MB_ERR_INVALID_CHARS)
#define WC_FLAGSMASK (WC_DISCARDNS|WC_SEPCHARS|WC_DEFAULTCHAR|WC_ERR_INVALID_CHARS|\
WC_COMPOSITECHECK|WC_NO_BEST_FIT_CHARS)
extern BOOL WINAPI Internal_EnumCalendarInfo( CALINFO_ENUMPROCW proc, LCID lcid, CALID id,
CALTYPE type, BOOL unicode, BOOL ex,
BOOL exex, LPARAM lparam );
extern BOOL WINAPI Internal_EnumDateFormats( DATEFMT_ENUMPROCW proc, LCID lcid, DWORD flags, BOOL unicode,
BOOL ex, BOOL exex, LPARAM lparam );
extern BOOL WINAPI Internal_EnumLanguageGroupLocales( LANGGROUPLOCALE_ENUMPROCW proc, LGRPID id,
DWORD flags, LONG_PTR param, BOOL unicode );
extern BOOL WINAPI Internal_EnumSystemCodePages( CODEPAGE_ENUMPROCW proc, DWORD flags, BOOL unicode );
extern BOOL WINAPI Internal_EnumSystemLanguageGroups( LANGUAGEGROUP_ENUMPROCW proc, DWORD flags,
LONG_PTR param, BOOL unicode );
extern BOOL WINAPI Internal_EnumTimeFormats( TIMEFMT_ENUMPROCW proc, LCID lcid, DWORD flags,
BOOL unicode, BOOL ex, LPARAM lparam );
extern BOOL WINAPI Internal_EnumUILanguages( UILANGUAGE_ENUMPROCW proc, DWORD flags,
LONG_PTR param, BOOL unicode );
/* current code pages */
static const union cptable *ansi_cptable;
static const union cptable *oem_cptable;
static const union cptable *mac_cptable;
static const union cptable *unix_cptable; /* NULL if UTF8 */
static const WCHAR iCalendarTypeW[] = {'i','C','a','l','e','n','d','a','r','T','y','p','e',0};
static const WCHAR iCountryW[] = {'i','C','o','u','n','t','r','y',0};
static const WCHAR iCurrDigitsW[] = {'i','C','u','r','r','D','i','g','i','t','s',0};
static const WCHAR iCurrencyW[] = {'i','C','u','r','r','e','n','c','y',0};
static const WCHAR iDateW[] = {'i','D','a','t','e',0};
static const WCHAR iDigitsW[] = {'i','D','i','g','i','t','s',0};
static const WCHAR iFirstDayOfWeekW[] = {'i','F','i','r','s','t','D','a','y','O','f','W','e','e','k',0};
static const WCHAR iFirstWeekOfYearW[] = {'i','F','i','r','s','t','W','e','e','k','O','f','Y','e','a','r',0};
static const WCHAR iLDateW[] = {'i','L','D','a','t','e',0};
static const WCHAR iLZeroW[] = {'i','L','Z','e','r','o',0};
static const WCHAR iMeasureW[] = {'i','M','e','a','s','u','r','e',0};
static const WCHAR iNegCurrW[] = {'i','N','e','g','C','u','r','r',0};
static const WCHAR iNegNumberW[] = {'i','N','e','g','N','u','m','b','e','r',0};
static const WCHAR iPaperSizeW[] = {'i','P','a','p','e','r','S','i','z','e',0};
static const WCHAR iTLZeroW[] = {'i','T','L','Z','e','r','o',0};
static const WCHAR iTimePrefixW[] = {'i','T','i','m','e','P','r','e','f','i','x',0};
static const WCHAR iTimeW[] = {'i','T','i','m','e',0};
static const WCHAR s1159W[] = {'s','1','1','5','9',0};
static const WCHAR s2359W[] = {'s','2','3','5','9',0};
static const WCHAR sCountryW[] = {'s','C','o','u','n','t','r','y',0};
static const WCHAR sCurrencyW[] = {'s','C','u','r','r','e','n','c','y',0};
static const WCHAR sDateW[] = {'s','D','a','t','e',0};
static const WCHAR sDecimalW[] = {'s','D','e','c','i','m','a','l',0};
static const WCHAR sGroupingW[] = {'s','G','r','o','u','p','i','n','g',0};
static const WCHAR sLanguageW[] = {'s','L','a','n','g','u','a','g','e',0};
static const WCHAR sListW[] = {'s','L','i','s','t',0};
static const WCHAR sLongDateW[] = {'s','L','o','n','g','D','a','t','e',0};
static const WCHAR sMonDecimalSepW[] = {'s','M','o','n','D','e','c','i','m','a','l','S','e','p',0};
static const WCHAR sMonGroupingW[] = {'s','M','o','n','G','r','o','u','p','i','n','g',0};
static const WCHAR sMonThousandSepW[] = {'s','M','o','n','T','h','o','u','s','a','n','d','S','e','p',0};
static const WCHAR sNativeDigitsW[] = {'s','N','a','t','i','v','e','D','i','g','i','t','s',0};
static const WCHAR sNegativeSignW[] = {'s','N','e','g','a','t','i','v','e','S','i','g','n',0};
static const WCHAR sPositiveSignW[] = {'s','P','o','s','i','t','i','v','e','S','i','g','n',0};
static const WCHAR sShortDateW[] = {'s','S','h','o','r','t','D','a','t','e',0};
static const WCHAR sThousandW[] = {'s','T','h','o','u','s','a','n','d',0};
static const WCHAR sTimeFormatW[] = {'s','T','i','m','e','F','o','r','m','a','t',0};
static const WCHAR sTimeW[] = {'s','T','i','m','e',0};
static const WCHAR sYearMonthW[] = {'s','Y','e','a','r','M','o','n','t','h',0};
static const WCHAR NumShapeW[] = {'N','u','m','s','h','a','p','e',0};
static struct registry_value
{
DWORD lctype;
const WCHAR *name;
WCHAR *cached_value;
} registry_values[] =
{
{ LOCALE_ICALENDARTYPE, iCalendarTypeW },
{ LOCALE_ICURRDIGITS, iCurrDigitsW },
{ LOCALE_ICURRENCY, iCurrencyW },
{ LOCALE_IDIGITS, iDigitsW },
{ LOCALE_IFIRSTDAYOFWEEK, iFirstDayOfWeekW },
{ LOCALE_IFIRSTWEEKOFYEAR, iFirstWeekOfYearW },
{ LOCALE_ILZERO, iLZeroW },
{ LOCALE_IMEASURE, iMeasureW },
{ LOCALE_INEGCURR, iNegCurrW },
{ LOCALE_INEGNUMBER, iNegNumberW },
{ LOCALE_IPAPERSIZE, iPaperSizeW },
{ LOCALE_ITIME, iTimeW },
{ LOCALE_S1159, s1159W },
{ LOCALE_S2359, s2359W },
{ LOCALE_SCURRENCY, sCurrencyW },
{ LOCALE_SDATE, sDateW },
{ LOCALE_SDECIMAL, sDecimalW },
{ LOCALE_SGROUPING, sGroupingW },
{ LOCALE_SLIST, sListW },
{ LOCALE_SLONGDATE, sLongDateW },
{ LOCALE_SMONDECIMALSEP, sMonDecimalSepW },
{ LOCALE_SMONGROUPING, sMonGroupingW },
{ LOCALE_SMONTHOUSANDSEP, sMonThousandSepW },
{ LOCALE_SNEGATIVESIGN, sNegativeSignW },
{ LOCALE_SPOSITIVESIGN, sPositiveSignW },
{ LOCALE_SSHORTDATE, sShortDateW },
{ LOCALE_STHOUSAND, sThousandW },
{ LOCALE_STIME, sTimeW },
{ LOCALE_STIMEFORMAT, sTimeFormatW },
{ LOCALE_SYEARMONTH, sYearMonthW },
/* The following are not listed under MSDN as supported,
* but seem to be used and also stored in the registry.
*/
{ LOCALE_ICOUNTRY, iCountryW },
{ LOCALE_IDATE, iDateW },
{ LOCALE_ILDATE, iLDateW },
{ LOCALE_ITLZERO, iTLZeroW },
{ LOCALE_SCOUNTRY, sCountryW },
{ LOCALE_SABBREVLANGNAME, sLanguageW },
/* The following are used in XP and later */
{ LOCALE_IDIGITSUBSTITUTION, NumShapeW },
{ LOCALE_SNATIVEDIGITS, sNativeDigitsW },
{ LOCALE_ITIMEMARKPOSN, iTimePrefixW }
};
static CRITICAL_SECTION cache_section;
static CRITICAL_SECTION_DEBUG critsect_debug =
{
0, 0, &cache_section,
{ &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
0, 0, { (DWORD_PTR)(__FILE__ ": cache_section") }
};
static CRITICAL_SECTION cache_section = { &critsect_debug, -1, 0, 0, 0, 0 };
/* Copy Ascii string to Unicode without using codepages */
static inline void strcpynAtoW( WCHAR *dst, const char *src, size_t n )
{
while (n > 1 && *src)
{
*dst++ = (unsigned char)*src++;
n--;
}
if (n) *dst = 0;
}
extern const unsigned short wctype_table[] DECLSPEC_HIDDEN;
extern const unsigned short nameprep_char_type[] DECLSPEC_HIDDEN;
extern const WCHAR nameprep_mapping[] DECLSPEC_HIDDEN;
static inline unsigned short get_table_entry( const unsigned short *table, WCHAR ch )
{
return table[table[table[ch >> 8] + ((ch >> 4) & 0x0f)] + (ch & 0xf)];
}
/***********************************************************************
* get_lcid_codepage
*
* Retrieve the ANSI codepage for a given locale.
*/
static inline UINT get_lcid_codepage( LCID lcid )
{
UINT ret;
if (!GetLocaleInfoW( lcid, LOCALE_IDEFAULTANSICODEPAGE|LOCALE_RETURN_NUMBER, (WCHAR *)&ret,
sizeof(ret)/sizeof(WCHAR) )) ret = 0;
return ret;
}
/***********************************************************************
* get_codepage_table
*
* Find the table for a given codepage, handling CP_ACP etc. pseudo-codepages
*/
static const union cptable *get_codepage_table( unsigned int codepage )
{
const union cptable *ret = NULL;
assert( ansi_cptable ); /* init must have been done already */
switch(codepage)
{
case CP_ACP:
return ansi_cptable;
case CP_OEMCP:
return oem_cptable;
case CP_MACCP:
return mac_cptable;
case CP_UTF7:
case CP_UTF8:
break;
case CP_THREAD_ACP:
if (NtCurrentTeb()->CurrentLocale == GetUserDefaultLCID()) return ansi_cptable;
codepage = get_lcid_codepage( NtCurrentTeb()->CurrentLocale );
if (!codepage) return ansi_cptable;
/* fall through */
default:
if (codepage == ansi_cptable->info.codepage) return ansi_cptable;
if (codepage == oem_cptable->info.codepage) return oem_cptable;
if (codepage == mac_cptable->info.codepage) return mac_cptable;
ret = wine_cp_get_table( codepage );
break;
}
return ret;
}
/***********************************************************************
* is_genitive_name_supported
*
* Determine could LCTYPE basically support genitive name form or not.
*/
static BOOL is_genitive_name_supported( LCTYPE lctype )
{
switch(lctype & 0xffff)
{
case LOCALE_SMONTHNAME1:
case LOCALE_SMONTHNAME2:
case LOCALE_SMONTHNAME3:
case LOCALE_SMONTHNAME4:
case LOCALE_SMONTHNAME5:
case LOCALE_SMONTHNAME6:
case LOCALE_SMONTHNAME7:
case LOCALE_SMONTHNAME8:
case LOCALE_SMONTHNAME9:
case LOCALE_SMONTHNAME10:
case LOCALE_SMONTHNAME11:
case LOCALE_SMONTHNAME12:
case LOCALE_SMONTHNAME13:
return TRUE;
default:
return FALSE;
}
}
/***********************************************************************
* create_registry_key
*
* Create the Control Panel\\International registry key.
*/
static inline HANDLE create_registry_key(void)
{
static const WCHAR cplW[] = {'C','o','n','t','r','o','l',' ','P','a','n','e','l',0};
static const WCHAR intlW[] = {'I','n','t','e','r','n','a','t','i','o','n','a','l',0};
OBJECT_ATTRIBUTES attr;
UNICODE_STRING nameW;
HANDLE cpl_key, hkey = 0;
if (RtlOpenCurrentUser( KEY_ALL_ACCESS, &hkey ) != STATUS_SUCCESS) return 0;
attr.Length = sizeof(attr);
attr.RootDirectory = hkey;
attr.ObjectName = &nameW;
attr.Attributes = 0;
attr.SecurityDescriptor = NULL;
attr.SecurityQualityOfService = NULL;
RtlInitUnicodeString( &nameW, cplW );
if (!NtCreateKey( &cpl_key, KEY_ALL_ACCESS, &attr, 0, NULL, 0, NULL ))
{
NtClose( attr.RootDirectory );
attr.RootDirectory = cpl_key;
RtlInitUnicodeString( &nameW, intlW );
if (NtCreateKey( &hkey, KEY_ALL_ACCESS, &attr, 0, NULL, 0, NULL )) hkey = 0;
}
NtClose( attr.RootDirectory );
return hkey;
}
static BOOL get_dummy_preferred_ui_language( DWORD flags, ULONG *count, WCHAR *buffer, ULONG *size )
{
LCTYPE type;
int lsize;
FIXME("(0x%x %p %p %p) returning a dummy value (current locale)\n", flags, count, buffer, size);
if (flags & MUI_LANGUAGE_ID)
type = LOCALE_ILANGUAGE;
else
type = LOCALE_SNAME;
lsize = GetLocaleInfoW(LOCALE_SYSTEM_DEFAULT, type, NULL, 0);
if (!lsize)
{
/* keep last error from callee */
return FALSE;
}
lsize++;
if (!*size)
{
*size = lsize;
*count = 1;
return TRUE;
}
if (lsize > *size)
{
SetLastError(ERROR_INSUFFICIENT_BUFFER);
return FALSE;
}
if (!GetLocaleInfoW(LOCALE_SYSTEM_DEFAULT, type, buffer, *size))
{
/* keep last error from callee */
return FALSE;
}
buffer[lsize-1] = 0;
*size = lsize;
*count = 1;
TRACE("returned variable content: %d, \"%s\", %d\n", *count, debugstr_w(buffer), *size);
return TRUE;
}
/***********************************************************************
* GetProcessPreferredUILanguages (KERNEL32.@)
*/
BOOL WINAPI GetProcessPreferredUILanguages( DWORD flags, ULONG *count, WCHAR *buf, ULONG *size )
{
FIXME( "%08x, %p, %p %p\n", flags, count, buf, size );
return get_dummy_preferred_ui_language( flags, count, buf, size );
}
/***********************************************************************
* GetSystemPreferredUILanguages (KERNEL32.@)
*/
BOOL WINAPI GetSystemPreferredUILanguages(DWORD flags, ULONG* count, WCHAR* buffer, ULONG* size)
{
if (flags & ~(MUI_LANGUAGE_NAME | MUI_LANGUAGE_ID | MUI_MACHINE_LANGUAGE_SETTINGS))
{
SetLastError(ERROR_INVALID_PARAMETER);
return FALSE;
}
if ((flags & MUI_LANGUAGE_NAME) && (flags & MUI_LANGUAGE_ID))
{
SetLastError(ERROR_INVALID_PARAMETER);
return FALSE;
}
if (*size && !buffer)
{
SetLastError(ERROR_INVALID_PARAMETER);
return FALSE;
}
return get_dummy_preferred_ui_language( flags, count, buffer, size );
}
/***********************************************************************
* SetProcessPreferredUILanguages (KERNEL32.@)
*/
BOOL WINAPI SetProcessPreferredUILanguages( DWORD flags, PCZZWSTR buffer, PULONG count )
{
FIXME("%u, %p, %p\n", flags, buffer, count );
return TRUE;
}
/***********************************************************************
* SetThreadPreferredUILanguages (KERNEL32.@)
*/
BOOL WINAPI SetThreadPreferredUILanguages( DWORD flags, PCZZWSTR buffer, PULONG count )
{
FIXME( "%u, %p, %p\n", flags, buffer, count );
return TRUE;
}
/***********************************************************************
* GetThreadPreferredUILanguages (KERNEL32.@)
*/
BOOL WINAPI GetThreadPreferredUILanguages( DWORD flags, ULONG *count, WCHAR *buf, ULONG *size )
{
FIXME( "%08x, %p, %p %p\n", flags, count, buf, size );
return get_dummy_preferred_ui_language( flags, count, buf, size );
}
/******************************************************************************
* GetUserPreferredUILanguages (KERNEL32.@)
*/
BOOL WINAPI GetUserPreferredUILanguages( DWORD flags, ULONG *count, WCHAR *buffer, ULONG *size )
{
TRACE( "%u %p %p %p\n", flags, count, buffer, size );
if (flags & ~(MUI_LANGUAGE_NAME | MUI_LANGUAGE_ID))
{
SetLastError(ERROR_INVALID_PARAMETER);
return FALSE;
}
if ((flags & MUI_LANGUAGE_NAME) && (flags & MUI_LANGUAGE_ID))
{
SetLastError(ERROR_INVALID_PARAMETER);
return FALSE;
}
if (*size && !buffer)
{
SetLastError(ERROR_INVALID_PARAMETER);
return FALSE;
}
return get_dummy_preferred_ui_language( flags, count, buffer, size );
}
/******************************************************************************
* get_locale_registry_value
*
* Gets the registry value name and cache for a given lctype.
*/
static struct registry_value *get_locale_registry_value( DWORD lctype )
{
int i;
for (i = 0; i < ARRAY_SIZE( registry_values ); i++)
if (registry_values[i].lctype == lctype)
return &registry_values[i];
return NULL;
}
/******************************************************************************
* get_registry_locale_info
*
* Retrieve user-modified locale info from the registry.
* Return length, 0 on error, -1 if not found.
*/
static INT get_registry_locale_info( struct registry_value *registry_value, LPWSTR buffer, INT len )
{
DWORD size;
INT ret;
HANDLE hkey;
NTSTATUS status;
UNICODE_STRING nameW;
KEY_VALUE_PARTIAL_INFORMATION *info;
static const int info_size = FIELD_OFFSET(KEY_VALUE_PARTIAL_INFORMATION, Data);
RtlEnterCriticalSection( &cache_section );
if (!registry_value->cached_value)
{
if (!(hkey = create_registry_key()))
{
RtlLeaveCriticalSection( &cache_section );
return -1;
}
RtlInitUnicodeString( &nameW, registry_value->name );
size = info_size + len * sizeof(WCHAR);
if (!(info = HeapAlloc( GetProcessHeap(), 0, size )))
{
NtClose( hkey );
SetLastError( ERROR_NOT_ENOUGH_MEMORY );
RtlLeaveCriticalSection( &cache_section );
return 0;
}
status = NtQueryValueKey( hkey, &nameW, KeyValuePartialInformation, info, size, &size );
/* try again with a bigger buffer when we have to return the correct size */
if (status == STATUS_BUFFER_OVERFLOW && !buffer && size > info_size)
{
KEY_VALUE_PARTIAL_INFORMATION *new_info;
if ((new_info = HeapReAlloc( GetProcessHeap(), 0, info, size )))
{
info = new_info;
status = NtQueryValueKey( hkey, &nameW, KeyValuePartialInformation, info, size, &size );
}
}
NtClose( hkey );
if (!status)
{
INT length = (size - info_size) / sizeof(WCHAR);
LPWSTR cached_value;
if (!length || ((WCHAR *)&info->Data)[length-1])
length++;
cached_value = HeapAlloc( GetProcessHeap(), 0, length * sizeof(WCHAR) );
if (!cached_value)
{
HeapFree( GetProcessHeap(), 0, info );
SetLastError( ERROR_NOT_ENOUGH_MEMORY );
RtlLeaveCriticalSection( &cache_section );
return 0;
}
memcpy( cached_value, info->Data, (length-1) * sizeof(WCHAR) );
cached_value[length-1] = 0;
HeapFree( GetProcessHeap(), 0, info );
registry_value->cached_value = cached_value;
}
else
{
if (status == STATUS_BUFFER_OVERFLOW && !buffer)
{
ret = (size - info_size) / sizeof(WCHAR);
}
else if (status == STATUS_OBJECT_NAME_NOT_FOUND)
{
ret = -1;
}
else
{
SetLastError( RtlNtStatusToDosError(status) );
ret = 0;
}
HeapFree( GetProcessHeap(), 0, info );
RtlLeaveCriticalSection( &cache_section );
return ret;
}
}
ret = lstrlenW( registry_value->cached_value ) + 1;
if (buffer)
{
if (ret > len)
{
SetLastError( ERROR_INSUFFICIENT_BUFFER );
ret = 0;
}
else
{
lstrcpyW( buffer, registry_value->cached_value );
}
}
RtlLeaveCriticalSection( &cache_section );
return ret;
}
static int get_value_base_by_lctype( LCTYPE lctype )
{
return lctype == LOCALE_ILANGUAGE || lctype == LOCALE_IDEFAULTLANGUAGE ? 16 : 10;
}
/******************************************************************************
* GetLocaleInfoW (KERNEL32.@)
*
* See GetLocaleInfoA.
*/
INT WINAPI GetLocaleInfoW( LCID lcid, LCTYPE lctype, LPWSTR buffer, INT len )
{
HRSRC hrsrc;
HGLOBAL hmem;
INT ret;
UINT lcflags;
const WCHAR *p;
unsigned int i;
if (len < 0 || (len && !buffer))
{
SetLastError( ERROR_INVALID_PARAMETER );
return 0;
}
if (lctype & LOCALE_RETURN_GENITIVE_NAMES &&
!is_genitive_name_supported( lctype ))
{
SetLastError( ERROR_INVALID_FLAGS );
return 0;
}
if (!len) buffer = NULL;
lcid = ConvertDefaultLocale( lcid );
lcflags = lctype & LOCALE_LOCALEINFOFLAGSMASK;
lctype &= 0xffff;
TRACE( "(lcid=0x%x,lctype=0x%x,%p,%d)\n", lcid, lctype, buffer, len );
/* first check for overrides in the registry */
if (!(lcflags & LOCALE_NOUSEROVERRIDE) && lcid == ConvertDefaultLocale( LOCALE_USER_DEFAULT ))
{
struct registry_value *value = get_locale_registry_value(lctype);
if (value)
{
if (lcflags & LOCALE_RETURN_NUMBER)
{
WCHAR tmp[16];
ret = get_registry_locale_info( value, tmp, ARRAY_SIZE( tmp ));
if (ret > 0)
{
WCHAR *end;
UINT number = strtolW( tmp, &end, get_value_base_by_lctype( lctype ) );
if (*end) /* invalid number */
{
SetLastError( ERROR_INVALID_FLAGS );
return 0;
}
ret = sizeof(UINT)/sizeof(WCHAR);
if (!buffer) return ret;
if (ret > len)
{
SetLastError( ERROR_INSUFFICIENT_BUFFER );
return 0;
}
memcpy( buffer, &number, sizeof(number) );
}
}
else ret = get_registry_locale_info( value, buffer, len );
if (ret != -1) return ret;
}
}
/* now load it from kernel resources */
if (!(hrsrc = FindResourceExW( kernel32_handle, (LPWSTR)RT_STRING,
ULongToPtr((lctype >> 4) + 1), lcid )))
{
SetLastError( ERROR_INVALID_FLAGS ); /* no such lctype */
return 0;
}
if (!(hmem = LoadResource( kernel32_handle, hrsrc )))
return 0;
p = LockResource( hmem );
for (i = 0; i < (lctype & 0x0f); i++) p += *p + 1;
if (lcflags & LOCALE_RETURN_NUMBER) ret = sizeof(UINT)/sizeof(WCHAR);
else if (is_genitive_name_supported( lctype ) && *p)
{
/* genitive form's stored after a null separator from a nominative */
for (i = 1; i <= *p; i++) if (!p[i]) break;
if (i <= *p && (lcflags & LOCALE_RETURN_GENITIVE_NAMES))
{
ret = *p - i + 1;
p += i;
}
else ret = i;
}
else
ret = (lctype == LOCALE_FONTSIGNATURE) ? *p : *p + 1;
if (!buffer) return ret;
if (ret > len)
{
SetLastError( ERROR_INSUFFICIENT_BUFFER );
return 0;
}
if (lcflags & LOCALE_RETURN_NUMBER)
{
UINT number;
WCHAR *end, *tmp = HeapAlloc( GetProcessHeap(), 0, (*p + 1) * sizeof(WCHAR) );
if (!tmp) return 0;
memcpy( tmp, p + 1, *p * sizeof(WCHAR) );
tmp[*p] = 0;
number = strtolW( tmp, &end, get_value_base_by_lctype( lctype ) );
if (!*end)
memcpy( buffer, &number, sizeof(number) );
else /* invalid number */
{
SetLastError( ERROR_INVALID_FLAGS );
ret = 0;
}
HeapFree( GetProcessHeap(), 0, tmp );
TRACE( "(lcid=0x%x,lctype=0x%x,%p,%d) returning number %d\n",
lcid, lctype, buffer, len, number );
}
else
{
memcpy( buffer, p + 1, ret * sizeof(WCHAR) );
if (lctype != LOCALE_FONTSIGNATURE) buffer[ret-1] = 0;
TRACE( "(lcid=0x%x,lctype=0x%x,%p,%d) returning %d %s\n",
lcid, lctype, buffer, len, ret, debugstr_w(buffer) );
}
return ret;
}
/******************************************************************************
* SetLocaleInfoA [KERNEL32.@]
*
* Set information about an aspect of a locale.
*
* PARAMS
* lcid [I] LCID of the locale
* lctype [I] LCTYPE_ flags from "winnls.h"
* data [I] Information to set
*
* RETURNS
* Success: TRUE. The information given will be returned by GetLocaleInfoA()
* whenever it is called without LOCALE_NOUSEROVERRIDE.
* Failure: FALSE. Use GetLastError() to determine the cause.
*
* NOTES
* - Values are only be set for the current user locale; the system locale
* settings cannot be changed.
* - Any settings changed by this call are lost when the locale is changed by
* the control panel (in Wine, this happens every time you change LANG).
* - The native implementation of this function does not check that lcid matches
* the current user locale, and simply sets the new values. Wine warns you in
* this case, but behaves the same.
*/
BOOL WINAPI SetLocaleInfoA(LCID lcid, LCTYPE lctype, LPCSTR data)
{
UINT codepage = CP_ACP;
WCHAR *strW;
DWORD len;
BOOL ret;
if (!(lctype & LOCALE_USE_CP_ACP)) codepage = get_lcid_codepage( lcid );
if (!data)
{
SetLastError( ERROR_INVALID_PARAMETER );
return FALSE;
}
len = MultiByteToWideChar( codepage, 0, data, -1, NULL, 0 );
if (!(strW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) )))
{
SetLastError( ERROR_NOT_ENOUGH_MEMORY );
return FALSE;
}
MultiByteToWideChar( codepage, 0, data, -1, strW, len );
ret = SetLocaleInfoW( lcid, lctype, strW );
HeapFree( GetProcessHeap(), 0, strW );
return ret;
}
/******************************************************************************
* SetLocaleInfoW (KERNEL32.@)
*
* See SetLocaleInfoA.
*/
BOOL WINAPI SetLocaleInfoW( LCID lcid, LCTYPE lctype, LPCWSTR data )
{
struct registry_value *value;
static const WCHAR intlW[] = {'i','n','t','l',0 };
UNICODE_STRING valueW;
NTSTATUS status;
HANDLE hkey;
lctype &= 0xffff;
value = get_locale_registry_value( lctype );
if (!data || !value)
{
SetLastError( ERROR_INVALID_PARAMETER );
return FALSE;
}
if (lctype == LOCALE_IDATE || lctype == LOCALE_ILDATE)
{
SetLastError( ERROR_INVALID_FLAGS );
return FALSE;
}
TRACE("setting %x (%s) to %s\n", lctype, debugstr_w(value->name), debugstr_w(data) );
/* FIXME: should check that data to set is sane */
/* FIXME: profile functions should map to registry */
WriteProfileStringW( intlW, value->name, data );
if (!(hkey = create_registry_key())) return FALSE;
RtlInitUnicodeString( &valueW, value->name );
status = NtSetValueKey( hkey, &valueW, 0, REG_SZ, data, (strlenW(data)+1)*sizeof(WCHAR) );
RtlEnterCriticalSection( &cache_section );
HeapFree( GetProcessHeap(), 0, value->cached_value );
value->cached_value = NULL;
RtlLeaveCriticalSection( &cache_section );
if (lctype == LOCALE_SSHORTDATE || lctype == LOCALE_SLONGDATE)
{
/* Set I-value from S value */
WCHAR *lpD, *lpM, *lpY;
WCHAR szBuff[2];
lpD = strrchrW(data, 'd');
lpM = strrchrW(data, 'M');
lpY = strrchrW(data, 'y');
if (lpD <= lpM)
{
szBuff[0] = '1'; /* D-M-Y */
}
else
{
if (lpY <= lpM)
szBuff[0] = '2'; /* Y-M-D */
else
szBuff[0] = '0'; /* M-D-Y */
}
szBuff[1] = '\0';
if (lctype == LOCALE_SSHORTDATE)
lctype = LOCALE_IDATE;
else
lctype = LOCALE_ILDATE;
value = get_locale_registry_value( lctype );
WriteProfileStringW( intlW, value->name, szBuff );
RtlInitUnicodeString( &valueW, value->name );
status = NtSetValueKey( hkey, &valueW, 0, REG_SZ, szBuff, sizeof(szBuff) );
RtlEnterCriticalSection( &cache_section );
HeapFree( GetProcessHeap(), 0, value->cached_value );
value->cached_value = NULL;
RtlLeaveCriticalSection( &cache_section );
}
NtClose( hkey );
return set_ntstatus( status );
}
/******************************************************************************
* SetCPGlobal (KERNEL32.@)
*
* Set the current Ansi code page Id for the system.
*
* PARAMS
* acp [I] code page ID to be the new ACP.
*
* RETURNS
* The previous ACP.
*/
UINT WINAPI SetCPGlobal( UINT acp )
{
UINT ret = GetACP();
const union cptable *new_cptable = wine_cp_get_table( acp );
if (new_cptable) ansi_cptable = new_cptable;
return ret;
}
/***********************************************************************
* IsValidCodePage (KERNEL32.@)
*
* Determine if a given code page identifier is valid.
*
* PARAMS
* codepage [I] Code page Id to verify.
*
* RETURNS
* TRUE, If codepage is valid and available on the system,
* FALSE otherwise.
*/
BOOL WINAPI IsValidCodePage( UINT codepage )
{
switch(codepage) {
case CP_UTF7:
case CP_UTF8:
return TRUE;
default:
return wine_cp_get_table( codepage ) != NULL;
}
}
/***********************************************************************
* IsDBCSLeadByteEx (KERNEL32.@)
*
* Determine if a character is a lead byte in a given code page.
*
* PARAMS
* codepage [I] Code page for the test.
* testchar [I] Character to test
*
* RETURNS
* TRUE, if testchar is a lead byte in codepage,
* FALSE otherwise.
*/
BOOL WINAPI IsDBCSLeadByteEx( UINT codepage, BYTE testchar )
{
const union cptable *table = get_codepage_table( codepage );
return table && wine_is_dbcs_leadbyte( table, testchar );
}
/***********************************************************************
* IsDBCSLeadByte (KERNEL32.@)
* IsDBCSLeadByte (KERNEL.207)
*
* Determine if a character is a lead byte.
*
* PARAMS
* testchar [I] Character to test
*
* RETURNS
* TRUE, if testchar is a lead byte in the ANSI code page,
* FALSE otherwise.
*/
BOOL WINAPI IsDBCSLeadByte( BYTE testchar )
{
if (!ansi_cptable) return FALSE;
return wine_is_dbcs_leadbyte( ansi_cptable, testchar );
}
/***********************************************************************
* GetCPInfo (KERNEL32.@)
*
* Get information about a code page.
*
* PARAMS
* codepage [I] Code page number
* cpinfo [O] Destination for code page information
*
* RETURNS
* Success: TRUE. cpinfo is updated with the information about codepage.
* Failure: FALSE, if codepage is invalid or cpinfo is NULL.
*/
BOOL WINAPI GetCPInfo( UINT codepage, LPCPINFO cpinfo )
{
const union cptable *table;
if (!cpinfo)
{
SetLastError( ERROR_INVALID_PARAMETER );
return FALSE;
}
if (!(table = get_codepage_table( codepage )))
{
switch(codepage)
{
case CP_UTF7:
case CP_UTF8:
cpinfo->DefaultChar[0] = 0x3f;
cpinfo->DefaultChar[1] = 0;
cpinfo->LeadByte[0] = cpinfo->LeadByte[1] = 0;
cpinfo->MaxCharSize = (codepage == CP_UTF7) ? 5 : 4;
return TRUE;
}
SetLastError( ERROR_INVALID_PARAMETER );
return FALSE;
}
if (table->info.def_char & 0xff00)
{
cpinfo->DefaultChar[0] = (table->info.def_char & 0xff00) >> 8;
cpinfo->DefaultChar[1] = table->info.def_char & 0x00ff;
}
else
{
cpinfo->DefaultChar[0] = table->info.def_char & 0xff;
cpinfo->DefaultChar[1] = 0;
}
if ((cpinfo->MaxCharSize = table->info.char_size) == 2)
memcpy( cpinfo->LeadByte, table->dbcs.lead_bytes, sizeof(cpinfo->LeadByte) );
else
cpinfo->LeadByte[0] = cpinfo->LeadByte[1] = 0;
return TRUE;
}
/***********************************************************************
* GetCPInfoExA (KERNEL32.@)
*
* Get extended information about a code page.
*
* PARAMS
* codepage [I] Code page number
* dwFlags [I] Reserved, must to 0.
* cpinfo [O] Destination for code page information
*
* RETURNS
* Success: TRUE. cpinfo is updated with the information about codepage.
* Failure: FALSE, if codepage is invalid or cpinfo is NULL.
*/
BOOL WINAPI GetCPInfoExA( UINT codepage, DWORD dwFlags, LPCPINFOEXA cpinfo )
{
CPINFOEXW cpinfoW;
if (!GetCPInfoExW( codepage, dwFlags, &cpinfoW ))
return FALSE;
/* the layout is the same except for CodePageName */
memcpy(cpinfo, &cpinfoW, sizeof(CPINFOEXA));
WideCharToMultiByte(CP_ACP, 0, cpinfoW.CodePageName, -1, cpinfo->CodePageName, sizeof(cpinfo->CodePageName), NULL, NULL);
return TRUE;
}
/***********************************************************************
* GetCPInfoExW (KERNEL32.@)
*
* Unicode version of GetCPInfoExA.
*/
BOOL WINAPI GetCPInfoExW( UINT codepage, DWORD dwFlags, LPCPINFOEXW cpinfo )
{
if (!GetCPInfo( codepage, (LPCPINFO)cpinfo ))
return FALSE;
switch(codepage)
{
case CP_UTF7:
{
static const WCHAR utf7[] = {'U','n','i','c','o','d','e',' ','(','U','T','F','-','7',')',0};
cpinfo->CodePage = CP_UTF7;
cpinfo->UnicodeDefaultChar = 0x3f;
strcpyW(cpinfo->CodePageName, utf7);
break;
}
case CP_UTF8:
{
static const WCHAR utf8[] = {'U','n','i','c','o','d','e',' ','(','U','T','F','-','8',')',0};
cpinfo->CodePage = CP_UTF8;
cpinfo->UnicodeDefaultChar = 0x3f;
strcpyW(cpinfo->CodePageName, utf8);
break;
}
default:
{
const union cptable *table = get_codepage_table( codepage );
cpinfo->CodePage = table->info.codepage;
cpinfo->UnicodeDefaultChar = table->info.def_unicode_char;
MultiByteToWideChar( CP_ACP, 0, table->info.name, -1, cpinfo->CodePageName,
ARRAY_SIZE( cpinfo->CodePageName ));
break;
}
}
return TRUE;
}
/***********************************************************************
* EnumSystemCodePagesA (KERNEL32.@)
*/
BOOL WINAPI EnumSystemCodePagesA( CODEPAGE_ENUMPROCA proc, DWORD flags )
{
return Internal_EnumSystemCodePages( (CODEPAGE_ENUMPROCW)proc, flags, FALSE );
}
/***********************************************************************
* utf7_write_w
*
* Helper for utf7_mbstowcs
*
* RETURNS
* TRUE on success, FALSE on error
*/
static inline BOOL utf7_write_w(WCHAR *dst, int dstlen, int *index, WCHAR character)
{
if (dstlen > 0)
{
if (*index >= dstlen)
return FALSE;
dst[*index] = character;
}
(*index)++;
return TRUE;
}
/***********************************************************************
* utf7_mbstowcs
*
* UTF-7 to UTF-16 string conversion, helper for MultiByteToWideChar
*
* RETURNS
* On success, the number of characters written
* On dst buffer overflow, -1
*/
static int utf7_mbstowcs(const char *src, int srclen, WCHAR *dst, int dstlen)
{
static const signed char base64_decoding_table[] =
{
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x00-0x0F */
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x10-0x1F */
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63, /* 0x20-0x2F */
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, /* 0x30-0x3F */
-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, /* 0x40-0x4F */
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, /* 0x50-0x5F */
-1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, /* 0x60-0x6F */
41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1 /* 0x70-0x7F */
};
const char *source_end = src + srclen;
int dest_index = 0;
DWORD byte_pair = 0;
short offset = 0;
while (src < source_end)
{
if (*src == '+')
{
src++;
if (src >= source_end)
break;
if (*src == '-')
{
/* just a plus sign escaped as +- */
if (!utf7_write_w(dst, dstlen, &dest_index, '+'))
return -1;
src++;
continue;
}
do
{
signed char sextet = *src;
if (sextet == '-')
{
/* skip over the dash and end base64 decoding
* the current, unfinished byte pair is discarded */
src++;
offset = 0;
break;
}
if (sextet < 0)
{
/* the next character of src is < 0 and therefore not part of a base64 sequence
* the current, unfinished byte pair is NOT discarded in this case
* this is probably a bug in Windows */
break;
}
sextet = base64_decoding_table[sextet];
if (sextet == -1)
{
/* -1 means that the next character of src is not part of a base64 sequence
* in other words, all sextets in this base64 sequence have been processed
* the current, unfinished byte pair is discarded */
offset = 0;
break;
}
byte_pair = (byte_pair << 6) | sextet;
offset += 6;
if (offset >= 16)
{
/* this byte pair is done */
if (!utf7_write_w(dst, dstlen, &dest_index, (byte_pair >> (offset - 16)) & 0xFFFF))
return -1;
offset -= 16;
}
src++;
}
while (src < source_end);
}
else
{
/* we have to convert to unsigned char in case *src < 0 */
if (!utf7_write_w(dst, dstlen, &dest_index, (unsigned char)*src))
return -1;
src++;
}
}
return dest_index;
}
/***********************************************************************
* MultiByteToWideChar (KERNEL32.@)
*
* Convert a multibyte character string into a Unicode string.
*
* PARAMS
* page [I] Codepage character set to convert from
* flags [I] Character mapping flags
* src [I] Source string buffer
* srclen [I] Length of src (in bytes), or -1 if src is NUL terminated
* dst [O] Destination buffer
* dstlen [I] Length of dst (in WCHARs), or 0 to compute the required length
*
* RETURNS
* Success: If dstlen > 0, the number of characters written to dst.
* If dstlen == 0, the number of characters needed to perform the
* conversion. In both cases the count includes the terminating NUL.
* Failure: 0. Use GetLastError() to determine the cause. Possible errors are
* ERROR_INSUFFICIENT_BUFFER, if not enough space is available in dst
* and dstlen != 0; ERROR_INVALID_PARAMETER, if an invalid parameter
* is passed, and ERROR_NO_UNICODE_TRANSLATION if no translation is
* possible for src.
*/
INT WINAPI MultiByteToWideChar( UINT page, DWORD flags, LPCSTR src, INT srclen,
LPWSTR dst, INT dstlen )
{
const union cptable *table;
int ret;
if (!src || !srclen || (!dst && dstlen) || dstlen < 0)
{
SetLastError( ERROR_INVALID_PARAMETER );
return 0;
}
if (srclen < 0) srclen = strlen(src) + 1;
switch(page)
{
case CP_SYMBOL:
if (flags)
{
SetLastError( ERROR_INVALID_FLAGS );
return 0;
}
ret = wine_cpsymbol_mbstowcs( src, srclen, dst, dstlen );
break;
case CP_UTF7:
if (flags)
{
SetLastError( ERROR_INVALID_FLAGS );
return 0;
}
ret = utf7_mbstowcs( src, srclen, dst, dstlen );
break;
case CP_UNIXCP:
if (unix_cptable)
{
ret = wine_cp_mbstowcs( unix_cptable, flags, src, srclen, dst, dstlen );
break;
}
#ifdef __APPLE__
flags |= MB_COMPOSITE; /* work around broken Mac OS X filesystem that enforces decomposed Unicode */
#endif
/* fall through */
case CP_UTF8:
if (flags & ~MB_FLAGSMASK)
{
SetLastError( ERROR_INVALID_FLAGS );
return 0;
}
ret = wine_utf8_mbstowcs( flags, src, srclen, dst, dstlen );
break;
default:
if (!(table = get_codepage_table( page )))
{
SetLastError( ERROR_INVALID_PARAMETER );
return 0;
}
if (flags & ~MB_FLAGSMASK)
{
SetLastError( ERROR_INVALID_FLAGS );
return 0;
}
ret = wine_cp_mbstowcs( table, flags, src, srclen, dst, dstlen );
break;
}
if (ret < 0)
{
switch(ret)
{
case -1: SetLastError( ERROR_INSUFFICIENT_BUFFER ); break;
case -2: SetLastError( ERROR_NO_UNICODE_TRANSLATION ); break;
}
ret = 0;
}
TRACE("cp %d %s -> %s, ret = %d\n",
page, debugstr_an(src, srclen), debugstr_wn(dst, ret), ret);
return ret;
}
/***********************************************************************
* utf7_can_directly_encode
*
* Helper for utf7_wcstombs
*/
static inline BOOL utf7_can_directly_encode(WCHAR codepoint)
{
static const BOOL directly_encodable_table[] =
{
1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, /* 0x00 - 0x0F */
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x10 - 0x1F */
1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, /* 0x20 - 0x2F */
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, /* 0x30 - 0x3F */
0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x40 - 0x4F */
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, /* 0x50 - 0x5F */
0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x60 - 0x6F */
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 /* 0x70 - 0x7A */
};
return codepoint <= 0x7A ? directly_encodable_table[codepoint] : FALSE;
}
/***********************************************************************
* utf7_write_c
*
* Helper for utf7_wcstombs
*
* RETURNS
* TRUE on success, FALSE on error
*/
static inline BOOL utf7_write_c(char *dst, int dstlen, int *index, char character)
{
if (dstlen > 0)
{
if (*index >= dstlen)
return FALSE;
dst[*index] = character;
}
(*index)++;
return TRUE;
}
/***********************************************************************
* utf7_wcstombs
*
* UTF-16 to UTF-7 string conversion, helper for WideCharToMultiByte
*
* RETURNS
* On success, the number of characters written
* On dst buffer overflow, -1
*/
static int utf7_wcstombs(const WCHAR *src, int srclen, char *dst, int dstlen)
{
static const char base64_encoding_table[] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
const WCHAR *source_end = src + srclen;
int dest_index = 0;
while (src < source_end)
{
if (*src == '+')
{
if (!utf7_write_c(dst, dstlen, &dest_index, '+'))
return -1;
if (!utf7_write_c(dst, dstlen, &dest_index, '-'))
return -1;
src++;
}
else if (utf7_can_directly_encode(*src))
{
if (!utf7_write_c(dst, dstlen, &dest_index, *src))
return -1;
src++;
}
else
{
unsigned int offset = 0;
DWORD byte_pair = 0;
if (!utf7_write_c(dst, dstlen, &dest_index, '+'))
return -1;
while (src < source_end && !utf7_can_directly_encode(*src))
{
byte_pair = (byte_pair << 16) | *src;
offset += 16;
while (offset >= 6)
{
if (!utf7_write_c(dst, dstlen, &dest_index, base64_encoding_table[(byte_pair >> (offset - 6)) & 0x3F]))
return -1;
offset -= 6;
}
src++;
}
if (offset)
{
/* Windows won't create a padded base64 character if there's no room for the - sign
* as well ; this is probably a bug in Windows */
if (dstlen > 0 && dest_index + 1 >= dstlen)
return -1;
byte_pair <<= (6 - offset);
if (!utf7_write_c(dst, dstlen, &dest_index, base64_encoding_table[byte_pair & 0x3F]))
return -1;
}
/* Windows always explicitly terminates the base64 sequence
even though RFC 2152 (page 3, rule 2) does not require this */
if (!utf7_write_c(dst, dstlen, &dest_index, '-'))
return -1;
}
}
return dest_index;
}
/***********************************************************************
* WideCharToMultiByte (KERNEL32.@)
*
* Convert a Unicode character string into a multibyte string.
*
* PARAMS
* page [I] Code page character set to convert to
* flags [I] Mapping Flags (MB_ constants from "winnls.h").
* src [I] Source string buffer
* srclen [I] Length of src (in WCHARs), or -1 if src is NUL terminated
* dst [O] Destination buffer
* dstlen [I] Length of dst (in bytes), or 0 to compute the required length
* defchar [I] Default character to use for conversion if no exact
* conversion can be made
* used [O] Set if default character was used in the conversion
*
* RETURNS
* Success: If dstlen > 0, the number of characters written to dst.
* If dstlen == 0, number of characters needed to perform the
* conversion. In both cases the count includes the terminating NUL.
* Failure: 0. Use GetLastError() to determine the cause. Possible errors are
* ERROR_INSUFFICIENT_BUFFER, if not enough space is available in dst
* and dstlen != 0, and ERROR_INVALID_PARAMETER, if an invalid
* parameter was given.
*/
INT WINAPI WideCharToMultiByte( UINT page, DWORD flags, LPCWSTR src, INT srclen,
LPSTR dst, INT dstlen, LPCSTR defchar, BOOL *used )
{
const union cptable *table;
int ret, used_tmp;
if (!src || !srclen || (!dst && dstlen) || dstlen < 0)
{
SetLastError( ERROR_INVALID_PARAMETER );
return 0;
}
if (srclen < 0) srclen = strlenW(src) + 1;
switch(page)
{
case CP_SYMBOL:
/* when using CP_SYMBOL, ERROR_INVALID_FLAGS takes precedence */
if (flags)
{
SetLastError( ERROR_INVALID_FLAGS );
return 0;
}
if (defchar || used)
{
SetLastError( ERROR_INVALID_PARAMETER );
return 0;
}
ret = wine_cpsymbol_wcstombs( src, srclen, dst, dstlen );
break;
case CP_UTF7:
/* when using CP_UTF7, ERROR_INVALID_PARAMETER takes precedence */
if (defchar || used)
{
SetLastError( ERROR_INVALID_PARAMETER );
return 0;
}
if (flags)
{
SetLastError( ERROR_INVALID_FLAGS );
return 0;
}
ret = utf7_wcstombs( src, srclen, dst, dstlen );
break;
case CP_UNIXCP:
if (unix_cptable)
{
ret = wine_cp_wcstombs( unix_cptable, flags, src, srclen, dst, dstlen,
defchar, used ? &used_tmp : NULL );
if (used) *used = used_tmp;
}
else
{
ret = wine_utf8_wcstombs( flags, src, srclen, dst, dstlen );
if (used) *used = FALSE;
}
break;
case CP_UTF8:
if (defchar || used)
{
SetLastError( ERROR_INVALID_PARAMETER );
return 0;
}
if (flags & ~WC_FLAGSMASK)
{
SetLastError( ERROR_INVALID_FLAGS );
return 0;
}
ret = wine_utf8_wcstombs( flags, src, srclen, dst, dstlen );
break;
default:
if (!(table = get_codepage_table( page )))
{
SetLastError( ERROR_INVALID_PARAMETER );
return 0;
}
if (flags & ~WC_FLAGSMASK)
{
SetLastError( ERROR_INVALID_FLAGS );
return 0;
}
ret = wine_cp_wcstombs( table, flags, src, srclen, dst, dstlen,
defchar, used ? &used_tmp : NULL );
if (used) *used = used_tmp;
break;
}
if (ret < 0)
{
switch(ret)
{
case -1: SetLastError( ERROR_INSUFFICIENT_BUFFER ); break;
case -2: SetLastError( ERROR_NO_UNICODE_TRANSLATION ); break;
}
ret = 0;
}
TRACE("cp %d %s -> %s, ret = %d\n",
page, debugstr_wn(src, srclen), debugstr_an(dst, ret), ret);
return ret;
}
/******************************************************************************
* GetStringTypeW (KERNEL32.@)
*
* See GetStringTypeA.
*/
BOOL WINAPI GetStringTypeW( DWORD type, LPCWSTR src, INT count, LPWORD chartype )
{
if (!src)
{
SetLastError( ERROR_INVALID_PARAMETER );
return FALSE;
}
if (count == -1) count = strlenW(src) + 1;
switch(type)
{
case CT_CTYPE1:
while (count--) *chartype++ = get_table_entry( wctype_table, *src++ ) & 0xfff;
break;
case CT_CTYPE2:
while (count--) *chartype++ = get_table_entry( wctype_table, *src++ ) >> 12;
break;
case CT_CTYPE3:
{
WARN("CT_CTYPE3: semi-stub.\n");
while (count--)
{
int c = *src;
WORD type1, type3 = 0; /* C3_NOTAPPLICABLE */
type1 = get_table_entry( wctype_table, *src++ ) & 0xfff;
/* try to construct type3 from type1 */
if(type1 & C1_SPACE) type3 |= C3_SYMBOL;
if(type1 & C1_ALPHA) type3 |= C3_ALPHA;
if ((c>=0x30A0)&&(c<=0x30FF)) type3 |= C3_KATAKANA;
if ((c>=0x3040)&&(c<=0x309F)) type3 |= C3_HIRAGANA;
if ((c>=0x4E00)&&(c<=0x9FAF)) type3 |= C3_IDEOGRAPH;
if (c == 0x0640) type3 |= C3_KASHIDA;
if ((c>=0x3000)&&(c<=0x303F)) type3 |= C3_SYMBOL;
if ((c>=0xD800)&&(c<=0xDBFF)) type3 |= C3_HIGHSURROGATE;
if ((c>=0xDC00)&&(c<=0xDFFF)) type3 |= C3_LOWSURROGATE;
if ((c>=0xFF00)&&(c<=0xFF60)) type3 |= C3_FULLWIDTH;
if ((c>=0xFF00)&&(c<=0xFF20)) type3 |= C3_SYMBOL;
if ((c>=0xFF3B)&&(c<=0xFF40)) type3 |= C3_SYMBOL;
if ((c>=0xFF5B)&&(c<=0xFF60)) type3 |= C3_SYMBOL;
if ((c>=0xFF21)&&(c<=0xFF3A)) type3 |= C3_ALPHA;
if ((c>=0xFF41)&&(c<=0xFF5A)) type3 |= C3_ALPHA;
if ((c>=0xFFE0)&&(c<=0xFFE6)) type3 |= C3_FULLWIDTH;
if ((c>=0xFFE0)&&(c<=0xFFE6)) type3 |= C3_SYMBOL;
if ((c>=0xFF61)&&(c<=0xFFDC)) type3 |= C3_HALFWIDTH;
if ((c>=0xFF61)&&(c<=0xFF64)) type3 |= C3_SYMBOL;
if ((c>=0xFF65)&&(c<=0xFF9F)) type3 |= C3_KATAKANA;
if ((c>=0xFF65)&&(c<=0xFF9F)) type3 |= C3_ALPHA;
if ((c>=0xFFE8)&&(c<=0xFFEE)) type3 |= C3_HALFWIDTH;
if ((c>=0xFFE8)&&(c<=0xFFEE)) type3 |= C3_SYMBOL;
*chartype++ = type3;
}
break;
}
default:
SetLastError( ERROR_INVALID_PARAMETER );
return FALSE;
}
return TRUE;
}
/******************************************************************************
* GetStringTypeExW (KERNEL32.@)
*
* See GetStringTypeExA.
*/
BOOL WINAPI GetStringTypeExW( LCID locale, DWORD type, LPCWSTR src, INT count, LPWORD chartype )
{
/* locale is ignored for Unicode */
return GetStringTypeW( type, src, count, chartype );
}
/******************************************************************************
* GetStringTypeA (KERNEL32.@)
*
* Get characteristics of the characters making up a string.
*
* PARAMS
* locale [I] Locale Id for the string
* type [I] CT_CTYPE1 = classification, CT_CTYPE2 = directionality, CT_CTYPE3 = typographic info
* src [I] String to analyse
* count [I] Length of src in chars, or -1 if src is NUL terminated
* chartype [O] Destination for the calculated characteristics
*
* RETURNS
* Success: TRUE. chartype is filled with the requested characteristics of each char
* in src.
* Failure: FALSE. Use GetLastError() to determine the cause.
*/
BOOL WINAPI GetStringTypeA( LCID locale, DWORD type, LPCSTR src, INT count, LPWORD chartype )
{
UINT cp;
INT countW;
LPWSTR srcW;
BOOL ret = FALSE;
if(count == -1) count = strlen(src) + 1;
if (!(cp = get_lcid_codepage( locale )))
{
FIXME("For locale %04x using current ANSI code page\n", locale);
cp = GetACP();
}
countW = MultiByteToWideChar(cp, 0, src, count, NULL, 0);
if((srcW = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR))))
{
MultiByteToWideChar(cp, 0, src, count, srcW, countW);
/*
* NOTE: the target buffer has 1 word for each CHARACTER in the source
* string, with multibyte characters there maybe be more bytes in count
* than character space in the buffer!
*/
ret = GetStringTypeW(type, srcW, countW, chartype);
HeapFree(GetProcessHeap(), 0, srcW);
}
return ret;
}
/******************************************************************************
* GetStringTypeExA (KERNEL32.@)
*
* Get characteristics of the characters making up a string.
*
* PARAMS
* locale [I] Locale Id for the string
* type [I] CT_CTYPE1 = classification, CT_CTYPE2 = directionality, CT_CTYPE3 = typographic info
* src [I] String to analyse
* count [I] Length of src in chars, or -1 if src is NUL terminated
* chartype [O] Destination for the calculated characteristics
*
* RETURNS
* Success: TRUE. chartype is filled with the requested characteristics of each char
* in src.
* Failure: FALSE. Use GetLastError() to determine the cause.
*/
BOOL WINAPI GetStringTypeExA( LCID locale, DWORD type, LPCSTR src, INT count, LPWORD chartype )
{
return GetStringTypeA(locale, type, src, count, chartype);
}
/* compose a full-width katakana. return consumed source characters. */
static INT compose_katakana( LPCWSTR src, INT srclen, LPWSTR dst )
{
const static BYTE katakana_map[] = {
/* */ 0x02, 0x0c, 0x0d, 0x01, 0xfb, 0xf2, 0xa1, /* U+FF61- */
0xa3, 0xa5, 0xa7, 0xa9, 0xe3, 0xe5, 0xe7, 0xc3, /* U+FF68- */
0xfc, 0xa2, 0xa4, 0xa6, 0xa8, 0xaa, 0xab, 0xad, /* U+FF70- */
0xaf, 0xb1, 0xb3, 0xb5, 0xb7, 0xb9, 0xbb, 0xbd, /* U+FF78- */
0xbf, 0xc1, 0xc4, 0xc6, 0xc8, 0xca, 0xcb, 0xcc, /* U+FF80- */
0xcd, 0xce, 0xcf, 0xd2, 0xd5, 0xd8, 0xdb, 0xde, /* U+FF88- */
0xdf, 0xe0, 0xe1, 0xe2, 0xe4, 0xe6, 0xe8, 0xe9, /* U+FF90- */
0xea, 0xeb, 0xec, 0xed, 0xef, 0xf3, 0x99, 0x9a, /* U+FF98- */
};
WCHAR before, dummy;
if (!dst)
dst = &dummy;
switch (*src)
{
case 0x309b: case 0x309c:
*dst = *src - 2;
return 1;
case 0x30f0: case 0x30f1: case 0x30fd:
*dst = *src;
break;
default:
{
int shift = *src - 0xff61;
if (shift < 0 || shift >= ARRAY_SIZE( katakana_map ))
return 0;
else
*dst = katakana_map[shift] | 0x3000;
}
}
if (srclen <= 1)
return 1;
before = *dst;
/* datakuten (voiced sound) */
if (*(src + 1) == 0xff9e)
{
if ((*src >= 0xff76 && *src <= 0xff84) ||
(*src >= 0xff8a && *src <= 0xff8e) ||
*src == 0x30fd)
*dst += 1;
else if (*src == 0xff73)
*dst = 0x30f4; /* KATAKANA LETTER VU */
else if (*src == 0xff9c)
*dst = 0x30f7; /* KATAKANA LETTER VA */
else if (*src == 0x30f0)
*dst = 0x30f8; /* KATAKANA LETTER VI */
else if (*src == 0x30f1)
*dst = 0x30f9; /* KATAKANA LETTER VE */
else if (*src == 0xff66)
*dst = 0x30fa; /* KATAKANA LETTER VO */
}
/* handakuten (semi-voiced sound) */
if (*(src + 1) == 0xff9f)
if (*src >= 0xff8a && *src <= 0xff8e)
*dst += 2;
return (*dst != before) ? 2 : 1;
}
/* map one or two half-width characters to one full-width character */
static INT map_to_fullwidth( LPCWSTR src, INT srclen, LPWSTR dst )
{
INT n;
if (*src <= '~' && *src > ' ' && *src != '\\')
*dst = *src - 0x20 + 0xff00;
else if (*src == ' ')
*dst = 0x3000;
else if (*src <= 0x00af && *src >= 0x00a2)
{
const static BYTE misc_symbols_table[] = {
0xe0, 0xe1, 0x00, 0xe5, 0xe4, 0x00, 0x00, /* U+00A2- */
0x00, 0x00, 0x00, 0xe2, 0x00, 0x00, 0xe3 /* U+00A9- */
};
if (misc_symbols_table[*src - 0x00a2])
*dst = misc_symbols_table[*src - 0x00a2] | 0xff00;
else
*dst = *src;
}
else if (*src == 0x20a9) /* WON SIGN */
*dst = 0xffe6;
else if ((n = compose_katakana(src, srclen, dst)) > 0)
return n;
else if (*src >= 0xffa0 && *src <= 0xffdc)
{
const static BYTE hangul_mapping_table[] = {
0x64, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, /* U+FFA0- */
0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, /* U+FFA8- */
0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, /* U+FFB0- */
0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x00, /* U+FFB8- */
0x00, 0x00, 0x4f, 0x50, 0x51, 0x52, 0x53, 0x54, /* U+FFC0- */
0x00, 0x00, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, /* U+FFC8- */
0x00, 0x00, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f, 0x60, /* U+FFD0- */
0x00, 0x00, 0x61, 0x62, 0x63 /* U+FFD8- */
};
if (hangul_mapping_table[*src - 0xffa0])
*dst = hangul_mapping_table[*src - 0xffa0] | 0x3100;
else
*dst = *src;
}
else
*dst = *src;
return 1;
}
/* decompose a full-width katakana character into one or two half-width characters. */
static INT decompose_katakana( WCHAR c, LPWSTR dst, INT dstlen )
{
const static BYTE katakana_map[] = {
/* */ 0x9e, 0x9f, 0x9e, 0x9f, 0x00, 0x00, 0x00, /* U+3099- */
0x00, 0x67, 0x71, 0x68, 0x72, 0x69, 0x73, 0x6a, /* U+30a1- */
0x74, 0x6b, 0x75, 0x76, 0x01, 0x77, 0x01, 0x78, /* U+30a8- */
0x01, 0x79, 0x01, 0x7a, 0x01, 0x7b, 0x01, 0x7c, /* U+30b0- */
0x01, 0x7d, 0x01, 0x7e, 0x01, 0x7f, 0x01, 0x80, /* U+30b8- */
0x01, 0x81, 0x01, 0x6f, 0x82, 0x01, 0x83, 0x01, /* U+30c0- */
0x84, 0x01, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, /* U+30c8- */
0x01, 0x02, 0x8b, 0x01, 0x02, 0x8c, 0x01, 0x02, /* U+30d0- */
0x8d, 0x01, 0x02, 0x8e, 0x01, 0x02, 0x8f, 0x90, /* U+30d8- */
0x91, 0x92, 0x93, 0x6c, 0x94, 0x6d, 0x95, 0x6e, /* U+30e0- */
0x96, 0x97, 0x98, 0x99, 0x9a, 0x9b, 0x00, 0x9c, /* U+30e8- */
0x00, 0x00, 0x66, 0x9d, 0x4e, 0x00, 0x00, 0x08, /* U+30f0- */
0x58, 0x58, 0x08, 0x65, 0x70, 0x00, 0x51 /* U+30f8- */
};
INT len = 0, shift = c - 0x3099;
BYTE k;
if (shift < 0 || shift >= ARRAY_SIZE( katakana_map ))
return 0;
k = katakana_map[shift];
if (!k)
{
if (dstlen > 0)
*dst = c;
len++;
}
else if (k > 0x60)
{
if (dstlen > 0)
*dst = k | 0xff00;
len++;
}
else
{
if (dstlen >= 2)
{
dst[0] = (k > 0x50) ? (c - (k & 0xf)) : (katakana_map[shift - k] | 0xff00);
dst[1] = (k == 2) ? 0xff9f : 0xff9e;
}
len += 2;
}
return len;
}
/* map single full-width character to single or double half-width characters. */
static INT map_to_halfwidth(WCHAR c, LPWSTR dst, INT dstlen)
{
INT n = decompose_katakana(c, dst, dstlen);
if (n > 0)
return n;
if (c == 0x3000)
*dst = ' ';
else if (c == 0x3001)
*dst = 0xff64;
else if (c == 0x3002)
*dst = 0xff61;
else if (c == 0x300c || c == 0x300d)
*dst = (c - 0x300c) + 0xff62;
else if (c >= 0x3131 && c <= 0x3163)
{
*dst = c - 0x3131 + 0xffa1;
if (*dst >= 0xffbf) *dst += 3;
if (*dst >= 0xffc8) *dst += 2;
if (*dst >= 0xffd0) *dst += 2;
if (*dst >= 0xffd8) *dst += 2;
}
else if (c == 0x3164)
*dst = 0xffa0;
else if (c == 0x2019)
*dst = '\'';
else if (c == 0x201d)
*dst = '"';
else if (c > 0xff00 && c < 0xff5f && c != 0xff3c)
*dst = c - 0xff00 + 0x20;
else if (c >= 0xffe0 && c <= 0xffe6)
{
const static WCHAR misc_symbol_map[] = {
0x00a2, 0x00a3, 0x00ac, 0x00af, 0x00a6, 0x00a5, 0x20a9
};
*dst = misc_symbol_map[c - 0xffe0];
}
else
*dst = c;
return 1;
}
/*************************************************************************
* LCMapStringEx (KERNEL32.@)
*
* Map characters in a locale sensitive string.
*
* PARAMS
* name [I] Locale name for the conversion.
* flags [I] Flags controlling the mapping (LCMAP_ constants from "winnls.h")
* src [I] String to map
* srclen [I] Length of src in chars, or -1 if src is NUL terminated
* dst [O] Destination for mapped string
* dstlen [I] Length of dst in characters
* version [I] reserved, must be NULL
* reserved [I] reserved, must be NULL
* lparam [I] reserved, must be 0
*
* RETURNS
* Success: The length of the mapped string in dst, including the NUL terminator.
* Failure: 0. Use GetLastError() to determine the cause.
*/
INT WINAPI LCMapStringEx(LPCWSTR name, DWORD flags, LPCWSTR src, INT srclen, LPWSTR dst, INT dstlen,
LPNLSVERSIONINFO version, LPVOID reserved, LPARAM lparam)
{
LPWSTR dst_ptr;
INT len;
if (version) FIXME("unsupported version structure %p\n", version);
if (reserved) FIXME("unsupported reserved pointer %p\n", reserved);
if (lparam)
{
static int once;
if (!once++) FIXME("unsupported lparam %lx\n", lparam);
}
if (!src || !srclen || dstlen < 0)
{
SetLastError(ERROR_INVALID_PARAMETER);
return 0;
}
/* mutually exclusive flags */
if ((flags & (LCMAP_LOWERCASE | LCMAP_UPPERCASE)) == (LCMAP_LOWERCASE | LCMAP_UPPERCASE) ||
(flags & (LCMAP_HIRAGANA | LCMAP_KATAKANA)) == (LCMAP_HIRAGANA | LCMAP_KATAKANA) ||
(flags & (LCMAP_HALFWIDTH | LCMAP_FULLWIDTH)) == (LCMAP_HALFWIDTH | LCMAP_FULLWIDTH) ||
(flags & (LCMAP_TRADITIONAL_CHINESE | LCMAP_SIMPLIFIED_CHINESE)) == (LCMAP_TRADITIONAL_CHINESE | LCMAP_SIMPLIFIED_CHINESE) ||
!flags)
{
SetLastError(ERROR_INVALID_FLAGS);
return 0;
}
if (!dstlen) dst = NULL;
if (flags & LCMAP_SORTKEY)
{
INT ret;
if (src == dst)
{
SetLastError(ERROR_INVALID_FLAGS);
return 0;
}
if (srclen < 0) srclen = strlenW(src);
TRACE("(%s,0x%08x,%s,%d,%p,%d)\n",
debugstr_w(name), flags, debugstr_wn(src, srclen), srclen, dst, dstlen);
ret = wine_get_sortkey(flags, src, srclen, (char *)dst, dstlen);
if (ret == 0)
SetLastError(ERROR_INSUFFICIENT_BUFFER);
else
ret++;
return ret;
}
/* SORT_STRINGSORT must be used exclusively with LCMAP_SORTKEY */
if (flags & SORT_STRINGSORT)
{
SetLastError(ERROR_INVALID_FLAGS);
return 0;
}
if (((flags & (NORM_IGNORENONSPACE | NORM_IGNORESYMBOLS)) &&
(flags & ~(NORM_IGNORENONSPACE | NORM_IGNORESYMBOLS))) ||
((flags & (LCMAP_HIRAGANA | LCMAP_KATAKANA | LCMAP_HALFWIDTH | LCMAP_FULLWIDTH)) &&
(flags & (LCMAP_SIMPLIFIED_CHINESE | LCMAP_TRADITIONAL_CHINESE))))
{
SetLastError(ERROR_INVALID_FLAGS);
return 0;
}
if (srclen < 0) srclen = strlenW(src) + 1;
TRACE("(%s,0x%08x,%s,%d,%p,%d)\n",
debugstr_w(name), flags, debugstr_wn(src, srclen), srclen, dst, dstlen);
if (!dst) /* return required string length */
{
if (flags & NORM_IGNORESYMBOLS)
{
for (len = 0; srclen; src++, srclen--)
{
WCHAR wch = *src;
/* tests show that win2k just ignores NORM_IGNORENONSPACE,
* and skips white space and punctuation characters for
* NORM_IGNORESYMBOLS.
*/
if (get_table_entry( wctype_table, wch ) & (C1_PUNCT | C1_SPACE))
continue;
len++;
}
}
else if (flags & LCMAP_FULLWIDTH)
{
for (len = 0; srclen; src++, srclen--, len++)
{
if (compose_katakana(src, srclen, NULL) == 2)
{
src++;
srclen--;
}
}
}
else if (flags & LCMAP_HALFWIDTH)
{
for (len = 0; srclen; src++, srclen--, len++)
{
WCHAR wch = *src;
/* map Hiragana to Katakana before decomposition if needed */
if ((flags & LCMAP_KATAKANA) &&
((wch >= 0x3041 && wch <= 0x3096) || wch == 0x309D || wch == 0x309E))
wch += 0x60;
if (decompose_katakana(wch, NULL, 0) == 2)
len++;
}
}
else
len = srclen;
return len;
}
if (src == dst && (flags & ~(LCMAP_LOWERCASE | LCMAP_UPPERCASE)))
{
SetLastError(ERROR_INVALID_FLAGS);
return 0;
}
if (flags & (NORM_IGNORENONSPACE | NORM_IGNORESYMBOLS))
{
for (len = dstlen, dst_ptr = dst; srclen && len; src++, srclen--)
{
WCHAR wch = *src;
if ((flags & NORM_IGNORESYMBOLS) && (get_table_entry( wctype_table, wch ) & (C1_PUNCT | C1_SPACE)))
continue;
*dst_ptr++ = wch;
len--;
}
goto done;
}
if (flags & (LCMAP_FULLWIDTH | LCMAP_HALFWIDTH | LCMAP_HIRAGANA | LCMAP_KATAKANA))
{
for (len = dstlen, dst_ptr = dst; len && srclen; src++, srclen--, len--, dst_ptr++)
{
WCHAR wch;
if (flags & LCMAP_FULLWIDTH)
{
/* map half-width character to full-width one,
e.g. U+FF71 -> U+30A2, U+FF8C U+FF9F -> U+30D7. */
if (map_to_fullwidth(src, srclen, &wch) == 2)
{
src++;
srclen--;
}
}
else
wch = *src;
if (flags & LCMAP_KATAKANA)
{
/* map hiragana to katakana, e.g. U+3041 -> U+30A1.
we can't use C3_HIRAGANA as some characters can't map to katakana */
if ((wch >= 0x3041 && wch <= 0x3096) ||
wch == 0x309D || wch == 0x309E)
wch += 0x60;
}
else if (flags & LCMAP_HIRAGANA)
{
/* map katakana to hiragana, e.g. U+30A1 -> U+3041.
we can't use C3_KATAKANA as some characters can't map to hiragana */
if ((wch >= 0x30A1 && wch <= 0x30F6) ||
wch == 0x30FD || wch == 0x30FE)
wch -= 0x60;
}
if (flags & LCMAP_HALFWIDTH)
{
/* map full-width character to half-width one,
e.g. U+30A2 -> U+FF71, U+30D7 -> U+FF8C U+FF9F. */
if (map_to_halfwidth(wch, dst_ptr, len) == 2)
{
len--;
dst_ptr++;
if (!len) break;
}
}
else
*dst_ptr = wch;
}
if (!(flags & (LCMAP_UPPERCASE | LCMAP_LOWERCASE)) || srclen)
goto done;
srclen = dst_ptr - dst;
src = dst;
}
if (flags & LCMAP_UPPERCASE)
{
for (len = dstlen, dst_ptr = dst; srclen && len; src++, srclen--)
{
*dst_ptr++ = toupperW(*src);
len--;
}
}
else if (flags & LCMAP_LOWERCASE)
{
for (len = dstlen, dst_ptr = dst; srclen && len; src++, srclen--)
{
*dst_ptr++ = tolowerW(*src);
len--;
}
}
else
{
len = min(srclen, dstlen);
memcpy(dst, src, len * sizeof(WCHAR));
dst_ptr = dst + len;
srclen -= len;
}
done:
if (srclen)
{
SetLastError(ERROR_INSUFFICIENT_BUFFER);
return 0;
}
return dst_ptr - dst;
}
/*************************************************************************
* LCMapStringW (KERNEL32.@)
*
* See LCMapStringA.
*/
INT WINAPI LCMapStringW(LCID lcid, DWORD flags, LPCWSTR src, INT srclen,
LPWSTR dst, INT dstlen)
{
TRACE("(0x%04x,0x%08x,%s,%d,%p,%d)\n",
lcid, flags, debugstr_wn(src, srclen), srclen, dst, dstlen);
return LCMapStringEx(NULL, flags, src, srclen, dst, dstlen, NULL, NULL, 0);
}
/*************************************************************************
* LCMapStringA (KERNEL32.@)
*
* Map characters in a locale sensitive string.
*
* PARAMS
* lcid [I] LCID for the conversion.
* flags [I] Flags controlling the mapping (LCMAP_ constants from "winnls.h").
* src [I] String to map
* srclen [I] Length of src in chars, or -1 if src is NUL terminated
* dst [O] Destination for mapped string
* dstlen [I] Length of dst in characters
*
* RETURNS
* Success: The length of the mapped string in dst, including the NUL terminator.
* Failure: 0. Use GetLastError() to determine the cause.
*/
INT WINAPI LCMapStringA(LCID lcid, DWORD flags, LPCSTR src, INT srclen,
LPSTR dst, INT dstlen)
{
WCHAR *bufW = NtCurrentTeb()->StaticUnicodeBuffer;
LPWSTR srcW, dstW;
INT ret = 0, srclenW, dstlenW;
UINT locale_cp = CP_ACP;
if (!src || !srclen || dstlen < 0)
{
SetLastError(ERROR_INVALID_PARAMETER);
return 0;
}
if (!(flags & LOCALE_USE_CP_ACP)) locale_cp = get_lcid_codepage( lcid );
srclenW = MultiByteToWideChar(locale_cp, 0, src, srclen, bufW, 260);
if (srclenW)
srcW = bufW;
else
{
srclenW = MultiByteToWideChar(locale_cp, 0, src, srclen, NULL, 0);
srcW = HeapAlloc(GetProcessHeap(), 0, srclenW * sizeof(WCHAR));
if (!srcW)
{
SetLastError(ERROR_NOT_ENOUGH_MEMORY);
return 0;
}
MultiByteToWideChar(locale_cp, 0, src, srclen, srcW, srclenW);
}
if (flags & LCMAP_SORTKEY)
{
if (src == dst)
{
SetLastError(ERROR_INVALID_FLAGS);
goto map_string_exit;
}
ret = wine_get_sortkey(flags, srcW, srclenW, dst, dstlen);
if (ret == 0)
SetLastError(ERROR_INSUFFICIENT_BUFFER);
else
ret++;
goto map_string_exit;
}
if (flags & SORT_STRINGSORT)
{
SetLastError(ERROR_INVALID_FLAGS);
goto map_string_exit;
}
dstlenW = LCMapStringEx(NULL, flags, srcW, srclenW, NULL, 0, NULL, NULL, 0);
if (!dstlenW)
goto map_string_exit;
dstW = HeapAlloc(GetProcessHeap(), 0, dstlenW * sizeof(WCHAR));
if (!dstW)
{
SetLastError(ERROR_NOT_ENOUGH_MEMORY);
goto map_string_exit;
}
LCMapStringEx(NULL, flags, srcW, srclenW, dstW, dstlenW, NULL, NULL, 0);
ret = WideCharToMultiByte(locale_cp, 0, dstW, dstlenW, dst, dstlen, NULL, NULL);
HeapFree(GetProcessHeap(), 0, dstW);
map_string_exit:
if (srcW != bufW) HeapFree(GetProcessHeap(), 0, srcW);
return ret;
}
/*************************************************************************
* FoldStringA (KERNEL32.@)
*
* Map characters in a string.
*
* PARAMS
* dwFlags [I] Flags controlling chars to map (MAP_ constants from "winnls.h")
* src [I] String to map
* srclen [I] Length of src, or -1 if src is NUL terminated
* dst [O] Destination for mapped string
* dstlen [I] Length of dst, or 0 to find the required length for the mapped string
*
* RETURNS
* Success: The length of the string written to dst, including the terminating NUL. If
* dstlen is 0, the value returned is the same, but nothing is written to dst,
* and dst may be NULL.
* Failure: 0. Use GetLastError() to determine the cause.
*/
INT WINAPI FoldStringA(DWORD dwFlags, LPCSTR src, INT srclen,
LPSTR dst, INT dstlen)
{
INT ret = 0, srclenW = 0;
WCHAR *srcW = NULL, *dstW = NULL;
if (!src || !srclen || dstlen < 0 || (dstlen && !dst) || src == dst)
{
SetLastError(ERROR_INVALID_PARAMETER);
return 0;
}
srclenW = MultiByteToWideChar(CP_ACP, dwFlags & MAP_COMPOSITE ? MB_COMPOSITE : 0,
src, srclen, NULL, 0);
srcW = HeapAlloc(GetProcessHeap(), 0, srclenW * sizeof(WCHAR));
if (!srcW)
{
SetLastError(ERROR_NOT_ENOUGH_MEMORY);
goto FoldStringA_exit;
}
MultiByteToWideChar(CP_ACP, dwFlags & MAP_COMPOSITE ? MB_COMPOSITE : 0,
src, srclen, srcW, srclenW);
dwFlags = (dwFlags & ~MAP_PRECOMPOSED) | MAP_FOLDCZONE;
ret = FoldStringW(dwFlags, srcW, srclenW, NULL, 0);
if (ret && dstlen)
{
dstW = HeapAlloc(GetProcessHeap(), 0, ret * sizeof(WCHAR));
if (!dstW)
{
SetLastError(ERROR_NOT_ENOUGH_MEMORY);
goto FoldStringA_exit;
}
ret = FoldStringW(dwFlags, srcW, srclenW, dstW, ret);
if (!WideCharToMultiByte(CP_ACP, 0, dstW, ret, dst, dstlen, NULL, NULL))
{
ret = 0;
SetLastError(ERROR_INSUFFICIENT_BUFFER);
}
}
HeapFree(GetProcessHeap(), 0, dstW);
FoldStringA_exit:
HeapFree(GetProcessHeap(), 0, srcW);
return ret;
}
/*************************************************************************
* FoldStringW (KERNEL32.@)
*
* See FoldStringA.
*/
INT WINAPI FoldStringW(DWORD dwFlags, LPCWSTR src, INT srclen,
LPWSTR dst, INT dstlen)
{
int ret;
switch (dwFlags & (MAP_COMPOSITE|MAP_PRECOMPOSED|MAP_EXPAND_LIGATURES))
{
case 0:
if (dwFlags)
break;
/* Fall through for dwFlags == 0 */
case MAP_PRECOMPOSED|MAP_COMPOSITE:
case MAP_PRECOMPOSED|MAP_EXPAND_LIGATURES:
case MAP_COMPOSITE|MAP_EXPAND_LIGATURES:
SetLastError(ERROR_INVALID_FLAGS);
return 0;
}
if (!src || !srclen || dstlen < 0 || (dstlen && !dst) || src == dst)
{
SetLastError(ERROR_INVALID_PARAMETER);
return 0;
}
ret = wine_fold_string(dwFlags, src, srclen, dst, dstlen);
if (!ret)
SetLastError(ERROR_INSUFFICIENT_BUFFER);
return ret;
}
/******************************************************************************
* CompareStringW (KERNEL32.@)
*
* See CompareStringA.
*/
INT WINAPI CompareStringW(LCID lcid, DWORD flags,
LPCWSTR str1, INT len1, LPCWSTR str2, INT len2)
{
return CompareStringEx(NULL, flags, str1, len1, str2, len2, NULL, NULL, 0);
}
/******************************************************************************
* CompareStringEx (KERNEL32.@)
*/
INT WINAPI CompareStringEx(LPCWSTR locale, DWORD flags, LPCWSTR str1, INT len1,
LPCWSTR str2, INT len2, LPNLSVERSIONINFO version, LPVOID reserved, LPARAM lParam)
{
DWORD supported_flags = NORM_IGNORECASE|NORM_IGNORENONSPACE|NORM_IGNORESYMBOLS|SORT_STRINGSORT
|NORM_IGNOREKANATYPE|NORM_IGNOREWIDTH|LOCALE_USE_CP_ACP;
DWORD semistub_flags = NORM_LINGUISTIC_CASING|LINGUISTIC_IGNORECASE|0x10000000;
/* 0x10000000 is related to diacritics in Arabic, Japanese, and Hebrew */
INT ret;
static int once;
if (version) FIXME("unexpected version parameter\n");
if (reserved) FIXME("unexpected reserved value\n");
if (lParam) FIXME("unexpected lParam\n");
if (!str1 || !str2)
{
SetLastError(ERROR_INVALID_PARAMETER);
return 0;
}
if (flags & ~(supported_flags|semistub_flags))
{
SetLastError(ERROR_INVALID_FLAGS);
return 0;
}
if (flags & semistub_flags)
{
if (!once++)
FIXME("semi-stub behavior for flag(s) 0x%x\n", flags & semistub_flags);
}
if (len1 < 0) len1 = strlenW(str1);
if (len2 < 0) len2 = strlenW(str2);
ret = wine_compare_string(flags, str1, len1, str2, len2);
if (ret) /* need to translate result */
return (ret < 0) ? CSTR_LESS_THAN : CSTR_GREATER_THAN;
return CSTR_EQUAL;
}
/******************************************************************************
* CompareStringA (KERNEL32.@)
*
* Compare two locale sensitive strings.
*
* PARAMS
* lcid [I] LCID for the comparison
* flags [I] Flags for the comparison (NORM_ constants from "winnls.h").
* str1 [I] First string to compare
* len1 [I] Length of str1, or -1 if str1 is NUL terminated
* str2 [I] Second string to compare
* len2 [I] Length of str2, or -1 if str2 is NUL terminated
*
* RETURNS
* Success: CSTR_LESS_THAN, CSTR_EQUAL or CSTR_GREATER_THAN depending on whether
* str1 is less than, equal to or greater than str2 respectively.
* Failure: FALSE. Use GetLastError() to determine the cause.
*/
INT WINAPI CompareStringA(LCID lcid, DWORD flags,
LPCSTR str1, INT len1, LPCSTR str2, INT len2)
{
WCHAR *buf1W = NtCurrentTeb()->StaticUnicodeBuffer;
WCHAR *buf2W = buf1W + 130;
LPWSTR str1W, str2W;
INT len1W = 0, len2W = 0, ret;
UINT locale_cp = CP_ACP;
if (!str1 || !str2)
{
SetLastError(ERROR_INVALID_PARAMETER);
return 0;
}
if (len1 < 0) len1 = strlen(str1);
if (len2 < 0) len2 = strlen(str2);
if (!(flags & LOCALE_USE_CP_ACP)) locale_cp = get_lcid_codepage( lcid );
if (len1)
{
if (len1 <= 130) len1W = MultiByteToWideChar(locale_cp, 0, str1, len1, buf1W, 130);
if (len1W)
str1W = buf1W;
else
{
len1W = MultiByteToWideChar(locale_cp, 0, str1, len1, NULL, 0);
str1W = HeapAlloc(GetProcessHeap(), 0, len1W * sizeof(WCHAR));
if (!str1W)
{
SetLastError(ERROR_NOT_ENOUGH_MEMORY);
return 0;
}
MultiByteToWideChar(locale_cp, 0, str1, len1, str1W, len1W);
}
}
else
{
len1W = 0;
str1W = buf1W;
}
if (len2)
{
if (len2 <= 130) len2W = MultiByteToWideChar(locale_cp, 0, str2, len2, buf2W, 130);
if (len2W)
str2W = buf2W;
else
{
len2W = MultiByteToWideChar(locale_cp, 0, str2, len2, NULL, 0);
str2W = HeapAlloc(GetProcessHeap(), 0, len2W * sizeof(WCHAR));
if (!str2W)
{
if (str1W != buf1W) HeapFree(GetProcessHeap(), 0, str1W);
SetLastError(ERROR_NOT_ENOUGH_MEMORY);
return 0;
}
MultiByteToWideChar(locale_cp, 0, str2, len2, str2W, len2W);
}
}
else
{
len2W = 0;
str2W = buf2W;
}
ret = CompareStringEx(NULL, flags, str1W, len1W, str2W, len2W, NULL, NULL, 0);
if (str1W != buf1W) HeapFree(GetProcessHeap(), 0, str1W);
if (str2W != buf2W) HeapFree(GetProcessHeap(), 0, str2W);
return ret;
}
/******************************************************************************
* LOCALE_Init
*/
void LOCALE_Init(void)
{
extern void CDECL __wine_init_codepages( const union cptable *ansi_cp, const union cptable *oem_cp );
extern UINT CDECL __wine_get_unix_codepage(void);
UINT ansi_cp = 1252, oem_cp = 437, mac_cp = 10000, unix_cp;
ansi_cp = get_lcid_codepage( LOCALE_SYSTEM_DEFAULT );
GetLocaleInfoW( LOCALE_SYSTEM_DEFAULT, LOCALE_IDEFAULTMACCODEPAGE | LOCALE_RETURN_NUMBER,
(LPWSTR)&mac_cp, sizeof(mac_cp)/sizeof(WCHAR) );
GetLocaleInfoW( LOCALE_SYSTEM_DEFAULT, LOCALE_IDEFAULTCODEPAGE | LOCALE_RETURN_NUMBER,
(LPWSTR)&oem_cp, sizeof(oem_cp)/sizeof(WCHAR) );
if (!(ansi_cptable = wine_cp_get_table( ansi_cp )))
ansi_cptable = wine_cp_get_table( 1252 );
if (!(oem_cptable = wine_cp_get_table( oem_cp )))
oem_cptable = wine_cp_get_table( 437 );
if (!(mac_cptable = wine_cp_get_table( mac_cp )))
mac_cptable = wine_cp_get_table( 10000 );
__wine_init_codepages( ansi_cptable, oem_cptable );
unix_cp = __wine_get_unix_codepage();
if (unix_cp != CP_UTF8) unix_cptable = wine_cp_get_table( unix_cp );
TRACE( "ansi=%03d oem=%03d mac=%03d unix=%03d\n",
ansi_cptable->info.codepage, oem_cptable->info.codepage,
mac_cptable->info.codepage, unix_cp );
}
static HANDLE NLS_RegOpenKey(HANDLE hRootKey, LPCWSTR szKeyName)
{
UNICODE_STRING keyName;
OBJECT_ATTRIBUTES attr;
HANDLE hkey;
RtlInitUnicodeString( &keyName, szKeyName );
InitializeObjectAttributes(&attr, &keyName, 0, hRootKey, NULL);
if (NtOpenKey( &hkey, KEY_READ, &attr ) != STATUS_SUCCESS)
hkey = 0;
return hkey;
}
/******************************************************************************
* EnumSystemLanguageGroupsA (KERNEL32.@)
*/
BOOL WINAPI EnumSystemLanguageGroupsA( LANGUAGEGROUP_ENUMPROCA proc, DWORD flags, LONG_PTR param )
{
return Internal_EnumSystemLanguageGroups( (LANGUAGEGROUP_ENUMPROCW)proc, flags, param, FALSE );
}
/******************************************************************************
* EnumLanguageGroupLocalesA (KERNEL32.@)
*/
BOOL WINAPI EnumLanguageGroupLocalesA( LANGGROUPLOCALE_ENUMPROCA proc, LGRPID id,
DWORD flags, LONG_PTR param )
{
return Internal_EnumLanguageGroupLocales( (LANGGROUPLOCALE_ENUMPROCW)proc, id, flags, param, FALSE );
}
/******************************************************************************
* EnumCalendarInfoA [KERNEL32.@]
*/
BOOL WINAPI EnumCalendarInfoA( CALINFO_ENUMPROCA proc, LCID lcid, CALID id, CALTYPE type )
{
return Internal_EnumCalendarInfo( (CALINFO_ENUMPROCW)proc, lcid, id, type, FALSE, FALSE, FALSE, 0 );
}
/******************************************************************************
* EnumCalendarInfoExA [KERNEL32.@]
*/
BOOL WINAPI EnumCalendarInfoExA( CALINFO_ENUMPROCEXA proc, LCID lcid, CALID id, CALTYPE type )
{
return Internal_EnumCalendarInfo( (CALINFO_ENUMPROCW)proc, lcid, id, type, FALSE, TRUE, FALSE, 0 );
}
/**************************************************************************
* EnumDateFormatsExA (KERNEL32.@)
*
* FIXME: MSDN mentions only LOCALE_USE_CP_ACP, should we handle
* LOCALE_NOUSEROVERRIDE here as well?
*/
BOOL WINAPI EnumDateFormatsExA(DATEFMT_ENUMPROCEXA proc, LCID lcid, DWORD flags)
{
return Internal_EnumDateFormats( (DATEFMT_ENUMPROCW)proc, lcid, flags, FALSE, TRUE, FALSE, 0 );
}
/**************************************************************************
* EnumDateFormatsA (KERNEL32.@)
*
* FIXME: MSDN mentions only LOCALE_USE_CP_ACP, should we handle
* LOCALE_NOUSEROVERRIDE here as well?
*/
BOOL WINAPI EnumDateFormatsA(DATEFMT_ENUMPROCA proc, LCID lcid, DWORD flags)
{
return Internal_EnumDateFormats( (DATEFMT_ENUMPROCW)proc, lcid, flags, FALSE, FALSE, FALSE, 0 );
}
/**************************************************************************
* EnumTimeFormatsA (KERNEL32.@)
*
* FIXME: MSDN mentions only LOCALE_USE_CP_ACP, should we handle
* LOCALE_NOUSEROVERRIDE here as well?
*/
BOOL WINAPI EnumTimeFormatsA( TIMEFMT_ENUMPROCA proc, LCID lcid, DWORD flags )
{
/* EnumTimeFormatsA doesn't support flags, EnumTimeFormatsW does. */
if (flags & ~LOCALE_USE_CP_ACP)
{
SetLastError(ERROR_INVALID_FLAGS);
return FALSE;
}
return Internal_EnumTimeFormats( (TIMEFMT_ENUMPROCW)proc, lcid, flags, FALSE, FALSE, 0 );
}
/******************************************************************************
* InvalidateNLSCache (KERNEL32.@)
*
* Invalidate the cache of NLS values.
*
* PARAMS
* None.
*
* RETURNS
* Success: TRUE.
* Failure: FALSE.
*/
BOOL WINAPI InvalidateNLSCache(void)
{
FIXME("() stub\n");
return FALSE;
}
/******************************************************************************
* EnumUILanguagesA (KERNEL32.@)
*/
BOOL WINAPI EnumUILanguagesA( UILANGUAGE_ENUMPROCA proc, DWORD flags, LONG_PTR param )
{
return Internal_EnumUILanguages( (UILANGUAGE_ENUMPROCW)proc, flags, param, FALSE );
}
enum locationkind {
LOCATION_NATION = 0,
LOCATION_REGION,
LOCATION_BOTH
};
struct geoinfo_t {
GEOID id;
WCHAR iso2W[3];
WCHAR iso3W[4];
GEOID parent;
INT uncode;
enum locationkind kind;
};
static const struct geoinfo_t geoinfodata[] = {
{ 2, {'A','G',0}, {'A','T','G',0}, 10039880, 28 }, /* Antigua and Barbuda */
{ 3, {'A','F',0}, {'A','F','G',0}, 47614, 4 }, /* Afghanistan */
{ 4, {'D','Z',0}, {'D','Z','A',0}, 42487, 12 }, /* Algeria */
{ 5, {'A','Z',0}, {'A','Z','E',0}, 47611, 31 }, /* Azerbaijan */
{ 6, {'A','L',0}, {'A','L','B',0}, 47610, 8 }, /* Albania */
{ 7, {'A','M',0}, {'A','R','M',0}, 47611, 51 }, /* Armenia */
{ 8, {'A','D',0}, {'A','N','D',0}, 47610, 20 }, /* Andorra */
{ 9, {'A','O',0}, {'A','G','O',0}, 42484, 24 }, /* Angola */
{ 10, {'A','S',0}, {'A','S','M',0}, 26286, 16 }, /* American Samoa */
{ 11, {'A','R',0}, {'A','R','G',0}, 31396, 32 }, /* Argentina */
{ 12, {'A','U',0}, {'A','U','S',0}, 10210825, 36 }, /* Australia */
{ 14, {'A','T',0}, {'A','U','T',0}, 10210824, 40 }, /* Austria */
{ 17, {'B','H',0}, {'B','H','R',0}, 47611, 48 }, /* Bahrain */
{ 18, {'B','B',0}, {'B','R','B',0}, 10039880, 52 }, /* Barbados */
{ 19, {'B','W',0}, {'B','W','A',0}, 10039883, 72 }, /* Botswana */
{ 20, {'B','M',0}, {'B','M','U',0}, 23581, 60 }, /* Bermuda */
{ 21, {'B','E',0}, {'B','E','L',0}, 10210824, 56 }, /* Belgium */
{ 22, {'B','S',0}, {'B','H','S',0}, 10039880, 44 }, /* Bahamas, The */
{ 23, {'B','D',0}, {'B','G','D',0}, 47614, 50 }, /* Bangladesh */
{ 24, {'B','Z',0}, {'B','L','Z',0}, 27082, 84 }, /* Belize */
{ 25, {'B','A',0}, {'B','I','H',0}, 47610, 70 }, /* Bosnia and Herzegovina */
{ 26, {'B','O',0}, {'B','O','L',0}, 31396, 68 }, /* Bolivia */
{ 27, {'M','M',0}, {'M','M','R',0}, 47599, 104 }, /* Myanmar */
{ 28, {'B','J',0}, {'B','E','N',0}, 42483, 204 }, /* Benin */
{ 29, {'B','Y',0}, {'B','L','R',0}, 47609, 112 }, /* Belarus */
{ 30, {'S','B',0}, {'S','L','B',0}, 20900, 90 }, /* Solomon Islands */
{ 32, {'B','R',0}, {'B','R','A',0}, 31396, 76 }, /* Brazil */
{ 34, {'B','T',0}, {'B','T','N',0}, 47614, 64 }, /* Bhutan */
{ 35, {'B','G',0}, {'B','G','R',0}, 47609, 100 }, /* Bulgaria */
{ 37, {'B','N',0}, {'B','R','N',0}, 47599, 96 }, /* Brunei */
{ 38, {'B','I',0}, {'B','D','I',0}, 47603, 108 }, /* Burundi */
{ 39, {'C','A',0}, {'C','A','N',0}, 23581, 124 }, /* Canada */
{ 40, {'K','H',0}, {'K','H','M',0}, 47599, 116 }, /* Cambodia */
{ 41, {'T','D',0}, {'T','C','D',0}, 42484, 148 }, /* Chad */
{ 42, {'L','K',0}, {'L','K','A',0}, 47614, 144 }, /* Sri Lanka */
{ 43, {'C','G',0}, {'C','O','G',0}, 42484, 178 }, /* Congo */
{ 44, {'C','D',0}, {'C','O','D',0}, 42484, 180 }, /* Congo (DRC) */
{ 45, {'C','N',0}, {'C','H','N',0}, 47600, 156 }, /* China */
{ 46, {'C','L',0}, {'C','H','L',0}, 31396, 152 }, /* Chile */
{ 49, {'C','M',0}, {'C','M','R',0}, 42484, 120 }, /* Cameroon */
{ 50, {'K','M',0}, {'C','O','M',0}, 47603, 174 }, /* Comoros */
{ 51, {'C','O',0}, {'C','O','L',0}, 31396, 170 }, /* Colombia */
{ 54, {'C','R',0}, {'C','R','I',0}, 27082, 188 }, /* Costa Rica */
{ 55, {'C','F',0}, {'C','A','F',0}, 42484, 140 }, /* Central African Republic */
{ 56, {'C','U',0}, {'C','U','B',0}, 10039880, 192 }, /* Cuba */
{ 57, {'C','V',0}, {'C','P','V',0}, 42483, 132 }, /* Cape Verde */
{ 59, {'C','Y',0}, {'C','Y','P',0}, 47611, 196 }, /* Cyprus */
{ 61, {'D','K',0}, {'D','N','K',0}, 10039882, 208 }, /* Denmark */
{ 62, {'D','J',0}, {'D','J','I',0}, 47603, 262 }, /* Djibouti */
{ 63, {'D','M',0}, {'D','M','A',0}, 10039880, 212 }, /* Dominica */
{ 65, {'D','O',0}, {'D','O','M',0}, 10039880, 214 }, /* Dominican Republic */
{ 66, {'E','C',0}, {'E','C','U',0}, 31396, 218 }, /* Ecuador */
{ 67, {'E','G',0}, {'E','G','Y',0}, 42487, 818 }, /* Egypt */
{ 68, {'I','E',0}, {'I','R','L',0}, 10039882, 372 }, /* Ireland */
{ 69, {'G','Q',0}, {'G','N','Q',0}, 42484, 226 }, /* Equatorial Guinea */
{ 70, {'E','E',0}, {'E','S','T',0}, 10039882, 233 }, /* Estonia */
{ 71, {'E','R',0}, {'E','R','I',0}, 47603, 232 }, /* Eritrea */
{ 72, {'S','V',0}, {'S','L','V',0}, 27082, 222 }, /* El Salvador */
{ 73, {'E','T',0}, {'E','T','H',0}, 47603, 231 }, /* Ethiopia */
{ 75, {'C','Z',0}, {'C','Z','E',0}, 47609, 203 }, /* Czech Republic */
{ 77, {'F','I',0}, {'F','I','N',0}, 10039882, 246 }, /* Finland */
{ 78, {'F','J',0}, {'F','J','I',0}, 20900, 242 }, /* Fiji Islands */
{ 80, {'F','M',0}, {'F','S','M',0}, 21206, 583 }, /* Micronesia */
{ 81, {'F','O',0}, {'F','R','O',0}, 10039882, 234 }, /* Faroe Islands */
{ 84, {'F','R',0}, {'F','R','A',0}, 10210824, 250 }, /* France */
{ 86, {'G','M',0}, {'G','M','B',0}, 42483, 270 }, /* Gambia, The */
{ 87, {'G','A',0}, {'G','A','B',0}, 42484, 266 }, /* Gabon */
{ 88, {'G','E',0}, {'G','E','O',0}, 47611, 268 }, /* Georgia */
{ 89, {'G','H',0}, {'G','H','A',0}, 42483, 288 }, /* Ghana */
{ 90, {'G','I',0}, {'G','I','B',0}, 47610, 292 }, /* Gibraltar */
{ 91, {'G','D',0}, {'G','R','D',0}, 10039880, 308 }, /* Grenada */
{ 93, {'G','L',0}, {'G','R','L',0}, 23581, 304 }, /* Greenland */
{ 94, {'D','E',0}, {'D','E','U',0}, 10210824, 276 }, /* Germany */
{ 98, {'G','R',0}, {'G','R','C',0}, 47610, 300 }, /* Greece */
{ 99, {'G','T',0}, {'G','T','M',0}, 27082, 320 }, /* Guatemala */
{ 100, {'G','N',0}, {'G','I','N',0}, 42483, 324 }, /* Guinea */
{ 101, {'G','Y',0}, {'G','U','Y',0}, 31396, 328 }, /* Guyana */
{ 103, {'H','T',0}, {'H','T','I',0}, 10039880, 332 }, /* Haiti */
{ 104, {'H','K',0}, {'H','K','G',0}, 47600, 344 }, /* Hong Kong S.A.R. */
{ 106, {'H','N',0}, {'H','N','D',0}, 27082, 340 }, /* Honduras */
{ 108, {'H','R',0}, {'H','R','V',0}, 47610, 191 }, /* Croatia */
{ 109, {'H','U',0}, {'H','U','N',0}, 47609, 348 }, /* Hungary */
{ 110, {'I','S',0}, {'I','S','L',0}, 10039882, 352 }, /* Iceland */
{ 111, {'I','D',0}, {'I','D','N',0}, 47599, 360 }, /* Indonesia */
{ 113, {'I','N',0}, {'I','N','D',0}, 47614, 356 }, /* India */
{ 114, {'I','O',0}, {'I','O','T',0}, 39070, 86 }, /* British Indian Ocean Territory */
{ 116, {'I','R',0}, {'I','R','N',0}, 47614, 364 }, /* Iran */
{ 117, {'I','L',0}, {'I','S','R',0}, 47611, 376 }, /* Israel */
{ 118, {'I','T',0}, {'I','T','A',0}, 47610, 380 }, /* Italy */
{ 119, {'C','I',0}, {'C','I','V',0}, 42483, 384 }, /* Côte d'Ivoire */
{ 121, {'I','Q',0}, {'I','R','Q',0}, 47611, 368 }, /* Iraq */
{ 122, {'J','P',0}, {'J','P','N',0}, 47600, 392 }, /* Japan */
{ 124, {'J','M',0}, {'J','A','M',0}, 10039880, 388 }, /* Jamaica */
{ 125, {'S','J',0}, {'S','J','M',0}, 10039882, 744 }, /* Jan Mayen */
{ 126, {'J','O',0}, {'J','O','R',0}, 47611, 400 }, /* Jordan */
{ 127, {'X','X',0}, {'X','X',0}, 161832256 }, /* Johnston Atoll */
{ 129, {'K','E',0}, {'K','E','N',0}, 47603, 404 }, /* Kenya */
{ 130, {'K','G',0}, {'K','G','Z',0}, 47590, 417 }, /* Kyrgyzstan */
{ 131, {'K','P',0}, {'P','R','K',0}, 47600, 408 }, /* North Korea */
{ 133, {'K','I',0}, {'K','I','R',0}, 21206, 296 }, /* Kiribati */
{ 134, {'K','R',0}, {'K','O','R',0}, 47600, 410 }, /* Korea */
{ 136, {'K','W',0}, {'K','W','T',0}, 47611, 414 }, /* Kuwait */
{ 137, {'K','Z',0}, {'K','A','Z',0}, 47590, 398 }, /* Kazakhstan */
{ 138, {'L','A',0}, {'L','A','O',0}, 47599, 418 }, /* Laos */
{ 139, {'L','B',0}, {'L','B','N',0}, 47611, 422 }, /* Lebanon */
{ 140, {'L','V',0}, {'L','V','A',0}, 10039882, 428 }, /* Latvia */
{ 141, {'L','T',0}, {'L','T','U',0}, 10039882, 440 }, /* Lithuania */
{ 142, {'L','R',0}, {'L','B','R',0}, 42483, 430 }, /* Liberia */
{ 143, {'S','K',0}, {'S','V','K',0}, 47609, 703 }, /* Slovakia */
{ 145, {'L','I',0}, {'L','I','E',0}, 10210824, 438 }, /* Liechtenstein */
{ 146, {'L','S',0}, {'L','S','O',0}, 10039883, 426 }, /* Lesotho */
{ 147, {'L','U',0}, {'L','U','X',0}, 10210824, 442 }, /* Luxembourg */
{ 148, {'L','Y',0}, {'L','B','Y',0}, 42487, 434 }, /* Libya */
{ 149, {'M','G',0}, {'M','D','G',0}, 47603, 450 }, /* Madagascar */
{ 151, {'M','O',0}, {'M','A','C',0}, 47600, 446 }, /* Macao S.A.R. */
{ 152, {'M','D',0}, {'M','D','A',0}, 47609, 498 }, /* Moldova */
{ 154, {'M','N',0}, {'M','N','G',0}, 47600, 496 }, /* Mongolia */
{ 156, {'M','W',0}, {'M','W','I',0}, 47603, 454 }, /* Malawi */
{ 157, {'M','L',0}, {'M','L','I',0}, 42483, 466 }, /* Mali */
{ 158, {'M','C',0}, {'M','C','O',0}, 10210824, 492 }, /* Monaco */
{ 159, {'M','A',0}, {'M','A','R',0}, 42487, 504 }, /* Morocco */
{ 160, {'M','U',0}, {'M','U','S',0}, 47603, 480 }, /* Mauritius */
{ 162, {'M','R',0}, {'M','R','T',0}, 42483, 478 }, /* Mauritania */
{ 163, {'M','T',0}, {'M','L','T',0}, 47610, 470 }, /* Malta */
{ 164, {'O','M',0}, {'O','M','N',0}, 47611, 512 }, /* Oman */
{ 165, {'M','V',0}, {'M','D','V',0}, 47614, 462 }, /* Maldives */
{ 166, {'M','X',0}, {'M','E','X',0}, 27082, 484 }, /* Mexico */
{ 167, {'M','Y',0}, {'M','Y','S',0}, 47599, 458 }, /* Malaysia */
{ 168, {'M','Z',0}, {'M','O','Z',0}, 47603, 508 }, /* Mozambique */
{ 173, {'N','E',0}, {'N','E','R',0}, 42483, 562 }, /* Niger */
{ 174, {'V','U',0}, {'V','U','T',0}, 20900, 548 }, /* Vanuatu */
{ 175, {'N','G',0}, {'N','G','A',0}, 42483, 566 }, /* Nigeria */
{ 176, {'N','L',0}, {'N','L','D',0}, 10210824, 528 }, /* Netherlands */
{ 177, {'N','O',0}, {'N','O','R',0}, 10039882, 578 }, /* Norway */
{ 178, {'N','P',0}, {'N','P','L',0}, 47614, 524 }, /* Nepal */
{ 180, {'N','R',0}, {'N','R','U',0}, 21206, 520 }, /* Nauru */
{ 181, {'S','R',0}, {'S','U','R',0}, 31396, 740 }, /* Suriname */
{ 182, {'N','I',0}, {'N','I','C',0}, 27082, 558 }, /* Nicaragua */
{ 183, {'N','Z',0}, {'N','Z','L',0}, 10210825, 554 }, /* New Zealand */
{ 184, {'P','S',0}, {'P','S','E',0}, 47611, 275 }, /* Palestinian Authority */
{ 185, {'P','Y',0}, {'P','R','Y',0}, 31396, 600 }, /* Paraguay */
{ 187, {'P','E',0}, {'P','E','R',0}, 31396, 604 }, /* Peru */
{ 190, {'P','K',0}, {'P','A','K',0}, 47614, 586 }, /* Pakistan */
{ 191, {'P','L',0}, {'P','O','L',0}, 47609, 616 }, /* Poland */
{ 192, {'P','A',0}, {'P','A','N',0}, 27082, 591 }, /* Panama */
{ 193, {'P','T',0}, {'P','R','T',0}, 47610, 620 }, /* Portugal */
{ 194, {'P','G',0}, {'P','N','G',0}, 20900, 598 }, /* Papua New Guinea */
{ 195, {'P','W',0}, {'P','L','W',0}, 21206, 585 }, /* Palau */
{ 196, {'G','W',0}, {'G','N','B',0}, 42483, 624 }, /* Guinea-Bissau */
{ 197, {'Q','A',0}, {'Q','A','T',0}, 47611, 634 }, /* Qatar */
{ 198, {'R','E',0}, {'R','E','U',0}, 47603, 638 }, /* Reunion */
{ 199, {'M','H',0}, {'M','H','L',0}, 21206, 584 }, /* Marshall Islands */
{ 200, {'R','O',0}, {'R','O','U',0}, 47609, 642 }, /* Romania */
{ 201, {'P','H',0}, {'P','H','L',0}, 47599, 608 }, /* Philippines */
{ 202, {'P','R',0}, {'P','R','I',0}, 10039880, 630 }, /* Puerto Rico */
{ 203, {'R','U',0}, {'R','U','S',0}, 47609, 643 }, /* Russia */
{ 204, {'R','W',0}, {'R','W','A',0}, 47603, 646 }, /* Rwanda */
{ 205, {'S','A',0}, {'S','A','U',0}, 47611, 682 }, /* Saudi Arabia */
{ 206, {'P','M',0}, {'S','P','M',0}, 23581, 666 }, /* St. Pierre and Miquelon */
{ 207, {'K','N',0}, {'K','N','A',0}, 10039880, 659 }, /* St. Kitts and Nevis */
{ 208, {'S','C',0}, {'S','Y','C',0}, 47603, 690 }, /* Seychelles */
{ 209, {'Z','A',0}, {'Z','A','F',0}, 10039883, 710 }, /* South Africa */
{ 210, {'S','N',0}, {'S','E','N',0}, 42483, 686 }, /* Senegal */
{ 212, {'S','I',0}, {'S','V','N',0}, 47610, 705 }, /* Slovenia */
{ 213, {'S','L',0}, {'S','L','E',0}, 42483, 694 }, /* Sierra Leone */
{ 214, {'S','M',0}, {'S','M','R',0}, 47610, 674 }, /* San Marino */
{ 215, {'S','G',0}, {'S','G','P',0}, 47599, 702 }, /* Singapore */
{ 216, {'S','O',0}, {'S','O','M',0}, 47603, 706 }, /* Somalia */
{ 217, {'E','S',0}, {'E','S','P',0}, 47610, 724 }, /* Spain */
{ 218, {'L','C',0}, {'L','C','A',0}, 10039880, 662 }, /* St. Lucia */
{ 219, {'S','D',0}, {'S','D','N',0}, 42487, 736 }, /* Sudan */
{ 220, {'S','J',0}, {'S','J','M',0}, 10039882, 744 }, /* Svalbard */
{ 221, {'S','E',0}, {'S','W','E',0}, 10039882, 752 }, /* Sweden */
{ 222, {'S','Y',0}, {'S','Y','R',0}, 47611, 760 }, /* Syria */
{ 223, {'C','H',0}, {'C','H','E',0}, 10210824, 756 }, /* Switzerland */
{ 224, {'A','E',0}, {'A','R','E',0}, 47611, 784 }, /* United Arab Emirates */
{ 225, {'T','T',0}, {'T','T','O',0}, 10039880, 780 }, /* Trinidad and Tobago */
{ 227, {'T','H',0}, {'T','H','A',0}, 47599, 764 }, /* Thailand */
{ 228, {'T','J',0}, {'T','J','K',0}, 47590, 762 }, /* Tajikistan */
{ 231, {'T','O',0}, {'T','O','N',0}, 26286, 776 }, /* Tonga */
{ 232, {'T','G',0}, {'T','G','O',0}, 42483, 768 }, /* Togo */
{ 233, {'S','T',0}, {'S','T','P',0}, 42484, 678 }, /* São Tomé and Príncipe */
{ 234, {'T','N',0}, {'T','U','N',0}, 42487, 788 }, /* Tunisia */
{ 235, {'T','R',0}, {'T','U','R',0}, 47611, 792 }, /* Turkey */
{ 236, {'T','V',0}, {'T','U','V',0}, 26286, 798 }, /* Tuvalu */
{ 237, {'T','W',0}, {'T','W','N',0}, 47600, 158 }, /* Taiwan */
{ 238, {'T','M',0}, {'T','K','M',0}, 47590, 795 }, /* Turkmenistan */
{ 239, {'T','Z',0}, {'T','Z','A',0}, 47603, 834 }, /* Tanzania */
{ 240, {'U','G',0}, {'U','G','A',0}, 47603, 800 }, /* Uganda */
{ 241, {'U','A',0}, {'U','K','R',0}, 47609, 804 }, /* Ukraine */
{ 242, {'G','B',0}, {'G','B','R',0}, 10039882, 826 }, /* United Kingdom */
{ 244, {'U','S',0}, {'U','S','A',0}, 23581, 840 }, /* United States */
{ 245, {'B','F',0}, {'B','F','A',0}, 42483, 854 }, /* Burkina Faso */
{ 246, {'U','Y',0}, {'U','R','Y',0}, 31396, 858 }, /* Uruguay */
{ 247, {'U','Z',0}, {'U','Z','B',0}, 47590, 860 }, /* Uzbekistan */
{ 248, {'V','C',0}, {'V','C','T',0}, 10039880, 670 }, /* St. Vincent and the Grenadines */
{ 249, {'V','E',0}, {'V','E','N',0}, 31396, 862 }, /* Bolivarian Republic of Venezuela */
{ 251, {'V','N',0}, {'V','N','M',0}, 47599, 704 }, /* Vietnam */
{ 252, {'V','I',0}, {'V','I','R',0}, 10039880, 850 }, /* Virgin Islands */
{ 253, {'V','A',0}, {'V','A','T',0}, 47610, 336 }, /* Vatican City */
{ 254, {'N','A',0}, {'N','A','M',0}, 10039883, 516 }, /* Namibia */
{ 257, {'E','H',0}, {'E','S','H',0}, 42487, 732 }, /* Western Sahara (disputed) */
{ 258, {'X','X',0}, {'X','X',0}, 161832256 }, /* Wake Island */
{ 259, {'W','S',0}, {'W','S','M',0}, 26286, 882 }, /* Samoa */
{ 260, {'S','Z',0}, {'S','W','Z',0}, 10039883, 748 }, /* Swaziland */
{ 261, {'Y','E',0}, {'Y','E','M',0}, 47611, 887 }, /* Yemen */
{ 263, {'Z','M',0}, {'Z','M','B',0}, 47603, 894 }, /* Zambia */
{ 264, {'Z','W',0}, {'Z','W','E',0}, 47603, 716 }, /* Zimbabwe */
{ 269, {'C','S',0}, {'S','C','G',0}, 47610, 891 }, /* Serbia and Montenegro (Former) */
{ 270, {'M','E',0}, {'M','N','E',0}, 47610, 499 }, /* Montenegro */
{ 271, {'R','S',0}, {'S','R','B',0}, 47610, 688 }, /* Serbia */
{ 273, {'C','W',0}, {'C','U','W',0}, 10039880, 531 }, /* Curaçao */
{ 276, {'S','S',0}, {'S','S','D',0}, 42487, 728 }, /* South Sudan */
{ 300, {'A','I',0}, {'A','I','A',0}, 10039880, 660 }, /* Anguilla */
{ 301, {'A','Q',0}, {'A','T','A',0}, 39070, 10 }, /* Antarctica */
{ 302, {'A','W',0}, {'A','B','W',0}, 10039880, 533 }, /* Aruba */
{ 303, {'X','X',0}, {'X','X',0}, 343 }, /* Ascension Island */
{ 304, {'X','X',0}, {'X','X',0}, 10210825 }, /* Ashmore and Cartier Islands */
{ 305, {'X','X',0}, {'X','X',0}, 161832256 }, /* Baker Island */
{ 306, {'B','V',0}, {'B','V','T',0}, 39070, 74 }, /* Bouvet Island */
{ 307, {'K','Y',0}, {'C','Y','M',0}, 10039880, 136 }, /* Cayman Islands */
{ 308, {'X','X',0}, {'X','X',0}, 10210824, 830, LOCATION_BOTH }, /* Channel Islands */
{ 309, {'C','X',0}, {'C','X','R',0}, 12, 162 }, /* Christmas Island */
{ 310, {'X','X',0}, {'X','X',0}, 27114 }, /* Clipperton Island */
{ 311, {'C','C',0}, {'C','C','K',0}, 10210825, 166 }, /* Cocos (Keeling) Islands */
{ 312, {'C','K',0}, {'C','O','K',0}, 26286, 184 }, /* Cook Islands */
{ 313, {'X','X',0}, {'X','X',0}, 10210825 }, /* Coral Sea Islands */
{ 314, {'X','X',0}, {'X','X',0}, 114 }, /* Diego Garcia */
{ 315, {'F','K',0}, {'F','L','K',0}, 31396, 238 }, /* Falkland Islands (Islas Malvinas) */
{ 317, {'G','F',0}, {'G','U','F',0}, 31396, 254 }, /* French Guiana */
{ 318, {'P','F',0}, {'P','Y','F',0}, 26286, 258 }, /* French Polynesia */
{ 319, {'T','F',0}, {'A','T','F',0}, 39070, 260 }, /* French Southern and Antarctic Lands */
{ 321, {'G','P',0}, {'G','L','P',0}, 10039880, 312 }, /* Guadeloupe */
{ 322, {'G','U',0}, {'G','U','M',0}, 21206, 316 }, /* Guam */
{ 323, {'X','X',0}, {'X','X',0}, 39070 }, /* Guantanamo Bay */
{ 324, {'G','G',0}, {'G','G','Y',0}, 308, 831 }, /* Guernsey */
{ 325, {'H','M',0}, {'H','M','D',0}, 39070, 334 }, /* Heard Island and McDonald Islands */
{ 326, {'X','X',0}, {'X','X',0}, 161832256 }, /* Howland Island */
{ 327, {'X','X',0}, {'X','X',0}, 161832256 }, /* Jarvis Island */
{ 328, {'J','E',0}, {'J','E','Y',0}, 308, 832 }, /* Jersey */
{ 329, {'X','X',0}, {'X','X',0}, 161832256 }, /* Kingman Reef */
{ 330, {'M','Q',0}, {'M','T','Q',0}, 10039880, 474 }, /* Martinique */
{ 331, {'Y','T',0}, {'M','Y','T',0}, 47603, 175 }, /* Mayotte */
{ 332, {'M','S',0}, {'M','S','R',0}, 10039880, 500 }, /* Montserrat */
{ 333, {'A','N',0}, {'A','N','T',0}, 10039880, 530, LOCATION_BOTH }, /* Netherlands Antilles (Former) */
{ 334, {'N','C',0}, {'N','C','L',0}, 20900, 540 }, /* New Caledonia */
{ 335, {'N','U',0}, {'N','I','U',0}, 26286, 570 }, /* Niue */
{ 336, {'N','F',0}, {'N','F','K',0}, 10210825, 574 }, /* Norfolk Island */
{ 337, {'M','P',0}, {'M','N','P',0}, 21206, 580 }, /* Northern Mariana Islands */
{ 338, {'X','X',0}, {'X','X',0}, 161832256 }, /* Palmyra Atoll */
{ 339, {'P','N',0}, {'P','C','N',0}, 26286, 612 }, /* Pitcairn Islands */
{ 340, {'X','X',0}, {'X','X',0}, 337 }, /* Rota Island */
{ 341, {'X','X',0}, {'X','X',0}, 337 }, /* Saipan */
{ 342, {'G','S',0}, {'S','G','S',0}, 39070, 239 }, /* South Georgia and the South Sandwich Islands */
{ 343, {'S','H',0}, {'S','H','N',0}, 42483, 654 }, /* St. Helena */
{ 346, {'X','X',0}, {'X','X',0}, 337 }, /* Tinian Island */
{ 347, {'T','K',0}, {'T','K','L',0}, 26286, 772 }, /* Tokelau */
{ 348, {'X','X',0}, {'X','X',0}, 343 }, /* Tristan da Cunha */
{ 349, {'T','C',0}, {'T','C','A',0}, 10039880, 796 }, /* Turks and Caicos Islands */
{ 351, {'V','G',0}, {'V','G','B',0}, 10039880, 92 }, /* Virgin Islands, British */
{ 352, {'W','F',0}, {'W','L','F',0}, 26286, 876 }, /* Wallis and Futuna */
{ 742, {'X','X',0}, {'X','X',0}, 39070, 2, LOCATION_REGION }, /* Africa */
{ 2129, {'X','X',0}, {'X','X',0}, 39070, 142, LOCATION_REGION }, /* Asia */
{ 10541, {'X','X',0}, {'X','X',0}, 39070, 150, LOCATION_REGION }, /* Europe */
{ 15126, {'I','M',0}, {'I','M','N',0}, 10039882, 833 }, /* Man, Isle of */
{ 19618, {'M','K',0}, {'M','K','D',0}, 47610, 807 }, /* Macedonia, Former Yugoslav Republic of */
{ 20900, {'X','X',0}, {'X','X',0}, 27114, 54, LOCATION_REGION }, /* Melanesia */
{ 21206, {'X','X',0}, {'X','X',0}, 27114, 57, LOCATION_REGION }, /* Micronesia */
{ 21242, {'X','X',0}, {'X','X',0}, 161832256 }, /* Midway Islands */
{ 23581, {'X','X',0}, {'X','X',0}, 10026358, 21, LOCATION_REGION }, /* Northern America */
{ 26286, {'X','X',0}, {'X','X',0}, 27114, 61, LOCATION_REGION }, /* Polynesia */
{ 27082, {'X','X',0}, {'X','X',0}, 161832257, 13, LOCATION_REGION }, /* Central America */
{ 27114, {'X','X',0}, {'X','X',0}, 39070, 9, LOCATION_REGION }, /* Oceania */
{ 30967, {'S','X',0}, {'S','X','M',0}, 10039880, 534 }, /* Sint Maarten (Dutch part) */
{ 31396, {'X','X',0}, {'X','X',0}, 161832257, 5, LOCATION_REGION }, /* South America */
{ 31706, {'M','F',0}, {'M','A','F',0}, 10039880, 663 }, /* Saint Martin (French part) */
{ 39070, {'X','X',0}, {'X','X',0}, 39070, 1, LOCATION_REGION }, /* World */
{ 42483, {'X','X',0}, {'X','X',0}, 742, 11, LOCATION_REGION }, /* Western Africa */
{ 42484, {'X','X',0}, {'X','X',0}, 742, 17, LOCATION_REGION }, /* Middle Africa */
{ 42487, {'X','X',0}, {'X','X',0}, 742, 15, LOCATION_REGION }, /* Northern Africa */
{ 47590, {'X','X',0}, {'X','X',0}, 2129, 143, LOCATION_REGION }, /* Central Asia */
{ 47599, {'X','X',0}, {'X','X',0}, 2129, 35, LOCATION_REGION }, /* South-Eastern Asia */
{ 47600, {'X','X',0}, {'X','X',0}, 2129, 30, LOCATION_REGION }, /* Eastern Asia */
{ 47603, {'X','X',0}, {'X','X',0}, 742, 14, LOCATION_REGION }, /* Eastern Africa */
{ 47609, {'X','X',0}, {'X','X',0}, 10541, 151, LOCATION_REGION }, /* Eastern Europe */
{ 47610, {'X','X',0}, {'X','X',0}, 10541, 39, LOCATION_REGION }, /* Southern Europe */
{ 47611, {'X','X',0}, {'X','X',0}, 2129, 145, LOCATION_REGION }, /* Middle East */
{ 47614, {'X','X',0}, {'X','X',0}, 2129, 34, LOCATION_REGION }, /* Southern Asia */
{ 7299303, {'T','L',0}, {'T','L','S',0}, 47599, 626 }, /* Democratic Republic of Timor-Leste */
{ 9914689, {'X','K',0}, {'X','K','S',0}, 47610, 906 }, /* Kosovo */
{ 10026358, {'X','X',0}, {'X','X',0}, 39070, 19, LOCATION_REGION }, /* Americas */
{ 10028789, {'A','X',0}, {'A','L','A',0}, 10039882, 248 }, /* Åland Islands */
{ 10039880, {'X','X',0}, {'X','X',0}, 161832257, 29, LOCATION_REGION }, /* Caribbean */
{ 10039882, {'X','X',0}, {'X','X',0}, 10541, 154, LOCATION_REGION }, /* Northern Europe */
{ 10039883, {'X','X',0}, {'X','X',0}, 742, 18, LOCATION_REGION }, /* Southern Africa */
{ 10210824, {'X','X',0}, {'X','X',0}, 10541, 155, LOCATION_REGION }, /* Western Europe */
{ 10210825, {'X','X',0}, {'X','X',0}, 27114, 53, LOCATION_REGION }, /* Australia and New Zealand */
{ 161832015, {'B','L',0}, {'B','L','M',0}, 10039880, 652 }, /* Saint Barthélemy */
{ 161832256, {'U','M',0}, {'U','M','I',0}, 27114, 581 }, /* U.S. Minor Outlying Islands */
{ 161832257, {'X','X',0}, {'X','X',0}, 10026358, 419, LOCATION_REGION }, /* Latin America and the Caribbean */
};
static const struct geoinfo_t *get_geoinfo_dataptr(GEOID geoid)
{
int min, max;
min = 0;
max = ARRAY_SIZE(geoinfodata)-1;
while (min <= max) {
const struct geoinfo_t *ptr;
int n = (min+max)/2;
ptr = &geoinfodata[n];
if (geoid == ptr->id)
/* we don't need empty entries */
return *ptr->iso2W ? ptr : NULL;
if (ptr->id > geoid)
max = n-1;
else
min = n+1;
}
return NULL;
}
/******************************************************************************
* GetUserGeoID (KERNEL32.@)
*
* Retrieves the ID of the user's geographic nation or region.
*
* PARAMS
* geoclass [I] One of GEOCLASS_NATION or GEOCLASS_REGION (SYSGEOCLASS enum from "winnls.h").
*
* RETURNS
* SUCCESS: The ID of the specified geographic class.
* FAILURE: GEOID_NOT_AVAILABLE.
*/
GEOID WINAPI GetUserGeoID(GEOCLASS geoclass)
{
GEOID ret = GEOID_NOT_AVAILABLE;
static const WCHAR geoW[] = {'G','e','o',0};
static const WCHAR nationW[] = {'N','a','t','i','o','n',0};
static const WCHAR regionW[] = {'R','e','g','i','o','n',0};
WCHAR bufferW[40], *end;
HANDLE hkey, hsubkey = 0;
UNICODE_STRING keyW;
const KEY_VALUE_PARTIAL_INFORMATION *info = (KEY_VALUE_PARTIAL_INFORMATION *)bufferW;
DWORD count = sizeof(bufferW);
RtlInitUnicodeString(&keyW, nationW);
switch (geoclass)
{
case GEOCLASS_NATION:
RtlInitUnicodeString(&keyW, nationW);
break;
case GEOCLASS_REGION:
RtlInitUnicodeString(&keyW, regionW);
break;
default:
WARN("Unknown geoclass %d\n", geoclass);
return ret;
}
if (!(hkey = create_registry_key())) return ret;
if ((hsubkey = NLS_RegOpenKey(hkey, geoW)))
{
if((NtQueryValueKey(hsubkey, &keyW, KeyValuePartialInformation,
bufferW, count, &count) == STATUS_SUCCESS ) && info->DataLength)
ret = strtolW((const WCHAR*)info->Data, &end, 10);
}
NtClose(hkey);
if (hsubkey) NtClose(hsubkey);
return ret;
}
/******************************************************************************
* SetUserGeoID (KERNEL32.@)
*
* Sets the ID of the user's geographic location.
*
* PARAMS
* geoid [I] The geographic ID to be set.
*
* RETURNS
* SUCCESS: TRUE.
* FAILURE: FALSE. GetLastError() will return ERROR_INVALID_PARAMETER if the ID was invalid.
*/
BOOL WINAPI SetUserGeoID(GEOID geoid)
{
const struct geoinfo_t *geoinfo = get_geoinfo_dataptr(geoid);
static const WCHAR geoW[] = {'G','e','o',0};
static const WCHAR nationW[] = {'N','a','t','i','o','n',0};
static const WCHAR regionW[] = {'R','e','g','i','o','n',0};
static const WCHAR formatW[] = {'%','i',0};
UNICODE_STRING nameW, keyW;
WCHAR bufferW[10];
OBJECT_ATTRIBUTES attr;
HANDLE hkey;
if (!geoinfo)
{
SetLastError(ERROR_INVALID_PARAMETER);
return FALSE;
}
if (!(hkey = create_registry_key())) return FALSE;
attr.Length = sizeof(attr);
attr.RootDirectory = hkey;
attr.ObjectName = &nameW;
attr.Attributes = 0;
attr.SecurityDescriptor = NULL;
attr.SecurityQualityOfService = NULL;
RtlInitUnicodeString(&nameW, geoW);
if (geoinfo->kind == LOCATION_NATION)
RtlInitUnicodeString(&keyW, nationW);
else
RtlInitUnicodeString(&keyW, regionW);
if (NtCreateKey(&hkey, KEY_ALL_ACCESS, &attr, 0, NULL, 0, NULL) != STATUS_SUCCESS)
{
NtClose(attr.RootDirectory);
return FALSE;
}
sprintfW(bufferW, formatW, geoinfo->id);
NtSetValueKey(hkey, &keyW, 0, REG_SZ, bufferW, (strlenW(bufferW) + 1) * sizeof(WCHAR));
NtClose(attr.RootDirectory);
NtClose(hkey);
return TRUE;
}
/******************************************************************************
* GetGeoInfoW (KERNEL32.@)
*/
INT WINAPI GetGeoInfoW(GEOID geoid, GEOTYPE geotype, LPWSTR data, int data_len, LANGID lang)
{
const struct geoinfo_t *ptr;
WCHAR buffW[12];
const WCHAR *str = buffW;
int len;
static const WCHAR fmtW[] = {'%','d',0};
TRACE("%d %d %p %d %d\n", geoid, geotype, data, data_len, lang);
if (!(ptr = get_geoinfo_dataptr(geoid))) {
SetLastError(ERROR_INVALID_PARAMETER);
return 0;
}
switch (geotype) {
case GEO_NATION:
sprintfW(buffW, fmtW, ptr->id);
break;
case GEO_ISO_UN_NUMBER:
sprintfW(buffW, fmtW, ptr->uncode);
break;
case GEO_PARENT:
sprintfW(buffW, fmtW, ptr->parent);
break;
case GEO_ISO2:
str = ptr->iso2W;
break;
case GEO_ISO3:
str = ptr->iso3W;
break;
case GEO_RFC1766:
case GEO_LCID:
case GEO_FRIENDLYNAME:
case GEO_OFFICIALNAME:
case GEO_TIMEZONES:
case GEO_OFFICIALLANGUAGES:
case GEO_LATITUDE:
case GEO_LONGITUDE:
FIXME("type %d is not supported\n", geotype);
SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
return 0;
default:
WARN("unrecognized type %d\n", geotype);
SetLastError(ERROR_INVALID_FLAGS);
return 0;
}
len = strlenW(str) + 1;
if (!data || !data_len)
return len;
memcpy(data, str, min(len, data_len)*sizeof(WCHAR));
if (data_len < len)
SetLastError(ERROR_INSUFFICIENT_BUFFER);
return data_len < len ? 0 : len;
}
/******************************************************************************
* GetGeoInfoA (KERNEL32.@)
*/
INT WINAPI GetGeoInfoA(GEOID geoid, GEOTYPE geotype, LPSTR data, int data_len, LANGID lang)
{
WCHAR *buffW;
INT len;
TRACE("%d %d %p %d %d\n", geoid, geotype, data, data_len, lang);
len = GetGeoInfoW(geoid, geotype, NULL, 0, lang);
if (!len)
return 0;
buffW = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
if (!buffW)
return 0;
GetGeoInfoW(geoid, geotype, buffW, len, lang);
len = WideCharToMultiByte(CP_ACP, 0, buffW, -1, NULL, 0, NULL, NULL);
if (!data || !data_len) {
HeapFree(GetProcessHeap(), 0, buffW);
return len;
}
len = WideCharToMultiByte(CP_ACP, 0, buffW, -1, data, data_len, NULL, NULL);
HeapFree(GetProcessHeap(), 0, buffW);
if (data_len < len)
SetLastError(ERROR_INSUFFICIENT_BUFFER);
return data_len < len ? 0 : len;
}
/******************************************************************************
* EnumSystemGeoID (KERNEL32.@)
*
* Calls a user's function for every location available on the system.
*
* PARAMS
* geoclass [I] Type of location desired (SYSGEOTYPE enum from "winnls.h")
* parent [I] GeoID for the parent
* enumproc [I] Callback function to call for each location (prototype in "winnls.h")
*
* RETURNS
* Success: TRUE.
* Failure: FALSE. Use GetLastError() to determine the cause.
*
* NOTES
* The enumproc function returns TRUE to continue enumerating
* or FALSE to interrupt the enumeration.
*
* On failure, GetLastError() returns one of the following values:
* - ERROR_INVALID_PARAMETER: no callback function was provided.
* - ERROR_INVALID_FLAGS: the location type was invalid.
*
* TODO
* On Windows 10, this function filters out those locations which
* simultaneously lack ISO and UN codes (e.g. Johnson Atoll).
*/
BOOL WINAPI EnumSystemGeoID(GEOCLASS geoclass, GEOID parent, GEO_ENUMPROC enumproc)
{
INT i;
TRACE("(%d, %d, %p)\n", geoclass, parent, enumproc);
if (!enumproc) {
SetLastError(ERROR_INVALID_PARAMETER);
return FALSE;
}
if (geoclass != GEOCLASS_NATION && geoclass != GEOCLASS_REGION && geoclass != GEOCLASS_ALL) {
SetLastError(ERROR_INVALID_FLAGS);
return FALSE;
}
for (i = 0; i < ARRAY_SIZE(geoinfodata); i++) {
const struct geoinfo_t *ptr = &geoinfodata[i];
if (geoclass == GEOCLASS_NATION && (ptr->kind != LOCATION_NATION))
continue;
/* LOCATION_BOTH counts as region. */
if (geoclass == GEOCLASS_REGION && (ptr->kind == LOCATION_NATION))
continue;
if (parent && ptr->parent != parent)
continue;
if (!enumproc(ptr->id))
return TRUE;
}
return TRUE;
}
enum {
BASE = 36,
TMIN = 1,
TMAX = 26,
SKEW = 38,
DAMP = 700,
INIT_BIAS = 72,
INIT_N = 128
};
static inline INT adapt(INT delta, INT numpoints, BOOL firsttime)
{
INT k;
delta /= (firsttime ? DAMP : 2);
delta += delta/numpoints;
for(k=0; delta>((BASE-TMIN)*TMAX)/2; k+=BASE)
delta /= BASE-TMIN;
return k+((BASE-TMIN+1)*delta)/(delta+SKEW);
}
/******************************************************************************
* IdnToAscii (KERNEL32.@)
* Implementation of Punycode based on RFC 3492.
*/
INT WINAPI IdnToAscii(DWORD dwFlags, LPCWSTR lpUnicodeCharStr, INT cchUnicodeChar,
LPWSTR lpASCIICharStr, INT cchASCIIChar)
{
static const WCHAR prefixW[] = {'x','n','-','-'};
WCHAR *norm_str;
INT i, label_start, label_end, norm_len, out_label, out = 0;
TRACE("%x %p %d %p %d\n", dwFlags, lpUnicodeCharStr, cchUnicodeChar,
lpASCIICharStr, cchASCIIChar);
norm_len = IdnToNameprepUnicode(dwFlags, lpUnicodeCharStr, cchUnicodeChar, NULL, 0);
if(!norm_len)
return 0;
norm_str = HeapAlloc(GetProcessHeap(), 0, norm_len*sizeof(WCHAR));
if(!norm_str) {
SetLastError(ERROR_NOT_ENOUGH_MEMORY);
return 0;
}
norm_len = IdnToNameprepUnicode(dwFlags, lpUnicodeCharStr,
cchUnicodeChar, norm_str, norm_len);
if(!norm_len) {
HeapFree(GetProcessHeap(), 0, norm_str);
return 0;
}
for(label_start=0; label_start<norm_len;) {
INT n = INIT_N, bias = INIT_BIAS;
INT delta = 0, b = 0, h;
out_label = out;
for(i=label_start; i<norm_len && norm_str[i]!='.' &&
norm_str[i]!=0x3002 && norm_str[i]!='\0'; i++)
if(norm_str[i] < 0x80)
b++;
label_end = i;
if(b == label_end-label_start) {
if(label_end < norm_len)
b++;
if(!lpASCIICharStr) {
out += b;
}else if(out+b <= cchASCIIChar) {
memcpy(lpASCIICharStr+out, norm_str+label_start, b*sizeof(WCHAR));
out += b;
}else {
HeapFree(GetProcessHeap(), 0, norm_str);
SetLastError(ERROR_INSUFFICIENT_BUFFER);
return 0;
}
label_start = label_end+1;
continue;
}
if(!lpASCIICharStr) {
out += 5+b; /* strlen(xn--...-) */
}else if(out+5+b <= cchASCIIChar) {
memcpy(lpASCIICharStr+out, prefixW, sizeof(prefixW));
out += 4;
for(i=label_start; i<label_end; i++)
if(norm_str[i] < 0x80)
lpASCIICharStr[out++] = norm_str[i];
lpASCIICharStr[out++] = '-';
}else {
HeapFree(GetProcessHeap(), 0, norm_str);
SetLastError(ERROR_INSUFFICIENT_BUFFER);
return 0;
}
if(!b)
out--;
for(h=b; h<label_end-label_start;) {
INT m = 0xffff, q, k;
for(i=label_start; i<label_end; i++) {
if(norm_str[i]>=n && m>norm_str[i])
m = norm_str[i];
}
delta += (m-n)*(h+1);
n = m;
for(i=label_start; i<label_end; i++) {
if(norm_str[i] < n) {
delta++;
}else if(norm_str[i] == n) {
for(q=delta, k=BASE; ; k+=BASE) {
INT t = k<=bias ? TMIN : k>=bias+TMAX ? TMAX : k-bias;
INT disp = q<t ? q : t+(q-t)%(BASE-t);
if(!lpASCIICharStr) {
out++;
}else if(out+1 <= cchASCIIChar) {
lpASCIICharStr[out++] = disp<='z'-'a' ?
'a'+disp : '0'+disp-'z'+'a'-1;
}else {
HeapFree(GetProcessHeap(), 0, norm_str);
SetLastError(ERROR_INSUFFICIENT_BUFFER);
return 0;
}
if(q < t)
break;
q = (q-t)/(BASE-t);
}
bias = adapt(delta, h+1, h==b);
delta = 0;
h++;
}
}
delta++;
n++;
}
if(out-out_label > 63) {
HeapFree(GetProcessHeap(), 0, norm_str);
SetLastError(ERROR_INVALID_NAME);
return 0;
}
if(label_end < norm_len) {
if(!lpASCIICharStr) {
out++;
}else if(out+1 <= cchASCIIChar) {
lpASCIICharStr[out++] = norm_str[label_end] ? '.' : 0;
}else {
HeapFree(GetProcessHeap(), 0, norm_str);
SetLastError(ERROR_INSUFFICIENT_BUFFER);
return 0;
}
}
label_start = label_end+1;
}
HeapFree(GetProcessHeap(), 0, norm_str);
return out;
}
/******************************************************************************
* IdnToNameprepUnicode (KERNEL32.@)
*/
INT WINAPI IdnToNameprepUnicode(DWORD dwFlags, LPCWSTR lpUnicodeCharStr, INT cchUnicodeChar,
LPWSTR lpNameprepCharStr, INT cchNameprepChar)
{
enum {
UNASSIGNED = 0x1,
PROHIBITED = 0x2,
BIDI_RAL = 0x4,
BIDI_L = 0x8
};
const WCHAR *ptr;
WORD flags;
WCHAR buf[64], *map_str, norm_str[64], ch;
DWORD i, map_len, norm_len, mask, label_start, label_end, out = 0;
BOOL have_bidi_ral, prohibit_bidi_ral, ascii_only;
TRACE("%x %p %d %p %d\n", dwFlags, lpUnicodeCharStr, cchUnicodeChar,
lpNameprepCharStr, cchNameprepChar);
if(dwFlags & ~(IDN_ALLOW_UNASSIGNED|IDN_USE_STD3_ASCII_RULES)) {
SetLastError(ERROR_INVALID_FLAGS);
return 0;
}
if(!lpUnicodeCharStr || cchUnicodeChar<-1) {
SetLastError(ERROR_INVALID_PARAMETER);
return 0;
}
if(cchUnicodeChar == -1)
cchUnicodeChar = strlenW(lpUnicodeCharStr)+1;
if(!cchUnicodeChar || (cchUnicodeChar==1 && lpUnicodeCharStr[0]==0)) {
SetLastError(ERROR_INVALID_NAME);
return 0;
}
for(label_start=0; label_start<cchUnicodeChar;) {
ascii_only = TRUE;
for(i=label_start; i<cchUnicodeChar; i++) {
ch = lpUnicodeCharStr[i];
if(i!=cchUnicodeChar-1 && !ch) {
SetLastError(ERROR_INVALID_NAME);
return 0;
}
/* check if ch is one of label separators defined in RFC3490 */
if(!ch || ch=='.' || ch==0x3002 || ch==0xff0e || ch==0xff61)
break;
if(ch > 0x7f) {
ascii_only = FALSE;
continue;
}
if((dwFlags&IDN_USE_STD3_ASCII_RULES) == 0)
continue;
if((ch>='a' && ch<='z') || (ch>='A' && ch<='Z')
|| (ch>='0' && ch<='9') || ch=='-')
continue;
SetLastError(ERROR_INVALID_NAME);
return 0;
}
label_end = i;
/* last label may be empty */
if(label_start==label_end && ch) {
SetLastError(ERROR_INVALID_NAME);
return 0;
}
if((dwFlags&IDN_USE_STD3_ASCII_RULES) && (lpUnicodeCharStr[label_start]=='-' ||
lpUnicodeCharStr[label_end-1]=='-')) {
SetLastError(ERROR_INVALID_NAME);
return 0;
}
if(ascii_only) {
/* maximal label length is 63 characters */
if(label_end-label_start > 63) {
SetLastError(ERROR_INVALID_NAME);
return 0;
}
if(label_end < cchUnicodeChar)
label_end++;
if(!lpNameprepCharStr) {
out += label_end-label_start;
}else if(out+label_end-label_start <= cchNameprepChar) {
memcpy(lpNameprepCharStr+out, lpUnicodeCharStr+label_start,
(label_end-label_start)*sizeof(WCHAR));
if(lpUnicodeCharStr[label_end-1] > 0x7f)
lpNameprepCharStr[out+label_end-label_start-1] = '.';
out += label_end-label_start;
}else {
SetLastError(ERROR_INSUFFICIENT_BUFFER);
return 0;
}
label_start = label_end;
continue;
}
map_len = 0;
for(i=label_start; i<label_end; i++) {
ch = lpUnicodeCharStr[i];
ptr = nameprep_mapping + nameprep_mapping[ch>>8];
ptr = nameprep_mapping + ptr[(ch>>4)&0x0f] + 3*(ch&0x0f);
if(!ptr[0]) map_len++;
else if(!ptr[1]) map_len++;
else if(!ptr[2]) map_len += 2;
else if(ptr[0]!=0xffff || ptr[1]!=0xffff || ptr[2]!=0xffff) map_len += 3;
}
if(map_len*sizeof(WCHAR) > sizeof(buf)) {
map_str = HeapAlloc(GetProcessHeap(), 0, map_len*sizeof(WCHAR));
if(!map_str) {
SetLastError(ERROR_NOT_ENOUGH_MEMORY);
return 0;
}
}else {
map_str = buf;
}
map_len = 0;
for(i=label_start; i<label_end; i++) {
ch = lpUnicodeCharStr[i];
ptr = nameprep_mapping + nameprep_mapping[ch>>8];
ptr = nameprep_mapping + ptr[(ch>>4)&0x0f] + 3*(ch&0x0f);
if(!ptr[0]) {
map_str[map_len++] = ch;
}else if(!ptr[1]) {
map_str[map_len++] = ptr[0];
}else if(!ptr[2]) {
map_str[map_len++] = ptr[0];
map_str[map_len++] = ptr[1];
}else if(ptr[0]!=0xffff || ptr[1]!=0xffff || ptr[2]!=0xffff) {
map_str[map_len++] = ptr[0];
map_str[map_len++] = ptr[1];
map_str[map_len++] = ptr[2];
}
}
norm_len = FoldStringW(MAP_FOLDCZONE, map_str, map_len, norm_str, ARRAY_SIZE(norm_str)-1);
if(map_str != buf)
HeapFree(GetProcessHeap(), 0, map_str);
if(!norm_len) {
if(GetLastError() == ERROR_INSUFFICIENT_BUFFER)
SetLastError(ERROR_INVALID_NAME);
return 0;
}
if(label_end < cchUnicodeChar) {
norm_str[norm_len++] = lpUnicodeCharStr[label_end] ? '.' : 0;
label_end++;
}
if(!lpNameprepCharStr) {
out += norm_len;
}else if(out+norm_len <= cchNameprepChar) {
memcpy(lpNameprepCharStr+out, norm_str, norm_len*sizeof(WCHAR));
out += norm_len;
}else {
SetLastError(ERROR_INSUFFICIENT_BUFFER);
return 0;
}
have_bidi_ral = prohibit_bidi_ral = FALSE;
mask = PROHIBITED;
if((dwFlags&IDN_ALLOW_UNASSIGNED) == 0)
mask |= UNASSIGNED;
for(i=0; i<norm_len; i++) {
ch = norm_str[i];
flags = get_table_entry( nameprep_char_type, ch );
if(flags & mask) {
SetLastError((flags & PROHIBITED) ? ERROR_INVALID_NAME
: ERROR_NO_UNICODE_TRANSLATION);
return 0;
}
if(flags & BIDI_RAL)
have_bidi_ral = TRUE;
if(flags & BIDI_L)
prohibit_bidi_ral = TRUE;
}
if(have_bidi_ral) {
ch = norm_str[0];
flags = get_table_entry( nameprep_char_type, ch );
if((flags & BIDI_RAL) == 0)
prohibit_bidi_ral = TRUE;
ch = norm_str[norm_len-1];
flags = get_table_entry( nameprep_char_type, ch );
if((flags & BIDI_RAL) == 0)
prohibit_bidi_ral = TRUE;
}
if(have_bidi_ral && prohibit_bidi_ral) {
SetLastError(ERROR_INVALID_NAME);
return 0;
}
label_start = label_end;
}
return out;
}
/******************************************************************************
* IdnToUnicode (KERNEL32.@)
*/
INT WINAPI IdnToUnicode(DWORD dwFlags, LPCWSTR lpASCIICharStr, INT cchASCIIChar,
LPWSTR lpUnicodeCharStr, INT cchUnicodeChar)
{
INT i, label_start, label_end, out_label, out = 0;
WCHAR ch;
TRACE("%x %p %d %p %d\n", dwFlags, lpASCIICharStr, cchASCIIChar,
lpUnicodeCharStr, cchUnicodeChar);
for(label_start=0; label_start<cchASCIIChar;) {
INT n = INIT_N, pos = 0, old_pos, w, k, bias = INIT_BIAS, delim=0, digit, t;
out_label = out;
for(i=label_start; i<cchASCIIChar; i++) {
ch = lpASCIICharStr[i];
if(ch>0x7f || (i!=cchASCIIChar-1 && !ch)) {
SetLastError(ERROR_INVALID_NAME);
return 0;
}
if(!ch || ch=='.')
break;
if(ch == '-')
delim = i;
if((dwFlags&IDN_USE_STD3_ASCII_RULES) == 0)
continue;
if((ch>='a' && ch<='z') || (ch>='A' && ch<='Z')
|| (ch>='0' && ch<='9') || ch=='-')
continue;
SetLastError(ERROR_INVALID_NAME);
return 0;
}
label_end = i;
/* last label may be empty */
if(label_start==label_end && ch) {
SetLastError(ERROR_INVALID_NAME);
return 0;
}
if((dwFlags&IDN_USE_STD3_ASCII_RULES) && (lpASCIICharStr[label_start]=='-' ||
lpASCIICharStr[label_end-1]=='-')) {
SetLastError(ERROR_INVALID_NAME);
return 0;
}
if(label_end-label_start > 63) {
SetLastError(ERROR_INVALID_NAME);
return 0;
}
if(label_end-label_start<4 ||
tolowerW(lpASCIICharStr[label_start])!='x' ||
tolowerW(lpASCIICharStr[label_start+1])!='n' ||
lpASCIICharStr[label_start+2]!='-' || lpASCIICharStr[label_start+3]!='-') {
if(label_end < cchASCIIChar)
label_end++;
if(!lpUnicodeCharStr) {
out += label_end-label_start;
}else if(out+label_end-label_start <= cchUnicodeChar) {
memcpy(lpUnicodeCharStr+out, lpASCIICharStr+label_start,
(label_end-label_start)*sizeof(WCHAR));
out += label_end-label_start;
}else {
SetLastError(ERROR_INSUFFICIENT_BUFFER);
return 0;
}
label_start = label_end;
continue;
}
if(delim == label_start+3)
delim++;
if(!lpUnicodeCharStr) {
out += delim-label_start-4;
}else if(out+delim-label_start-4 <= cchUnicodeChar) {
memcpy(lpUnicodeCharStr+out, lpASCIICharStr+label_start+4,
(delim-label_start-4)*sizeof(WCHAR));
out += delim-label_start-4;
}else {
SetLastError(ERROR_INSUFFICIENT_BUFFER);
return 0;
}
if(out != out_label)
delim++;
for(i=delim; i<label_end;) {
old_pos = pos;
w = 1;
for(k=BASE; ; k+=BASE) {
ch = i<label_end ? tolowerW(lpASCIICharStr[i++]) : 0;
if((ch<'a' || ch>'z') && (ch<'0' || ch>'9')) {
SetLastError(ERROR_INVALID_NAME);
return 0;
}
digit = ch<='9' ? ch-'0'+'z'-'a'+1 : ch-'a';
pos += digit*w;
t = k<=bias ? TMIN : k>=bias+TMAX ? TMAX : k-bias;
if(digit < t)
break;
w *= BASE-t;
}
bias = adapt(pos-old_pos, out-out_label+1, old_pos==0);
n += pos/(out-out_label+1);
pos %= out-out_label+1;
if((dwFlags&IDN_ALLOW_UNASSIGNED)==0 &&
get_table_entry(nameprep_char_type, n)==1/*UNASSIGNED*/) {
SetLastError(ERROR_INVALID_NAME);
return 0;
}
if(!lpUnicodeCharStr) {
out++;
}else if(out+1 <= cchASCIIChar) {
memmove(lpUnicodeCharStr+out_label+pos+1,
lpUnicodeCharStr+out_label+pos,
(out-out_label-pos)*sizeof(WCHAR));
lpUnicodeCharStr[out_label+pos] = n;
out++;
}else {
SetLastError(ERROR_INSUFFICIENT_BUFFER);
return 0;
}
pos++;
}
if(out-out_label > 63) {
SetLastError(ERROR_INVALID_NAME);
return 0;
}
if(label_end < cchASCIIChar) {
if(!lpUnicodeCharStr) {
out++;
}else if(out+1 <= cchUnicodeChar) {
lpUnicodeCharStr[out++] = lpASCIICharStr[label_end];
}else {
SetLastError(ERROR_INSUFFICIENT_BUFFER);
return 0;
}
}
label_start = label_end+1;
}
return out;
}
/******************************************************************************
* GetFileMUIPath (KERNEL32.@)
*/
BOOL WINAPI GetFileMUIPath(DWORD flags, PCWSTR filepath, PWSTR language, PULONG languagelen,
PWSTR muipath, PULONG muipathlen, PULONGLONG enumerator)
{
FIXME("stub: 0x%x, %s, %s, %p, %p, %p, %p\n", flags, debugstr_w(filepath),
debugstr_w(language), languagelen, muipath, muipathlen, enumerator);
SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
return FALSE;
}
/******************************************************************************
* GetFileMUIInfo (KERNEL32.@)
*/
BOOL WINAPI GetFileMUIInfo(DWORD flags, PCWSTR path, FILEMUIINFO *info, DWORD *size)
{
FIXME("stub: %u, %s, %p, %p\n", flags, debugstr_w(path), info, size);
SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
return FALSE;
}
/******************************************************************************
* FindNLSStringEx (KERNEL32.@)
*/
INT WINAPI FindNLSStringEx(const WCHAR *localename, DWORD flags, const WCHAR *src,
INT src_size, const WCHAR *value, INT value_size,
INT *found, NLSVERSIONINFO *version_info, void *reserved,
LPARAM sort_handle)
{
/* FIXME: this function should normalize strings before calling CompareStringEx() */
DWORD mask = flags;
int offset, inc, count;
TRACE("%s %x %s %d %s %d %p %p %p %ld\n", wine_dbgstr_w(localename), flags,
wine_dbgstr_w(src), src_size, wine_dbgstr_w(value), value_size, found,
version_info, reserved, sort_handle);
if (version_info != NULL || reserved != NULL || sort_handle != 0 ||
!IsValidLocaleName(localename) || src == NULL || src_size == 0 ||
src_size < -1 || value == NULL || value_size == 0 || value_size < -1)
{
SetLastError(ERROR_INVALID_PARAMETER);
return -1;
}
if (src_size == -1)
src_size = strlenW(src);
if (value_size == -1)
value_size = strlenW(value);
src_size -= value_size;
if (src_size < 0) return -1;
mask = flags & ~(FIND_FROMSTART | FIND_FROMEND | FIND_STARTSWITH | FIND_ENDSWITH);
count = flags & (FIND_FROMSTART | FIND_FROMEND) ? src_size + 1 : 1;
offset = flags & (FIND_FROMSTART | FIND_STARTSWITH) ? 0 : src_size;
inc = flags & (FIND_FROMSTART | FIND_STARTSWITH) ? 1 : -1;
while (count--)
{
if (CompareStringEx(localename, mask, src + offset, value_size, value, value_size, NULL, NULL, 0) == CSTR_EQUAL)
{
if (found)
*found = value_size;
return offset;
}
offset += inc;
}
return -1;
}