diff --git a/src/common/FFPlatform.h b/src/common/FFPlatform.h index cf8224dda..8655563e3 100644 --- a/src/common/FFPlatform.h +++ b/src/common/FFPlatform.h @@ -31,6 +31,10 @@ typedef struct FFPlatform { FFstrbuf userShell; FFPlatformSysinfo sysinfo; + +#if _WIN32 + uint32_t initCP; // The code page used by the console when the program started +#endif } FFPlatform; void ffPlatformInit(FFPlatform* platform); diff --git a/src/common/impl/FFPlatform.c b/src/common/impl/FFPlatform.c index ff4c45cd2..5af48610b 100644 --- a/src/common/impl/FFPlatform.c +++ b/src/common/impl/FFPlatform.c @@ -18,6 +18,7 @@ void ffPlatformInit(FFPlatform* platform) { #ifdef _WIN32 ffStrbufInit(&platform->sid); + platform->initCP = 0; #endif FFPlatformSysinfo* info = &platform->sysinfo; diff --git a/src/common/impl/init.c b/src/common/impl/init.c index 885b0b6ee..f4307e1f9 100644 --- a/src/common/impl/init.c +++ b/src/common/impl/init.c @@ -39,7 +39,7 @@ static void defaultConfig(void) { } #ifdef _WIN32 -static volatile UINT oldCp = CP_UTF8; +static UINT oldCp = CP_UTF8; void resetConsoleCP(void) { if (oldCp != CP_UTF8) { SetConsoleOutputCP(oldCp); @@ -69,6 +69,10 @@ void ffInitInstance(void) { defaultConfig(); initState(&instance.state); + +#ifdef _WIN32 + instance.state.platform.initCP = oldCp; +#endif } static volatile bool ffDisableLinewrap = false; diff --git a/src/detection/locale/locale_windows.c b/src/detection/locale/locale_windows.c index 4f5896667..75a0e47f0 100644 --- a/src/detection/locale/locale_windows.c +++ b/src/detection/locale/locale_windows.c @@ -19,8 +19,69 @@ const char* ffDetectLocale(FFstrbuf* result) { if (size <= 1) { // including '\0' return "GetUserDefaultLocaleName() failed"; } + ffStrbufAppendNWS(result, (uint32_t) size - 1, name); + if (result->length > 2 && result->chars[2] == '-') { // Windows uses '-' instead of '_' + result->chars[2] = '_'; + } - ffStrbufSetNWS(result, (uint32_t) size - 1, name); + uint32_t codePage = instance.state.platform.initCP; + if (__builtin_expect(codePage != 0, true)) { + ffStrbufAppendC(result, '.'); + + switch (codePage) { + case CP_UTF8: + ffStrbufAppendS(result, "UTF-8"); + break; + case CP_UTF7: + ffStrbufAppendS(result, "UTF-7"); + break; + case 1200: + ffStrbufAppendS(result, "UTF-16LE"); + break; + case 1201: + ffStrbufAppendS(result, "UTF-16BE"); + break; + case 936: + ffStrbufAppendS(result, "GBK"); + break; + case 54936: + ffStrbufAppendS(result, "GB18030"); + break; + case 950: + ffStrbufAppendS(result, "BIG5"); + break; + case 932: + ffStrbufAppendS(result, "Shift_JIS"); + break; + case 949: + ffStrbufAppendS(result, "EUC-KR"); + break; + case 20932: + ffStrbufAppendS(result, "EUC-JP"); + break; + + case 20866: + ffStrbufAppendS(result, "KOI8-R"); + break; + case 21866: + ffStrbufAppendS(result, "KOI8-U"); + break; + case 20127: + ffStrbufAppendS(result, "US-ASCII"); + break; + + case 874: + case 1250 ... 1258: + ffStrbufAppendS(result, "Windows-"); + ffStrbufAppendUInt(result, codePage); + break; + + default: + ffStrbufAppendS(result, "CP"); + ffStrbufAppendUInt(result, codePage); + break; + } + } return nullptr; }