1/**
2 * This file has no copyright assigned and is placed in the Public Domain.
3 * This file is part of the mingw-w64 runtime package.
4 * No warranty is given; refer to the file DISCLAIMER.PD within this package.
5 */
6
7#ifndef WIN32_LEAN_AND_MEAN
8#define WIN32_LEAN_AND_MEAN
9#endif
10#include <windows.h>
11#include <locale.h>
12
13static BOOL WINAPI fallback_IsDBCSLeadByteEx(UINT cp, BYTE c)
14{
15 int i;
16 CPINFO cp_info;
17 if (GetCPInfo(cp, &cp_info) && cp_info.MaxCharSize == 2) {
18 for (i = 0; i < MAX_LEADBYTES && cp_info.LeadByte[i]; i += 2) {
19 if (c >= cp_info.LeadByte[i] && c <= cp_info.LeadByte[i+1])
20 return TRUE;
21 }
22 }
23 return FALSE;
24}
25
26_Static_assert(__builtin_types_compatible_p(__typeof__(fallback_IsDBCSLeadByteEx), __typeof__(IsDBCSLeadByteEx)),
27 "Functions fallback_IsDBCSLeadByteEx() and IsDBCSLeadByteEx() are not compatible");
28
29int __cdecl __mingw_isleadbyte_cp(int c, unsigned int cp)
30{
31 static __typeof__(IsDBCSLeadByteEx) *call_IsDBCSLeadByteEx = NULL;
32 if (!call_IsDBCSLeadByteEx) {
33 FARPROC farproc = NULL;
34 HMODULE kernel32 = GetModuleHandleA("kernel32.dll");
35 if (kernel32)
36 farproc = GetProcAddress(kernel32, "IsDBCSLeadByteEx");
37 if (!farproc)
38 farproc = (FARPROC)(PVOID)fallback_IsDBCSLeadByteEx;
39 (void)InterlockedExchangePointer((PVOID*)&call_IsDBCSLeadByteEx, (PVOID)farproc);
40 }
41 return call_IsDBCSLeadByteEx(cp, c);
42}